Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Seeking a Batchfile to "catalogue" drive - folder names names and their contents

Hi, does anyone have a batchfile to write a file with the contents of a drive, showing sub-folder names and their contents within them indented so that it is obvious that the files are in that folder. I don't want any info apart from folder and filenames listed.

Folder 1
    Filename d
Folder 2
    Filename g
    Filename t
    Filename h
Folder 3
    Filename k
Folder 4
   
I hope this is clear.
0
aideb
Asked:
aideb
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
knightEknightCommented:
Well, the quickest way is just to DIR the whole drive like this:

dir/s/b/a-d  D:\*.*
0
 
knightEknightCommented:
... the output is not in the exact format, but it shows you everything you want.   (change D: to your drive letter)
0
 
knightEknightCommented:
... also, I forgot to redirect to a file:

dir/s/b/a-d  D:\*.*   >  D:\filelist.txt
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
BillDLCommented:
Hi aideb

>>> "...indented so that it is obvious that the files are in that folder" <<<

The TREE command in Windows XP gives you a visual hierarchy that simulates the Windows Explorer folder/file layout, and you can redirect the screen output to a text file using this command syntax:

TREE /a /f C:\Some_Folder > C:\Some_Folder1\Output_File.txt

Example output is as shown in the code snippet below.  Perhaps this formatting may be useful to you.

Folder PATH listing for volume Local Disk
Volume serial number is CCB6-7A61
C:.
|   test.txt
|   
+---0-60-0-24_Unzipped
|   +---UPX_Compressed
|   |       Dial-a-fix.exe
|   |       secedit.exe
|   |       
|   \---UPX_Decompressed
|           Dial-a-fix.exe
|           secedit.exe
|           
+---0-60-0-24_Zipped
|       0-60-0-24_Zipped.zip
|       
\---Docs
        Dial-a-fix - Lunarsoft Wiki.url

Open in new window

0
 
BillDLCommented:
Output of the same folder contents using the the same DIR command switches as suggested by knightEknight shown below.

There is probably a batch file method to parse the output and break each line into chunks using the \ as a field delimeter starting from the right, and insert a TAB or 4 spaces instead of the preceding "chunks" which will be folder names in the path of each line.  Unfortunately I don't have time right at the moment to try and write a batch file, but I will look at this later if neither of the aboev suggestions is useful to you.
C:\UNZIP\Dial-a-Fix\0-60-0-24_Unzipped\UPX_Compressed\Dial-a-fix.exe
C:\UNZIP\Dial-a-Fix\0-60-0-24_Unzipped\UPX_Compressed\secedit.exe
C:\UNZIP\Dial-a-Fix\0-60-0-24_Unzipped\UPX_Decompressed\Dial-a-fix.exe
C:\UNZIP\Dial-a-Fix\0-60-0-24_Unzipped\UPX_Decompressed\secedit.exe
C:\UNZIP\Dial-a-Fix\0-60-0-24_Zipped\0-60-0-24_Zipped.zip
C:\UNZIP\Dial-a-Fix\Docs\Dial-a-fix - Lunarsoft Wiki.url

Open in new window

0
 
Bill PrewCommented:
Okay, I'll join the party.  Seems like TREE is the closest to what was requested originally, but hard to know for sure.

My question is around how the original poster wants subfolders to be represented?  The example showed only one level of folders, no nested folders.

Would it be like this?

Folder 1
    Filename d
Folder 2
    Filename g
    Folder 3
        Filename k
        Filename t
Folder 4
    Filename h

Or like this?

Folder 1
    Filename d
Folder 2
    Filename g
    Folder 2\Folder 3
        Filename k
        Filename t
Folder 4
    Filename h

Or something else?

~bp
0
 
t0t0Commented:
Apologies for coming in on this one late.

Please try the following (LISTFILES.BAT) code. See examples below:

   Example 1)   LISTFILES

   Example 2)   LISTFILES >output.txt

   Example 3)   LISTFILES "c:\temp"

   Example 4)   LISTFILES "c:\temp" >output.txt

   Etc...



