Creating subdirectories and moving files in batch (Windows)
For use when you have a single directory of many files that need to be moved into subdirectories that don't exist. This works only when the names of the files contain some element that can be used as the name of the subdirectory they will be moved into.
In this example, discrete publication identifiers are represented by the string before the final underscore and two-digit page number at the end of each filename, for example "mupi_indep_258_19801229". Because the subdirectory identifier is contained in each filename, the subdirectories can be created using the files themselves.
Create list of files
In the command prompt, navigate to the top-level directory and run the following command to create a text file listing each file, not including directories, in the folder:
dir /b /a:-d > <files.txt>
The output file can be named anything, but should be easily identifiable as a list of files in the directory.
Isolate subdirectory names
Open <files.txt> in Notepad++ and select Edit → Line Operations → Sort Lines Lexicographically Ascending to alphabetize the lines. Remove any blank lines at the top of the list and save the document.
Next, use Find/Replace (Ctrl + H) to remove file extension and page number info from each line. Use regular expressions as needed to remove filename elements in bulk.
In this example, every filename in the directory ends with an underscore, a two-digit page number, and the .png extension. With the "Regular expression" Search Mode radio at the bottom selected, the period symbol can be used to stand in for any character. That means a search for "_...png" will identify any file matching the expected pattern in the directory as a match. Leaving the "Replace with" box empty will erase all matching strings.
The goal here is for each line to contain only the name of a subdirectory that will be created. If some subdirectories are to contain several pages of files, there will be many duplicate lines at this stage.
Copy the stripped-down lines into an Excel workbook and remove duplicate rows, using the Remove Duplicates function in the Data tab. This will leave you with a simple list of subdirectories to be created.
Save this file as <directories.csv> or copy the rows back into Notepad++ and save as <directories.txt>
Create subdirectories
In the command prompt, run the following command to create a directory matching the text of each line of the CSV/text file you just created:
for /f %t in (<directories.csv>) do mkdir [path to directory]\%t
The directory will now contain all the files as well as the subdirectories they need to be moved into.
Create CSV file with files & destination subdirectory
Open <files.txt> in Notepad++ once again. Make sure the lines have been sorted alphabetically, and blank rows removed. Copy the lines into the first column of a new Excel workbook.
Return to <files.txt> and run the same Find/Replace steps used above to isolate subdirectory names. Copy the subdirectories list as a second column in the same Excel workbook.
Each row of the Excel workbook should now contain the name of each file in one column, followed by the subdirectory it needs to be moved into in the second column. Look up and down the spreadsheet to make sure this is the case, checking for rows that do not match.
Save the Excel workbook as <movefiles.csv>
Move files into subdirectories
In the command prompt, run the following command to move the files into their respective subdirectories.
for /f "tokens=1,2 delims=," %a in (<movefiles.csv>) do move [path to directory]\%a [path to directory]\%b
This will open the <movefiles.csv> spreadsheet file, and for each row copy the file named in the first column into the subdirectory named in the second column. Because the command prompt understands an (alphabetical) "order" to variables (%a and %b), the letter of the variable named at the beginning of the command will automatically be applied to the first column, and the letter after will automatically be applied to the second column.
Welcome to the University Wiki Service! Please use your IID (yourEID@eid.utexas.edu) when prompted for your email address during login or click here to enter your EID. If you are experiencing any issues loading content on pages, please try these steps to clear your browser cache.