Batch Script to Take First & Second Part of Folder Name and Create New Folders From Them
Have you ever found yourself in a situation where you have a folder with a long name and you need to split it into two parts and create new folders based on those parts? This can be a tedious task if you have multiple folders to process manually. But fear not! With a simple 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 steps of creating a batch script that takes the first and second part of a folder name and creates new folders based on those parts. This script can be useful in various scenarios, such as organizing files, renaming folders, or creating a folder hierarchy.
Prerequisites
Before we dive into the script, let's make sure you have everything you need:
- A Windows computer
- A basic understanding of batch scripting
- A folder with the desired structure
Step 1: Open Notepad
The first step is to open Notepad or any other text editor of your choice. This is where we will write our batch script.
Step 2: Write the Script
Now, let's write the script. Copy and paste the following code into your text editor:
@echo off
setlocal enabledelayedexpansion
for /D %%A in (*) do (
set "folderName=%%A"
set "firstPart=!folderName:~0,1!"
set "secondPart=!folderName:~1,2!"
mkdir "!firstPart!"
mkdir "!secondPart!"
)
echo Folders created successfully.
pause
Let's break down the script to understand how it works:
@echo off: This line turns off the echoing of commands, which makes the script cleaner and easier to read.setlocal enabledelayedexpansion: This enables the use of delayed variable expansion, which allows us to manipulate variables within loops.for /D %%A in (*) do: This loop iterates through all the folders in the current directory.set "folderName=%%A": This assigns the current folder name to the variablefolderName.set "firstPart=!folderName:~0,1!": This extracts the first character of the folder name and assigns it to the variablefirstPart.set "secondPart=!folderName:~1,2!": This extracts the second and third characters of the folder name and assigns them to the variablesecondPart.mkdir "!firstPart!": This creates a new folder using the first part of the folder name.mkdir "!secondPart!": This creates a new folder using the second part of the folder name.echo Folders created successfully.: This displays a success message once the script has finished executing.pause: This prevents the command prompt window from closing immediately, allowing you to view the output.
Step 3: Save and Run the Script
Once you have finished writing the script, save it with a .bat extension. For example, you can save it as split_folders.bat.
Now, navigate to the folder where you want to split the folder names and double-click on the .bat file. The script will start executing, and you will see new folders created based on the first and second parts of the original folder names.
That's it! You have successfully created a batch script to split folder names and create new folders based on those parts.
Batch scripting can be a powerful tool for automating repetitive tasks. In this article, we have shown you how to create a batch script that takes the first and second part of a folder name and creates new folders based on those parts. This script can save you a lot of time and effort, especially when dealing with a large number of folders.
We hope this article has been helpful to you. If you have any questions or need further assistance, feel free to reach out to our tech support team.
References
| Source | Link |
|---|---|
| Microsoft Docs - For /D | https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles |
| SS64 - Setlocal | https://ss64.com/nt/setlocal.html |
| SS64 - Delayed Expansion | https://ss64.com/nt/delayedexpansion.html |