Most people probably know that can use the variable%0 in a batch file to get the name of the executing batch file. However if you use that in conjunction with some you can easily split that into a drive, directory or filename.
Therefore to get the location of an executing batch file from within that batch file you can use the variable%dp0. Where d is the drive, p is the path and 0 is of course the name of the executing batch file. This comes in real handy for me because I have some batch files on network drives that do some simple installs. Since the install files are usually in the same directory as batch file I can use%dp0 as their path. Now when I double click on a batch file in windows explorer whether the drive is mapped or a UNC path the batch file has the correct path to the files. If the drive is mapped this is not really necessary because the working directory is set to the directory that the batch file is in.
CALL - Evaluate environment variables. SET - View environment variables, set local variables. SETX - Set environment variables. Q100843 - The four types of environment variable. Q286705 - Set compatibility variables in a batch file. Q242557 - Registry Settings for Folder Redirection. StackOverflow - Storing a Newline in a variable.
However if you access this directory via a UNC path this is not the case. So by using%dp0 you can get the correct directory path, even for UNC paths.
Before I took the time to figure this out I always had to map the network drive to run the batch file, but no longer. This was inspired by comments on s post. On commenter suggested changing to the directory by using 'cd /d%0.' This of course doesn't work for UNC paths, so I just used the path instead. On another note instead of using 'cd /d%0.' To change the directory you can use 'cd /d%dp0' instead. 66 Comments.
To change to the current directory of a network location (or a local path), try using the following command: pushd '%dp0' When you want to change back, just be sure to run 'popd' at the end of your script to put the path back. Yhamade -. your%dp0 idea really helped me thanks jokop -.
Excellent post. I've always wondered how to parse the%0 parameter thingy. Now I don't have to include an environment variable%workingDirectory% in every batch file that I run. The 'batch parameter modifiers' link is dead. Jim -.
Thanks for the tip!!!! Karl -. I updated the link. Puzzlehacker -. thanks!!! Your%dp0 idea really helped me!!!!!
Roman -. thanks for%dp0 command. This is helping me in many of my scripts kris -. Thx for this very nice article.
It helps me a lot. On Windows XP and Vista you may use%CD% (for current directory) Michael Prescott -.
Yes you can use%CD% assuming you are running that batch file from that directory. But I don't believe that will work if say double click it from an explorer folder. Puzzlehacker -. Ultimate post!!
Really helped me a lot. Hey do u have any more valuable info on environment variables, for loops. I know basics of these but I want to use them to apply a bit complex logic. Tnx sAc -. It is really helpful.it worked like a charm.thanks [email protected] -. So useful.
Than you Otto -. With Windows 2000, I've seen Python and some other command-line applications and compilers fail to detect the directory path from which they are executing; however, the%CD% always works for me with Windows XP and Vista. It's really easy to test to see if it works for you. Create a batch file called echoCD.bat and put the following statements in it: echo%CD% pause Now, move the file into a few directories and double-click or open a command prompt.
Works either way for me. Does it fail for anyone else? Any ideas why it fails for puzzlehacker? Michael Prescott -. Thanks it rely helps Amr301 -. Cheers Mate!
![]()
Exactly what I've been looking for! Thanks -. Hey thats superb. This is exactly what I was looking for. Suvrajit Dhar -.
I had issues with%CD% when calling a couple batch files I had in other directories from a master batch file. It returned the wrong value ( the user directory for my logon ). Your solution works beautifully and is far less 'over engineered' than the environment variable ones I saw. Thanks a lot!
Eduardo -. Thanks for the tip! Also, I never could find a good source for 'supported' commands and such for batch files. I'll be implementing this in csbatch! -. awesome post,it really clear my confusion.
One more question here: set DEVROOT=%dp0 set DEVROOT=%DEVROOT:0,-21% my question is what the DEVROOT is now? What's the meaning of 0,-21? Thanks neil -.
Thanks. With a wide variety in network drives and paths this really helped automate my scripts Ryan -.
Very cool. Wotamidoing -. Awesome! Thanks for taking the time to post this info. Sneak -. Thanks for%dp0!!!
Carlo -. Great Post. Saved me a great deal of time and frustration!
EW -. Thanks a lot! This is a life saver! John Francis -. Great information! NG -.
WOW GENIOUS!! Why the hell aren't there more guys like puzzlehacker!! Chris -. this is really good. What if my batch file is in a zip. Will i be able to get the path for my zip file.? Please help me Ravi -.
You just made my day, man. Najati -. Thanks for the nice post. It saved my day.
VJAlla -. helped me a lot, thanks i was looking for something this simple but didn't expect it really exist. Tim -. Oh man! You, awesome.
Dung -. Oh yes! Good good work! Ed -. thanks buddy you solved my big problem thanks rohit afterglow -.
Please help. Any assistance is greatly appreciated.
Tom Cusick I have a batch file on a server that is run from a scheduled process. I want to create a dated directory on the server location to backup the data files. When running the below code, I get an Error. CODE cd%dp0 REM pa-file1 FTP-idpdw Directory the file is running from for /F 'tokens=2-4 delims=/- '%%A in ('date/T') do set var=%%A%%B%%C md%var% pause xcopy%dp0.%dp0%var% REM xcopy pa-file1 FTP-idpdw. pa-file1 FTP-idpdw%var% /CODE ERROR ' Pa-file1 ftp-idpdw' CMD.EXE was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory.
' Pa-file1 ftp-idpdw ' CMD does not support UNC paths as current directories. /ERROR Tom Cusick -. cd doesn't like UNC (i.e. Network paths), either don't CD to the directory or use pushd for UNC directories instead. Puzzlehacker -. Thanks!
This is exactly what I was looking for. Greetings from Taiwan!
Jack -. This%d%p really helped me and was exactly what I was looking for thanks!
Guy -. Don't recall having this back in the MS 6.0 days.:) Thanks!
Avi -. thanks,%d0 cd%p0 is much simpler than: for%%s in (%0) do%%ds for%%s in (%0) do cd%%ps xP -.
Many thanks. It is extemely helpful. Lucy -. Thanks,didn't even know that theres such a thing as 'batch parameter modifiers' Asela -.%dp0 is kickass that helped me so much andrew -.
Awesome dude - excellent snippet. Very usefull!
Vildand -. Thanks a lot very useful!
David -. Well, 4.5 years later and your advice is still coming in handy.
This solved a significant problem I was having. Kelly -.
This is wonderful!! I always had few lines of script to parse the Directory path from%0, but this never struck me. Anand -. Why did I not think of this before. This is awesome. Solves a lot of isues for me. Dhrumil Shah -.
Thanx. This is very helpful. Swagat -. Thanks for the article.
Using%dp0 solved my problem with UNC path. Naveen -. Almost 5 years later and this is still a very helpful piece of information. Thanks and Cheers! Rudy Lattae -.
Thanks a lot. Its very helpful Vikrant -. thanks dudes help me alot. Bm -. thank you.
Dows anyone knows how to open the%dp0. WhateverFolder in the currently opened explorer instead of creating a new instance? Cloud -.
If (like me) you're having trouble with%dp0 with directory name including (for e.g.) an '&' replace: cd%dp0 with cd%0. Denis Villechalane -. this works too set path=%0. Then use%path% in batch script debo -.
This tip saved an hour a week for me, thanks. Jim -. This cd /d%dp0 syntax just saved my professional derrier. Thanks very much!!!! I just inserted into 30+ bat files that stopped working with our application that was moved from Win XP to Win 7. Thanks again!!!
Jack jack -. On Windows XP and Vista you may use%CD% (for current directory) It worked for me until Windows 7, where when I run a batch, at least as Administrator, my path if 'C: Windows System32' cd%dp0 solved thanks!
-. Excellent tip, it can be used to find the your batch executing directory when running cmd.exe as administrator direct you to C: system32 Lijian -.
thanks a lot.%dp0 helped me to fix the issue t my end.cheers guys. Dilip -. Thanks guy. You're guru of batch files. Drweb86 -.
WOW, 7 1/2 years after the original post and today I really needed this answer. You have saved me tons of greif/time in my script automation in a windows 7 enviroment. Gotta love the Internet:) John C.
I think I ran into a bug in Window's batch scripting. I cannot set variables that are in an expanded if statement.
Here is an isolated part of my script: @echo off set success=1 set Version=12345 set Target=Client set Type=456 set dir= set zip= if '%Version%' set success=0 if '%Type%' set success=0 if 11 set test=42 if%success%1 ( set test2=57005 if '%Target%'Client' ( set dir=ModName v%Version% set zip=ModName v%Version%%Type%.zip echo Version:%Version% echo Type:%Type% echo. Echo Target:%Target% echo dir:%dir% echo zip:%zip% echo. Echo test:%test% echo test2:%test2% ) ) else ( echo Not successful. ) This is the output from a brand new instance of cmd: C: Users RandomClown Desktoptest.bat Version: 12345 Type: 456 Target: Client dir: zip: test: 42 test2: What you should notice is that the single line if statement correctly sets stuff.
The multiline if will perform anything that is NOT a set. I dont think I missed anything.
Also, the multiline if statement is correctly executing the right lines, as the else ( echo Not successful. ) lines did not execute. Why did the lines not execute? You missed something;-) cmd expands variables when commands are parsed, not when they are run. It so happens that an if or for statement with a block (.
) (or actually any block) counds as a single command in that case. So when you set variables inside a block and try using them in the same block there are no variables anymore – they were replaced by the values the variables had before the block even executed. Stick a setlocal enabledelayedexpansion at the start of your batch file and use!zip! Instead of%zip%. See help set for a detailed discussion of the problem.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |