Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch files: List all files in a directory with relative paths

Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?

For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do

for /r . %%g in (*.txt) do echo %%g >> C:\temp\test.txt 

or

dir *.txt /b /s >> C:\temp\test.txt 

and I will get something like

C:\test\Doc1.txt C:\test\subdir\Doc2.txt C:\test\subdir\Doc3.txt 

If I do

for /r . %%g in (*.txt) do echo %%~nxg >> C:\temp\test.txt 

I will get something like

Doc1.txt Doc2.txt Doc3.txt 

But what I really want is:

Doc1.txt subdir\Doc2.txt subdir\Doc3.txt 

Is it possible?

If my post is too confusing: I basically want List files recursively in Linux CLI with path relative to the current directory, but just for Windows.

like image 858
Tim Meyer Avatar asked Dec 05 '11 12:12

Tim Meyer


People also ask

How do you get a list of all files in a folder and subfolders in Windows?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.

What is the command to list all files and subdirectories in a directory?

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

How do I use relative paths in a batch file?

Use Relative Paths in Your Batch Files. Reader Paul writes in with an interesting tip for using relative paths in a batch file: you can use a special code to represent the current path—useful for batch files on a Flash drive.

How to list all files and folders in a specific path?

Using the code below, the names of the folders and their associated paths are listed on column A and B: Using the Dir () function you can get the list of files and folders in a specific path. The Dir () function takes 2 input parameters, the directory path and the type of file we are looking for:

How do I list the contents of a file in batch?

Batch Script - Listing Folder Contents. The listing of folder contents can be done with the dir command. This command allows you to see the available files and directories in the current directory. The dir command also shows the last modification date and time, as well as the file size.

How do I find the path of a batch file?

If you use "%~dp0" (sans quotes) in a batch file, this will point to the batch file's path. For example : SET MAC=00:00:00:00:00:00.


1 Answers

The simplest (but not the fastest) way to iterate a directory tree and list relative file paths is to use FORFILES.

forfiles /s /m *.txt /c "cmd /c echo @relpath" 

The relative paths will be quoted with a leading .\ as in

".\Doc1.txt" ".\subdir\Doc2.txt" ".\subdir\Doc3.txt" 


To remove quotes:

for /f %%A in ('forfiles /s /m *.txt /c "cmd /c echo @relpath"') do echo %%~A 


To remove quotes and the leading .\:

setlocal disableDelayedExpansion for /f "delims=" %%A in ('forfiles /s /m *.txt /c "cmd /c echo @relpath"') do (   set "file=%%~A"   setlocal enableDelayedExpansion   echo !file:~2!   endlocal ) 

or without using delayed expansion

for /f "tokens=1* delims=\" %%A in (   'forfiles /s /m *.txt /c "cmd /c echo @relpath"' ) do for %%F in (^"%%B) do echo %%~F 
like image 125
dbenham Avatar answered Sep 20 '22 00:09

dbenham