0

1.
Searched everywhere, I do find tutorials to print list of files in directory and sub directories using dir command as follows:

@echo off
dir /S > list.txt
pause

Above code will return all files in list.txt. We can use switches to get lists in various formats. What I am trying to do is: When I run batch file output file list.txt should be placed in all Sub Directories and not only in the directory from batch file is being executed.
2.
I am trying to to rename list.txt as %current directory name%.txt and having no clues to achieve that.

manaswin
  • 41
  • 5
  • Code provided by Squashman is working fine and printing print.txt in folder and subfolder. But when I tried to print bare format output through /b switch using following code : ' del /b List.txt for /F "delims=" %%G IN ('dir /b') DO @echo "%%G">>"%%~dpGList.txt" ' It is not working. – manaswin Oct 28 '16 at 08:22
  • @Squashman can you please address bare format output issue as mentioned above... – manaswin Oct 29 '16 at 08:46

1 Answers1

1

This uses the for variable modifiers as the directory it needs to write the list.txt file.

del /s list.txt
for /F "delims=" %%G IN ('dir /b /s') DO @echo "%%G">>"%%~dpGlist.txt"
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Yes, list.txt is now being printed and placed in each folder. Is it possible to rename list.txt to %current-folder-name%.txt? – manaswin Oct 26 '16 at 14:45
  • @manaswin, would need to nest another `FOR` command to parse out the last folder name. Again you would use the command modifiers to do this. – Squashman Oct 26 '16 at 15:39
  • @Squashman, Can you (or any gentleman here) please show the code as example 2 please... Or someone may throw another piece of code entirely to print foldername.txt in each subfolder. – manaswin Oct 27 '16 at 12:46
  • @manaswin, that is unfortunately a bit harder to code and I can't figure out a clean way to do it. – Squashman Oct 27 '16 at 19:46
  • I have tried to print bare format output through /b switch. Code uses is as follows : del /b List.txt for /F "delims=" %%G IN ('dir /b') DO @echo "%%G">>"%%~dpGList.txt" But it is not working. However with /b /s switch it is working fine. – manaswin Oct 28 '16 at 08:00