10

I want to use the Windows command line to list files with path.

For example:

~\folder1
  \1.jpg
  \2.jpg
~\folder2
  \01.jpg
  \02.jpg

I want the output to be similar to the relative paths rather than the full path though:

.\folder1\1.jpg
.\foler1\2.jpg

When I use DIR /S /B *.jpg > list.txt that lists the full path like C:\download\folder\folder1\1.jpg, I only need it to list the relative path like .\folder1\1.jpg...

Using the Linux Command Line of find ./ -iname *.jpg > list.txt, gives me the expected result but I need a solution that'll work like this with Windows.

4 Answers 4

2

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") or SET 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 the SET 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

1
3

FOR POWERSHELL - ONE LINER

You can also use PowerShell:

PS> Get-ChildItem -Recurse . | Resolve-Path -Relative

Or from cmd prompt or in a batch file:

powershell.exe "Get-ChildItem -Recurse . | Resolve-Path -Relative"
1

I was also looking for function to list the relative path only.

Here some alternative solutions...

(Please replace %cd% with your start path, e.g. C:\download\folder )

List all sub-directories and files with relative path to a specific start path:

@echo off
SET "StartPath=%cd%"

SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!"') DO (
    set "SubDirsAndFiles=%%f"
    set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
    ECHO !SubDirsAndFiles! 
)

List selected file types only with the relative path to a specific start path (e.g. *.jpg, *.png):

@echo off
SET "StartPath=%cd%"

SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!\*.jpg" "!StartPath!\*.png"') DO (
    set "SubDirsAndFiles=%%f"
    set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
    ECHO !SubDirsAndFiles! 
)

List all sub-directories and files with relative path to a specific start path (paths ends with \):

@echo off
SET "StartPath=%cd%"

SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!"') DO (
    set "SubDirsAndFiles=%%f"
    set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
    IF EXIST "!StartPath!\!SubDirsAndFiles!\" (
        REM Echo This is a folder...
        ECHO !SubDirsAndFiles!\
    ) ELSE (
        REM Echo This is a file...
        ECHO !SubDirsAndFiles!
    )
)
0

One line BATCH command:

for /r %x in (*) do set _curFilePath=%~x& call echo .\%_curFilePath:%CD%\=%

In a batch file (.bat or .cmd):

for /r %%x in (*) do set _curFilePath=%%~x& call echo .\%%_curFilePath:%CD%\=%%

"call" is used to temporarily mimic a dynamic allocation. Change "echo" by the command you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .