Set process path as the working dir

March 12, 2011 at 09:19:58
Specs: Windows XP, AMD Athlon 5400+ // 3GB RAM
For example, there is a file testfile.exe. How can I make the work dir of my batch file the location of that file if the only thing I know is the name of the file/process and the fact that it is running at the moment?

See More: Set process path as the working dir

Report •

March 12, 2011 at 10:44:23
If the batch is running in the same dir as the ".exe"


Happy is ONE who says I am an OttoMAN.

Report •

March 12, 2011 at 13:23:25
Thanks for your reply.

The batch is not in the same folder as the exe. I somehow need to take the path from the process.

Report •

March 12, 2011 at 15:02:31
The problem with this is that there can be multiple processes with the same executable
name in different locations. So how do you choose which one?

Even then it might need an outside utility because tasklist won't output the whole path.

Here is a Windows XP ONLY script that will list the paths:

@echo off

ver | >nul find /i " xp " || (
    echo this is an XP ONLY SCRIPT!
    goto :eof

set process="testfile.exe"
set fn="%temp%\msinforeport%random%"

> %fn% type nul
start /wait "" "%commonprogramfiles%\microsoft shared\msinfo\msinfo32.exe" /categories +SWEnvRunningTasks /report %fn%

for /f "tokens=1,2 delims=	" %%a in (' find /i %process% ^< %fn% ') do (
    echo %%a is at: %%b
del /q %fn%


Report •

Related Solutions

March 12, 2011 at 15:32:34
Thanks for your reply.

This file can have only one instance running at a time so that can't be a problem.
And is it possible to make it work on Vista and 7?

Report •

March 12, 2011 at 16:58:59
The reason that only works properly on xp (and perhaps below) is because outputting
individual categories was removed from msinfo32 after xp...

I found some VBscript to kill process here:

Slightly modified and embedded it in a batch..

Be aware that you probably know as much VBscript as me ;)

@echo off

set process="'testfile.exe'"

    echo:Dim objWMIService, objProcess, colProcess
    echo:Dim strComputer, strProcess
    echo:strComputer = "."
    echo:Set objArgs = WScript.Arguments
    echo:strProcess = %process%
    echo:Set objWMIService = GetObject^("winmgmts:" ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2"^)
    echo:Set colProcess = objWMIService.ExecQuery ^("Select * from Win32_Process Where Name = " ^& strProcess ^)
    echo:For Each objProcess in colProcess
    echo:   wscript.echo objProcess.ExecutablePath
) > "%temp%\getprocesspath.vbs"

for /f "tokens=*" %%a in (' cscript //nologo "%temp%\getprocesspath.vbs" ') do (
    echo %%a


Report •

March 13, 2011 at 05:15:29
Thanks for your reply.

The script you posted does show the process path and it works on win7.
But how can I set the process path as the work dir in my batch?

Report •

March 13, 2011 at 06:36:23
cd "%%~dpa"

Instead of

echo %%a

Report •

Ask Question