Computing.Net > Forums > Programming > Batch files: Get absolute path

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch files: Get absolute path

Reply to Message Icon

Name: vesperdolphin
Date: March 17, 2009 at 06:11:50 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi,

I receive a file that contains the following:

\direcotry1\directory2\directory3\file1
\direcotry1\file2
\direcotry1\directory2\directory3\directory4\file3 \direcotry1\file4
\direcotry1\directory2\file5
file6

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
command1
command2
)

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.



Sponsored Link
Ads by Google

Response Number 1
Name: reno
Date: March 17, 2009 at 07:04:45 Pacific
Reply:

using find:

C:\batch>for /f "tokens=*" %a in (test.txt) do @echo %~pa|find /v "%cd:~2%\"
\direcotry1\directory2\directory3\
\direcotry1\
\direcotry1\directory2\directory3\directory4\
\direcotry1\directory2\


using if:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch files: Get absolute path

BATCH: Filename from absolute path www.computing.net/answers/programming/batch-filename-from-absolute-path/16979.html

Batch File: Get Words www.computing.net/answers/programming/batch-file-get-words/13946.html

batch file scheduling www.computing.net/answers/programming/batch-file-scheduling/11933.html