Creating new folders in Windows can be a time-consuming task, especially when you have to create multiple folders with similar names. However, with the help of a batch script, you can automate this process and save yourself a lot of time and effort.
In this article, we will guide you through the process of creating a batch script that will create new folders based on the first and second parts of the folder name. This script will be useful when you have a list of folder names with a common structure and you want to create new folders based on that structure.
Step 1: Create a New Batch Script
The first step is to create a new batch script file. Batch scripts are plain text files with a .bat extension. You can create a new batch script using any text editor, such as Notepad.
Open Notepad and create a new file. Save the file with a .bat extension, for example, create_folders.bat.
Step 2: Write the Batch Script
Once you have created the batch script file, you can start writing the script. Batch scripts are written using a set of commands that are executed one after the other.
Here is an example of a batch script that creates new folders based on the first and second parts of the folder name:
@echo off
setlocal enabledelayedexpansion
for /d %%a in (*) do (
set "folder=%%a"
set "part1=!folder:~0,4!"
set "part2=!folder:~5!"
md "!part1! - !part2!"
)
Let's break down the script and understand how it works:
@echo off: This command turns off the echoing of commands in the script. It makes the script cleaner and easier to read.setlocal enabledelayedexpansion: This command enables delayed variable expansion. It allows us to use variables within a loop or a block of code.for /d %%a in (*) do (: This command starts a loop that iterates over all the directories in the current folder.set "folder=%%a": This command assigns the current directory name to the variable "folder".set "part1=!folder:~0,4!": This command extracts the first four characters of the folder name and assigns it to the variable "part1".set "part2=!folder:~5!": This command extracts the characters from the sixth position onwards of the folder name and assigns it to the variable "part2".md "!part1! - !part2!": This command creates a new folder with the name "part1 - part2". For example, if the folder name is "FolderName", it will create a new folder named "Fold - erName".): This closing parenthesis marks the end of the loop.
You can modify the script to adjust the number of characters to extract from the folder name. For example, if you want to extract the first five characters, you can change the set "part1=!folder:~0,4!" line to set "part1=!folder:~0,5!".
Step 3: Run the Batch Script
Now that you have written the batch script, you can run it to create the new folders. Follow these steps to run the script:
- Open the folder where you want to create the new folders.
- Right-click on the batch script file (create_folders.bat) and select "Run as administrator". Running as an administrator ensures that the script has the necessary permissions to create folders.
- The script will start running, and you will see new folders being created based on the first and second parts of the existing folder names.
Once the script has finished running, you will have new folders with names based on the first and second parts of the original folder names.
Creating new folders based on the first and second parts of the folder name can be a tedious and time-consuming task. However, with the help of a batch script, you can automate this process and save a lot of time and effort.
In this article, we have explained how to create a batch script that creates new folders based on the first and second parts of the folder name. We have provided a sample script and explained each step in detail.
By following the steps outlined in this article, you can easily create new folders from the first and second parts of existing folder names, making your file organization tasks much more efficient.
References
| Reference | Link |
|---|---|
| Batch Scripting | https://en.wikipedia.org/wiki/Batch_file |
| MD command | https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/md |
| Delayed Expansion | https://ss64.com/nt/delayedexpansion.html |