cmd file list

1

Good morning,

I have a problem to make lists of the files that are in the folders.

I have a folder that contains several folders inside, and within each subfolder there are several * .txt files. What I want is that within each subfolder there is a txt with the route information:

C:\TimeSeries\New folder\Archivo_1.txt
C:\TimeSeries\New folder\Archivo_2.txt

In the folder New folder a txt with the information above will appear. In the folder New folder 2 will appear:

C:\TimeSeries\New folder 2\Archivo_1.txt
C:\TimeSeries\New folder 2\Archivo_2.txt

This I can do by going into the subfolder (for example, in New folder ) and writing in the cmd:

dir/s/b *.txt > list_of_files.txt

But having many subfolders, I want to place myself in the folder that contains all the subfolders and with a command, a loop or something like that I go through all the subfolders and I save a txt file in each subfolder with the list of subfolders. corresponding files.

I'm not sure if it can be done.

Thank you!

Update:

To get the list of all the sub-folders with the full path you need to open the console in the folder that contains the subfolders and put the following:

for /f "delims=" %D in ('dir /a:d /b') do dir /s/b "%~fD\*.txt" > "%~fD\list_of_files.txt"
    
asked by Ahinoa 11.12.2017 в 11:53
source

1 answer

1

Of course it is possible to do it, using a batch file. Create a file with .bat extension (for example, list.bat). Edit it and put the following:

for /f "delims=" %%D in ('dir /a:d /b') do dir "%%~fD\*.*" > "%%~fD\list_of_files.txt"

then run lista.bat and you will see that you have created a list_of_files file in each subfolder with the dir of each.

Explanation of the command:

  • for /f traverses and processes each of the files specified in the in part, in this case dir /a:d /b
  • "delims=" is used so that the for does not try to search for "tokens", but returns the full text.
  • dir /a:d /b : Gets a list only of the directories, without any header
  • For each directory obtained, a dir directorio\*.* > directorio\list_of_files.txt is made, generating the list of files of the subdirectory in a file within the same subdirectory.
  • %%~f means expanding the following variable to its full path

Alternatively, if you do not want to create a bat , you can directly on the command line execute the following:

for /f "delims=" %D in ('dir /a:d /b') do dir "%~fD\*.*" > "%~fD\list_of_files.txt"

Edited

In the comments you say you need to store the full path of each file. To do this, you need to do a second for within each folder. In a bat file these would be:

for /f "delims=" %%X in ('dir /a:d /b') do for /f "delims=" %%a in ('dir "%%~fX" /b ') do echo "%%~fa" >> "%%~fX\list_of_files.txt"

and if you are going to run it on the command line:

for /f "delims=" %X in ('dir /a:d /b') do for /f "delims=" %a in ('dir "%~fX" /b ') do echo "%~fa" >> "%~fX\list_of_files.txt"
    
answered by 11.12.2017 / 12:36
source