0

Let's say I have folder A and Folder B. Folder A has files A1 and A2. Folder B has files B1 and B2.

I want to create a text file in each folder that list the files in that folder. So create fileA.txt in folder A that list filenames A1 and A2 and same for B

Tried the following but didn't work

for /d %%g in ("*") do (
    for /r %%y in ("*.mp4") do (
        echo file '%%y'
    ) > ./%%g/mylist.txt
) 

Any help is appreciated

2
  • 2
    Use >> to append to the file. A single > overwrites previous content, so only the last write persists.
    – LotPings
    Oct 27, 2018 at 0:54
  • @LotPings thank you so much. That got me one step further (below is my updated script). The problem now is that each of the files created in both Folder A and Folder B has the list of ALL the A & the B files. I wanna include only the A files in the mylist.txt created in the A folder and the B files for the text file in the B folder. Any suggestions? Thanks again! for /d %%g in ("*") do ( for /r %%y in ("*.mp4") do ( echo file '%%y' >> ./%%g/mylist.txt ) )
    – joe_sh
    Oct 28, 2018 at 23:00

3 Answers 3

0

This is very simple to achieve using the Command Prompt :

cd "\path\to\folder-A"
dir /b *.mp4 > fileA.txt

The switch /b will cause the listing of only the bare file names.

2
  • Thanks for the tip, but that's not what I really need. it's a huge number of folders and I need to automate it in a re-usable script (hence the batch file). Any advice on how to achieve that in a batch file is appreciated.
    – joe_sh
    Oct 28, 2018 at 22:54
  • What's the problem in using dir in a batch script?
    – harrymc
    Oct 29, 2018 at 7:41
0

Starting from %Base% folder this batch will iterate the folder structure and create mylist.txt containing all files of type %ext% found prepended with file and enclosed in single quotes.

:: Q:\Test\2018\10\27\SO_1370390.cmd
@Echo off
Set "Base=X:\start\path"
Set "ext=.mp4"

for /R "%Base%" %%A in (.) do (
    ( for %%B in ("%%~fA\*%ext%") do Echo file '%%~fB'
    ) >"%%~fA\mylist.txt"
)
0

for /d /r . %%a in (*) do >"%%a\myList.txt" dir /b "%%a"

For each directory, recursively from current active directory send the result of bare dir command the the indicated file inside the directory being processed.

You must log in to answer this question.

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