0

I have a several dozen folders with multiple files in them, and I was wondering how (or if it's possible) to echo the contents of each folder in a text file using, for example, dir > INDEX.txt with a batch file or from the command line.

I know that executing FOR /D %A IN (X:\destinationFolder*.*) DO (DIR >> INDEX.TXT "%A\") gets me a text file in the parent directory with all of the folders listed with their contents, but can I make individual indices for each folder? Or am I asking too much of batch scripting?

Not sure if it's crucial info, but I'm running Windows 10. Any help is greatly appreciated.

  • 1
    by indices do you mean just having a bit of space between your dir listings? or could you clarify by adding what you are thinking the output should look like by editing your question – mael' Jul 2 at 20:01
1

Not really sure what you're looking for here, but throwing this together was more fun than working:

@echo off

set "dir=C:\Your\Top\Folder"

del /f /q "%dir%\index.txt"

if exist "%dir%" (
    echo(
    echo Report for:
    echo %dir%
    echo(
    echo Subfolders:
    dir /a:d /o:n /b "%dir%"
    echo(
    echo Files:
    dir /a:-d /o:n /b "%dir%"
    echo(
    echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
)>>"%dir%\index.txt"

for /d /r "%dir%" %%A in (*) do (
    echo(
    echo Report for:
    echo %%A
    echo(
    echo Subfolders:
    dir /a:d /o:n /b "%%A"
    echo(
    echo Files:
    dir /a:-d /o:n /b "%%A"
    echo(
    echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
)>>"%dir%\index.txt"

Naturally, you can keep/toss/change any part of this to make your output file look the way you want - so this is really just to give you an idea. This sets a dir variable as your top-folder so we can change things up easier. The if exist is there just to get the dir output for that top-folder - otherwise we have a for /d /r command that will loop recursively through it. The output layout is personal preference based on what I found the most appealing (without worrying about it too much) to look at after looping this through several places. You'll notice after setting the dir variable the first thing the batch does is delete the index file so I wouldn't have to manually do it after every time I checked it - otherwise >> will just keep echo'ing multiple runs to the end of it.

If you were wanting a separate index file for each subfolder, all you have to do is enable delayed expansion and nest the main part of the for loop like this:

setlocal enabledelayedexpansion
for /d /r "%dir%" %%A in (*) do (
    set "name=%%~nA"
    (echo(
    echo Report for:
    echo %%A
    echo(
    echo Subfolders:
    dir /a:d /o:n /b "%%A"
    echo(
    echo Files:
    dir /a:-d /o:n /b "%%A"
    echo(
    echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\)>>"%dir%\!name! index.txt"
)

This will give you a separate text file with the name of the folder (%%~nA) in the title of each one. If you're lazy like me and want to delete the individualized index files prior to running this the nth number of times, you can throw an if exist "%dir%\!name! index.txt" del /f /q "%dir%\!name! index.txt" immediately after setting the name variable in the loop - which I might as well put in a block:

for /d /r "%dir%" %%A in (*) do (
    set "name=%%~nA"
    if exist "%dir%\!name! index.txt" del /f /q "%dir%\!name! index.txt"
    (echo(

Let me know if you were looking for something totally different, or if this was helpful at all.

References: dir and for.

  • I indeed was looking for a separate index file for each subfolder; I figured it had to do something with enabling delayed expansion, but I wasn't sure how to write the rest of the script. I always get confused when it comes to using the excIamation marks. I know batch is old, but I'm always amazed at how far you guys out there can push it. I'll be using your solution as a study guide. I thank you very much. – Jase Jul 3 at 23:54
  • you use the ! in delayed expansion when a variable's value will change between loop iterations; so in my example, we set "name=%%~nA", since our %%A parameter changes, name changes with it and we use !name! to indicate each separate text file we're outputting to. what else were you looking for it to do if not the code block that's already there? – mael' Jul 4 at 2:46

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.