Click here for important information about

Batch file to find file and set DIR to PATH

Microsoft Windows xp professional w/serv...
January 5, 2011 at 02:21:09
Specs: Windows XP

I was trying to find a way to create a batch file to find for a file and user ONLY the directory to set as PATH, (eg. file test2.txt located in c:\dir1\dir2\test2.txt | path set to %PATH%;c:\dir1\dir2\)

All my attempts have led to load the full path and not only the dir.

Anyone has any ideas?

Thanks in advance


See More: Batch file to find file and set DIR to PATH

January 5, 2011 at 03:16:19
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s c:\files\my.pdf') do (
set path=%path%;%%~DPa

Life is too important to be taken seriously.


Report •

January 5, 2011 at 03:26:14
Thanks M2!!

That indeed helps, but my problem is that supposedly i do not know where the file my.pdf is, so the script needs to search for it.

Think it's possible?

Report •

January 5, 2011 at 03:58:42

Hi again,

I have this script that finds exportdemo.exe and > the output into a file:

@echo off > notesfound.txt
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (findnotes.txt) do (
dir /s/b/a-d c:\%%a 2>nul >> notesfound.txt

notesfound.txt gets this output:
c:\Program Files\Lotus\Notes\ExportDemo.exe

Then this script (both yours actually) reads from the notesfound.txt and tries to set path:

setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (notesfound.txt) do (
set path=%path%;%%~DPa

This is output:


C:\CLI\windows>setLocal EnableDelayedExpansion

C:\CLI\windows>for /F "tokens=* delims= " %a in (notesfound.txt) do (set path=C:
em;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\QuickTime\QTS
ystem\;C:\Program Files\wix;C:\SFU\common\;%~DPa )

C:\CLI\windows>(set path=C:\Perl\site\bin;C:\Perl\bin;C:\WINDOWS\system32;C:\WIN
DOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Autodesk Shared\;C:\
Program Files\QuickTime\QTSystem\;C:\Program Files\wix;C:\SFU\common\;c:\Program
Files\Lotus\Notes\ )

And yet the path for c:\Program Files\Lotus\Notes\ doesnt get in the PATH variable.

em32\Wbem;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\QuickT
ime\QTSystem\;C:\Program Files\wix;C:\SFU\common\

Any ideas of what might be happening?

Report •

Related Solutions

January 5, 2011 at 04:59:09
But wont that syntax correspond only to the root and not consider c:\dir\my.pdf ?

the previous example i posted works fine, greps the dir for the path, just doesnt set the path straight.
Think it may be breaking due to the path/filename spacing?

Report •

January 5, 2011 at 06:48:07

got it all to work except the running the script from a batch file part ;)

If i run the file 1.bat from the command line it works and the new path gets added
1.bat contains:
for /f "tokens=* delims= " %%a in (c:\cli\notesfound.txt) do set PATH=%path%;%%~DPa

but if i run 2.bat it doesnt !
2.bat contains

everything is echoed into the terminal properly but PATH is not affected.
Should i use any type of call to run the first script from the second or open a new interpreter?


Report •

January 5, 2011 at 10:16:23
@echo start c:\cli\1.bat ^& exit >> 2.bat

Happy is ONE who says I am an OttoMAN.

Report •

January 6, 2011 at 03:45:40
Thanks to all!
That did the trick ;)

Report •

Ask Question