How to jump to the root directory from current directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

How to jump to the root directory from current directory

#1 Post by radiantracy » 02 Jan 2013 01:08

Hi,

My requirement is that if I am in C:/Test/Folder1/Folder2, the batch command shud jump to C:/Test/Folder1. What is the batch command to do the same. My code is :
SET ARGUMENTS=%1
Set PROJROOTFOLDER=%ARGUMENTS%
popd
ECHO %PROJROOTFOLDER%

here %1 gets arg from user and it shud jump to the previous folder. How shud i modify the code jump to a directory above ?

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: How to jump to the root directory from current directory

#2 Post by john924xps » 02 Jan 2013 01:20

That code's unnecessary. You could just execute "cd..". That would make the program enter its parent directory

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: How to jump to the root directory from current directory

#3 Post by radiantracy » 02 Jan 2013 01:24

Hi john924xps, I tried cd.. but its not moving to the root directory.

SET ARGUMENTS=%1

The %1 is fetched from the console of another application which gives oly the program directory say C:/Workspace/Project.
I want the arg to be replaced with C:/Workspace.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to jump to the root directory from current directory

#4 Post by abc0502 » 02 Jan 2013 02:37

You can use the pushd command followed by the location you want,
and the CD command should take the switch /D to change to that directory.
as it might that the location you want to change to is behind the current one like to change from

Code: Select all

C:\Documents and Settings\admin>
to

Code: Select all

C:\
using the /D switch will do.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to jump to the root directory from current directory

#5 Post by foxidrive » 02 Jan 2013 02:48

This will set the variable to one directory less.

Code: Select all

@echo off
SET ARGUMENTS=%1
for %%a in ("%~dp1") do set "PROJROOTFOLDER=%%a"
ECHO "%PROJROOTFOLDER%"
pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to jump to the root directory from current directory

#6 Post by Squashman » 02 Jan 2013 06:45

CD ..

And what you are referring to is not the ROOT. Root would be the drive letter.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to jump to the root directory from current directory

#7 Post by !k » 03 Jan 2013 05:49

"Determine parent folder name without entire folder path" viewtopic.php?f=3&t=2427

skat3444
Posts: 7
Joined: 03 Jan 2013 04:26

Re: How to jump to the root directory from current directory

#8 Post by skat3444 » 04 Jan 2013 06:23

you can use the command CD..

example:


------------------------------
cd %username%\Downloads
start "downloaded file"
-----------------------------

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: How to jump to the root directory from current directory

#9 Post by radiantracy » 04 Jan 2013 22:47

thanks a lot. working on all the possibilities posted.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: How to jump to the root directory from current directory

#10 Post by carlsomo » 15 Jan 2013 23:20

Code: Select all

set fullpath=c:\windows\system32
d:
echo %cd%
echo %fullpath%
cd /d %fullpath%\.\..
echo %__CD__% is the parent dir of fullpath with a trailing '\'
echo %cd% is the parent dir of fullpath without the trailing '\'

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: How to jump to the root directory from current directory

#11 Post by shirulkar » 16 Jan 2013 00:03

Cd \.
Above command is the best to change root directory.

suresh_knv
Posts: 5
Joined: 21 Jan 2013 07:09

Re: How to jump to the root directory from current directory

#12 Post by suresh_knv » 25 Jan 2013 02:47

cd .. is the best command to use to get to the just above parent folder and cd \ is used to get to the drive letter as mentioned above...

Post Reply