:: ===============================
:: Listfiles.bat
:: Written by Paul Tomasi 12/2009
:: ===============================
@echo off
setlocal enabledelayedexpansion

if not "%~1"=="" (
   set folder=%~1
) else (
   set folder=%cd%
)

set pth=

for /r "%folder%" %%a in (*) do (
   if not "%%~dpa"=="!pth!" (
      title %%a
      set pth=%%~dpa
      echo %%~dpa
      echo    %%~nxa
   ) else (
      echo    %%~nxa
   )
)
0
 
t0t0Commented:
Here's a variation to the above code. The output is exactly the same as before. I have merely omitted the repetitive "echo %%~nxa"...

Apologies for coming in on this one late.

Please try the following (LISTFILES.BAT) code. See examples below:

   Example 1)   LISTFILES

   Example 2)   LISTFILES >output.txt

   Example 3)   LISTFILES "c:\temp"

   Example 4)   LISTFILES "c:\temp" >output.txt

   Etc...



:: ===============================
:: Listfiles.bat
:: Written by Paul Tomasi 12/2009
:: ===============================

@echo off
setlocal enabledelayedexpansion

if not "%~1"=="" (
   set folder=%~1
) else (
   set folder=%cd%
)

set pth=

for /r "%folder%" %%a in (*) do (
   if not "%%~dpa"=="!pth!" (
      title %%a
      set pth=%%~dpa
      echo %%~dpa
   )
   echo    %%~nxa
)




NOTE: you can remove the line:

   title %%a

to speed things up a little if you feel you do not need visual indication of progress.
0
 
t0t0Commented:
A variation of the code above is to use the DIR /S option however, it takes a while for the /S option to gather all the information before actually producing any output... Once this information has been gahtered though, output is very fast!



:: ===============================
:: Listfiles.bat
:: Written by Paul Tomasi 12/2009
:: ===============================

@echo off
setlocal enabledelayedexpansion

set pth=

for /f "tokens=*" %%a in ('dir /b /s "%~1"') do (
   if not "%%~dpa"=="!pth!" (
      title %%a
      set pth=%%~dpa
      echo %%~dpa
   )
   echo    %%~nxa
)
0
 
t0t0Commented:
The TREE command is very fast however, the output is not formatted to suit your requirements....
0
 
t0t0Commented:
BTW, in the 3 batch files above (LISTFILES.BAT) where I have included the line:

   title=%%a

if you run DOS (or CMD) in full screen mode (not windowed with a titlebar) then these lines should be removed (to increase speed) because they will have no effect on the output. This line is ONLY included so that it indicates the batch file is running (and not just 'hanging') - more noticeable when using redirction as in:

   listfiles >output.txt
.
0
 
BillDLCommented:
Hi aideb.

Do you want empty folders listed in your output text file?

The output of the excellent batch files by t0t0 yield slightly different results in this respect, in case you hadn't yet noticed.  It could be an important factor in your choosing which of the batch files best serves your needs.

t0t0's 1st and 2nd batch files given in Comment ID:26118340 and ID:26118364 do not list empty folders.

The last batch file that uses the DIR command (Comment ID: 26118406) does list empty folders.  It lists them by name in amongst the alphabetically sorted list of any loose files that exist in the given parent folder (same as it does for populated folders), but does not then list them as the full path, which would be the case for sub-folders that do contain files.  This could make it more difficult to differentiate between folder names and files with no extension if you ever have such files.

Adding the /a-d switch (don't list directory names) to the ('dir /b /s "%~1"') portion of the FOR command, ie.
('dir /a-d /b /s "%~1"')
will not list the names of empty sub-folders in amongst the file listing for each master folder.

You will obviously never encounter this if you never have empty folders anywhere in the directories being listed, or perhaps you may even prefer not to have empty folders listed, but I thought I would mention it just in case this is an important factor.

Bill
0
 
aidebAuthor Commented:
Thanks for all the guidance. Invaluable as ever!

Cheers
0
 
t0t0Commented:
Thank you....
0
 
BillDLCommented:
Thank you aideb.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now