Computer Hope

Home / Microsoft / Microsoft DOS / %~dp0 vs %cd% 0 Members and 2 Guests are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: %~dp0 vs %cd%  (Read 22838 times)
optix2000
Topic Starter
Rookie
*
Posts: 17

Thanked: 0
OS: Other
Computer: Specs
Experience: Experienced

Noobcake


WWW
« on: April 01, 2008, 09:42:04 PM »

Hello,

Just wondering what's the difference in using %~dp0 or %cd% to find the current directory, since I'm not sure that either one will work in every case. Or do both work exactly the same?

Thanks.
Logged
Dias de verano
Guest
« Reply #1 on: April 02, 2008, 12:33:19 AM »

They are not equivalent.

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is D:\dirshow.bat:

Code:
@echo off
echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0

Run it from C:\ and this is what you see

Code:
C:\>D:\dirshow.bat
this is %cd%  C:\
this is %~dp0 D:\
« Last Edit: April 02, 2008, 09:58:10 AM by Dias de verano » Logged
Pages: [1] - (Top) Print 
Home / Microsoft / Microsoft DOS / %~dp0 vs %cd% « previous next »
Jump to:  


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC
Page created in 0.045 seconds with 18 queries.