List Files in a Subdirectory and get Relative Paths only with Windows Command Line
Using Windows command line to List files with subdirectory
I want to output: .\folder1\1.jpg,.\foler1\2.jpg....
I know if we use dir /s/b *.jpg > list.txt that will get something like C:\download\folder\folder1\1.jpg, I don't want to that, I just need .\folder1\1.jpg
Variable Clarification (below)
- Where the logic below is
FOR %X IN ("folder1","Folder2")
orSET Folders=("Folder1","Folder2","Folder3","Folder4")
you will put your"Folder1"
,"Folder2"
, and so on just like that with the double quotes and commas for each folder name you want to look for and list the./Foldername/file.jpg
to the file you are appending the echo'd output. - For the Command Line Copy Example, you will want to do a
CD /D C:\Path
to the parent directory where the subfolders where the.JPG
files exist as that logic is implicit assuming you're in the parent directory to find those child subfolders and the filetypes/extensions they contain. - For the other examples, you will just plug in the parent directory that contains all the child subfolders where the
.JPG
files exist in theSET ParentDir=
value and everything should work otherwise as expected. - The other variables that are
SET
should be self explanatory I assume as those are named pretty close to what they mean but let me know if you need further clarification otherwise.
Command Line Copy Example
FOR %X IN ("folder1","Folder2") DO FOR /F "TOKENS=*" %F IN ('DIR /B /A-D ".\%~X\*.jpg"') DO ECHO .\%~X\%~F>>C:\Path\Log.txt
Set Variables Command Line Copy Example
(Use the below to set your folder names and file extensions for the DIR
command. Also plug in the parent directory full path where the subfolders you want to append in that format to the text file is located.)
SET FileType=*.jpg
SET ParentDir=C:\Users\Name\Desktop
SET Folders=("Folder1","Folder2","Folder3","Folder4")
SET LogFile=C:\Path\LogFile.txt
CD /D "%ParentDir%"
FOR %X IN %Folders% DO FOR /F "TOKENS=*" %F IN ('DIR /B /A-D ".\%~X\%FileType%"') DO ECHO .\%~X\%~F>>"%LogFile%"
Batch Script Example with Setting Variables
@ECHO ON
SET FileType=*.jpg
SET ParentDir=C:\Users\Name\Desktop
SET Folders=("Folder1","Folder2","Folder3","Folder4")
SET LogFile=C:\Path\LogFile.txt
CD /D "%ParentDir%"
FOR %%X IN %Folders% DO FOR /F "TOKENS=*" %%F IN (
'DIR /B /A-D ".\%%~X\%FileType%"'
) DO ECHO .\%%~X\%%~F>>"%LogFile%"
GOTO EOF
Further Reading and Resources
- FOR /F
- FOR
- SET