DOS has always been handy for creating the type of batch files that XP could never handle well so here are a few scripts you might find useful.
List all files in a directory and subdirectory
Simple and straightforward this one, a script to list all files in a directory and all corresponding subdirectories. Copy the code into Notepad or other text editor and save it as get_files.bat:
for /r %%a in (*) do echo %%a >> get_files.txt
The for command here is used to run a further command for each file or folder in the set. The addition of the /r switch is to tell the script to recurse all directories and subdirectories from where the script is run.
The variable name %%a is used to hold the current folder being traversed.
The in (*) bit is the set of files to match, which in this case is everything denoted by using the * wildcard character.
The do echo %%a script executes the echo%%a command that simply displays the current file being read in the current folder name which is represented by the variable.
The final code >> get_files.txt directs and appends all the output to a text file called get_files.txt which is necessary because the script is repeated several times as it recurses each folder and so we want to collate all the names into a single file.
List all folders and sub-folders in a directory and subdirectory
A slight variation on the first script, this second one lists all folders and sub-folders in a directory and subsequent subdirectories. Copy the code into Notepad and save it as get_dirs.bat:
for /d %%a in (*) do dir /ad /on /s /b "%%a" >> get_dirs.txt
Let’s go through the code. The for command here is used to run a further command for each file or folder in the set. The addition of the /d switch is to tell the script to match against directory names only.
As before, the variable name %%a is used to hold the current folder being traversed and in (*) is the set of files to match, which again is everything denoted by using the * wildcard character.
The do dir /ad /on /s /b "%%a" bit executes the dir command on the current folder (denoted by "%%a") with a set of switches to produce the required output.
The /ad switch is to ensure that only directories and subdirectories are read since the purpose of the script is to list all folders only; the /on switch is to sort the directories in order of name; the /s switch is to ensure the dir command recurses all subdirectories and the /b switch produces the output in an easy-to-read format with no summary or header information.
A further change to this script is that the current folder variable %%a is enclosed by speech marks ". This is important because otherwise, any folder names that have spaces in them will not be read and a file not found message is returned.
The final code >> get_dirs.txt directs and appends all the output to a text file called get_dirs.txt which again is the important bit because the script is repeated several times for each folder and so we want to collate all the names into a single file.
List all files and folders in a directory and subdirectory to one level only
Here’s a specific variation on the above two scripts. Suppose I have a mass of directories and files archived from various backups and installations and need a list of all files and directories in a folder. Sounds easy enough but the catch is that I only want the script to traverse one level down in a directory since everything is archived by date.
So for example, my directory structure looks like this:
c:\backup\110201\… (a bunch of other directories and subdirectories under 110201)
c:\backup\110407\… (a bunch of other directories and subdirectories under 110407)
My script needs to recursively traverse all folders in c:\backup and provide me with a list of all files and subdirectories therein without drilling down into each and every single sub-directory.
Here’s the code of the batch file which we’ll call get_list.bat:
for /d %%a in (*.*) do dir /on "%%a" >> get_list.txt
Running get_list.bat in the c:\backup folder will then result in something like the following:
… and so on
One last point to bear in mind with all of the above scripts is that running the batch file multiple times will append the results onto the same text file.
Now go forth and script eager young script cadets!