I am creating .txt files with a list of the file names within a given subfolder. I have around 100 subfolders within each of the 18 main folders, so I would like batch create the .txt files to save time if possible. For each subfolder, I am using the following code

dir /b > filenames.txt

Can I do this in larger batches from all subfolders within each main folder?



You can iterate recursively on all files in all subfolders of a given folder with FOR /R. I believe the following code does the trick, assuming you want to parse D:\Test :

for /R "D:\Test" %I in (*) do @echo "%I" >> filenames.txt

EDIT : The previous code outputs the full path to each file it finds, if you only want the filenames, use this code instead :

for /R "D:\Test" %I in (*) do @echo "%~nxI" >> filenames.txt
  • This does not do what the OP is asking, which is for a separate listing file in each directory. – DavidPostill May 11 at 16:15
  • Thanks for answer, Louis! And then thanks for clarifying, David! I adjusted the code slightly and the outcomes works the same for what I need. I did : for /R %I in (*) do @echo "%~nxI" >> filenames.txt – Alex M. May 13 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.