Scaver
Scaver

Reputation: 55

Batch: Getting folder info with subs into files

I have a folder structure like his. Headfolders with subfolders.

Folder1\subfolder1
Folder1\subfolder2
Folder2\subfolder1
etc.

I want to place a txt file in every subfolder with this in the textfile:

Foldername: Folder1
Subfoldername: subfolder2

The code below works, but only for one layer of folders, not with nested ones.

for /r "c:\test\subtest" %%f in (.) do (
copy "C:\info.txt" "%%~ff" > nul
echo Foldername: %%~nxf>>%%~ff\info.txt 

How can I get also the nested info into the txt file?

Upvotes: 2

Views: 46

Answers (1)

soja
soja

Reputation: 1607

If you just want two levels (folder and subfolder) I would do it simply like this:

@echo off
for /d %%a in (*) do (
    for /d %%b in ("%%~a\*") do (
        echo>"%%~b\info.txt" Foldername: %%~a
        echo>>"%%~b\info.txt" Subfoldername: %%~nxb
    )
)

If you want it in all subfolders for some variable depth it's probably easier to run a FOR /F loop on the output of DIR /S /B /AD along with some string substitution, perhaps.

Upvotes: 1

Related Questions