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

lynx pravoka
  • 71
  • 1
  • 1
  • 9
  • `dir /S /B` returns full paths... – aschipfl Oct 13 '17 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. – lynx pravoka Oct 13 '17 at 10:42

2 Answers2

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!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 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? – lynx pravoka Oct 13 '17 at 11:00
  • @ 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 – lynx pravoka Oct 13 '17 at 12:17
  • 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... – aschipfl Oct 13 '17 at 16:25
  • 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 Feb 19 '23 at 11:53
  • @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"`… – aschipfl Feb 20 '23 at 09:57
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

andi0815
  • 129
  • 1
  • 6