How to go to this directory???

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

How to go to this directory???

#1 Post by Mohammad_Dos » 13 Sep 2010 05:44

Hi,
for example I want to go to this directory:
D:\my pic\new

I can use this:

D:
cd "my pic"
cd new



I want go to this directory just by one command line. What should I do?

or

for example in dos I have:

%dir%=d:\my pic\new

How to go to the directory in %dir%?

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

Re: How to go to this directory???

#2 Post by !k » 13 Sep 2010 07:25

Read cd/?

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: How to go to this directory???

#3 Post by Mohammad_Dos » 13 Sep 2010 10:02

ok. thank u
my first question answered. but my secont question not answered yet.
please guide me


new question:

for example my batch file in this directory:
d:\new folder

I run the batch file and at last now in batch file we are in this directory:
f:\

How to back to first directory in dos(d:\new folder)?
How to find the directory that runing batch file is in it and how to go to this directory?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to go to this directory???

#4 Post by aGerman » 13 Sep 2010 10:24

There are a lot of possibilities. Try PUSHD and POPD.
Example:

Code: Select all

@echo off &setlocal
echo %cd%
cd /d "D:\my pic\new"
echo %cd%
pushd "F:\"
echo %cd%
popd
echo %cd%
pause


Otherwise the drive and path of your batch file you can find in %~dp0 so you could use

Code: Select all

cd /d "%~dp0"

if you want to change the working directory to the folder your batch file is saved in.

Regards
aGerman

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

Re: How to go to this directory???

#5 Post by !k » 13 Sep 2010 10:28

Mohammad_Dos wrote:How to find the directory that runing batch file is in it and how to go to this directory?

1st way

Code: Select all

set "home=%cd%"
...
...
chdir /d %home%

2nd way

Code: Select all

chdir /d %~dp0

3rd way

Code: Select all

pushd f:\
...
...
popd

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: How to go to this directory???

#6 Post by Mohammad_Dos » 15 Sep 2010 08:44

thank u very much !k and aGerman

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: How to go to this directory???

#7 Post by Mohammad_Dos » 19 Nov 2010 07:27

and now I have a new question.
How to copy some files to directory that the batch file is in it?

for example I runed a batch file and want copy all files in "D:\new Folder\Pic" to batch file directory

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

Re: How to go to this directory???

#8 Post by !k » 20 Nov 2010 11:03

%~dp0

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: How to go to this directory???

#9 Post by Mohammad_Dos » 20 Nov 2010 12:04

thank u very very much

Post Reply