0

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

2
  • dir /S /B returns full paths... Commented Oct 13, 2017 at 10:33
  • @aschipfl thx its that i want. by the way do you know how pass the value from dir to next command in 1 line? at now i try dir filename /s /b | del i dont know how to bypass the return to del. Commented Oct 13, 2017 at 10:42

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

5 Comments

Cool thats i want. now i try to infinite loop. i get error in for /f delims with basic loop :loop and goto loop how i can achieve this without hungry resource?
@ aschipfl sory if i interrupt u. how can i get 1 down folder from absolute path? lets say C:\test\boo\filename.txt. i want to get C:\test\boo\ only. so far i try is with find str but like something wrong.` set file = filename for /F "delims=" %%I in ('dir /S /B %file&') do findstr,%file%,, "%%I" from above code i inialize filename to variabel file and using findstr to replace with 3 argument. 1. filename. 2. change to empty. 3. output from dir. to use later with rmdir. thx
To point to the parent directory of an item, simply append \.. (like %I\.. in my code); to resolve it (so to change C:\test\boo\filename.txt\.. to C:\test\boo) use a standard for loop (no /F): for %J in ("C:\test\boo\filename.txt\..") do echo/%~J, or for %J in ("%I\..") do echo/%~J in my code...
Sorry, for me this is answer does not really help: dir /s actually searches the whole harddrive (below the cwd) for files with the same name and finally outputs all them with their absolute path. This: - does not convert a single file path but could output several paths - takes soo long
@ChrisFetz, to search in the current working directory, just use dir /B without /S: for /F "delims=" %I in ('dir /B "filename.txt"') do set "FILE=%~fI"
0

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

2 Comments

Why don't you like %~f1? ;-)
i get the answer from previous answer. i don't know why he delete his answer. its just a simple options that i dont mention in dir. /b

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.