
HOW TO MAKE A NEW FILE IN DOS CODE
Here’s the code of the batch file which we’ll call get_list.bat:įor /d %%a in (*.*) do dir /on "%%a" > get_list.txt

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. So for example, my directory structure looks like this:Ĭ:\backup\110201\… (a bunch of other directories and subdirectories under 110201)Ĭ:\backup\110407\… (a bunch of other directories and subdirectories under 110407) 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. 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. Here’s a specific variation on the above two scripts. List all files and folders in a directory and subdirectory to one level only 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. 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 /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.Ī further change to this script is that the current folder variable %%a is enclosed by speech marks ". 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 addition of the /d switch is to tell the script to match against directory names only.Īs 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 for command here is used to run a further command for each file or folder in the set. Copy the code into Notepad and save it as get_dirs.bat:įor /d %%a in (*) do dir /ad /on /s /b "%%a" > get_dirs.txt List all folders and sub-folders in a directory and subdirectoryĪ slight variation on the first script, this second one lists all folders and sub-folders in a directory and subsequent subdirectories. 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. 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 in (*) bit is the set of files to match, which in this case is everything denoted by using the * wildcard character. The variable name %%a is used to hold the current folder being traversed.

The addition of the /r switch is to tell the script to recurse all directories and subdirectories from where the script is run. Copy the code into Notepad or other text editor and save it as get_files.bat:įor /r %%a in (*) do echo %%a > get_files.txt

Simple and straightforward this one, a script to list all files in a directory and all corresponding subdirectories. List all files in a directory and subdirectory 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.
