Batch files: Get value out of string
-
03-07-2019 - |
Question
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 filename (file1, file2, file3, file4, file5, ...). This is because I need to perform some actions for each file.
Thanks a lot in advance for your help.
Solution
Use this. Just replace the echo inside the loop to do whatever it is you need to do to file1, file2 and so on.
@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%i in (infile.txt) do (
set x=%%~ni
echo !x!
)
endlocal
OTHER TIPS
Are you looking for this command?
dir /r /b /A:-D
EDIT:
I am sorry I did not understand that correctly. You can use that dir command to collect filenames directly in the similar manner as you asked.
You cannot use too many string processing function in batch files, but what you want is possible:
@echo off
for /f %%I in (inputfile.txt) DO ECHO %%~nxI
This will print filenames without path from inputfile.txt.