kedit.png
Just spend a few hours on a simple batch job issue 🙁

I had a job.cmd file I executed, but the containing commands was not executed as Windows could not locate the filename.exe files 😐  The problem was rather simple, the current directory (%cd%) while executing the job.cmd file was “c:windowssystem32”..  doh 🙁

Anyhow, found out that there is actually a simple way around this;

@Echo off
set IDP=%~dp0
set OLDDIR=%cd%
cd /d %IDP%
filename.exe /S
cd /d %OLDDIR%

This is rather simple, IDP (%IDP%) is set to the executing batch file’s directory via the IDP=%~dp0 and finally OLDDIR contain the ‘before’ current directory.  If you need to set the %cd% at the end, well no probably not (but hey I’m a nice guy 😉 )..

For further details;
http://weblogs.asp.net/whaggard/archive/2005/01/28/get-directory-path-of-an-executing-batch-file.aspx

http://technet2.microsoft.com/windowsserver/en/library/97731e49-ffa3-4918-87fb-5318743f29321033.mspx?mfr=true