Batch files: Get absolute path

March 17, 2009 at 06:11:50
Specs: Windows XP


I receive a file that contains the following:

\direcotry1\directory2\directory3\directory4\file3 \direcotry1\file4

The amount of files in the file and the amount of directories are variable.

What I need is the path only.
\direcotry1\directory2\directory3\ for file1 \direcotry1\ for file2
\direcotry1\directory2\directory3\directory4\ for file3 \direcotry1\ for file4
\direcotry1\directory2\ for file5
and nothing for file6

I used the variable %%~pi which works for all except for the last one. For the last one it returns \cft\
I guess the \cft\ is returned as the program that is calling the bat file launches it from there.

FOR /F %%i in (test.txt) DO (
echo %%~pi

Does anyone know how I can avoid that the batfile returns \cft\? I want the bat to return nothing when there's no path.

Thanks a lot in advance for your help.

See More: Batch files: Get absolute path

Report •

March 17, 2009 at 07:04:45
using find:
C:\batch>for /f "tokens=*" %a in (test.txt) do @echo %~pa|find /v "%cd:~2%\"

using if:

C:\batch>for /f "tokens=*" %a in (test.txt) do @if not .%~pa==.%cd:~2%\ echo %~pa

Report •
Related Solutions

Ask Question