lynx pravoka
lynx pravoka

Reputation: 71

CMD Windows find/search and return absolute path

is there a way to return absolute path of a file and or folder by find?

let's say filename.txt in C:\test

set file=filename.txt

dir %file% /s 

at now it return c:\test in line 6. but what i want is it return absolute path

c:\test\filename.txt and set it to variabel to be use next time.

thank you

Upvotes: 0

Views: 14650

Answers (2)

aschipfl
aschipfl

Reputation: 34949

Simply add the /B option to the dir/S command line, so it returns full absolute paths.

To capture the output of this command line, use a for /F loop:

for /F "delims=" %I in ('dir /S /B "filename.txt"') do rem/ Do something with `%I`...

For example, to delete matching files, do this:

for /F "delims=" %I in ('dir /S /B "filename.txt"') do del "%I"

Note that you have to double the % signs for this code to be used within a batch file, so %I becomes %%I!

Upvotes: 1

andi0815
andi0815

Reputation: 130

Assuming that you get the filename as a first argument to a batch script, your batch-script could look like this:

set file=%~dpnx1
echo %file%

Calling the script will return the the absolute path to the given file and you may use it as a variable onwards. More details on this can be found in the post: Resolve absolute path from relative path and/or file name

Upvotes: 0

Related Questions