Have you ever found yourself with a bunch of files that need to be organized into different folders, but the folders have similar names? Don't worry, we have a solution for you! In this article, we will guide you through a batch script that can automatically move files into corresponding folders with similar names.
Before we begin, let's first understand what a batch script is. A batch script is a file containing a series of commands that are executed in sequence. It allows you to automate repetitive tasks and save time.
To get started, open a text editor such as Notepad and create a new file. You can name it anything you like, but make sure it has the extension ".bat" to indicate that it is a batch script.
Now, let's write the code for our batch script:
@echo off
setlocal enabledelayedexpansion
rem Replace "source_folder" with the path to the folder containing your files
set "source_folder=C:\path\to\source_folder"
rem Replace "destination_folder" with the path to the folder where you want to move the files
set "destination_folder=C:\path\to\destination_folder"
for %%F in ("%source_folder%\*") do (
rem Get the filename without the extension
set "filename=%%~nF"
rem Create a subfolder with the same name as the filename
mkdir "%destination_folder%\!filename!"
rem Move the file to the corresponding subfolder
move "%%F" "%destination_folder%\!filename!"
)
echo Files have been moved successfully!
pause
Let's go through the code to understand what each part does.
The first line @echo off is used to prevent the display of each command in the batch script as it is executed. This makes the output cleaner and easier to read.
The setlocal enabledelayedexpansion command is used to enable delayed variable expansion. This allows us to use variables within loops and conditional statements.
Next, we set the paths for the source folder and the destination folder. Make sure to replace the placeholders with the actual paths to your folders.
The for %%F in ("%source_folder%\*") do loop iterates over each file in the source folder. For each file, it performs the following actions:
- Gets the filename without the extension using the
set "filename=%%~nF"command. - Creates a subfolder with the same name as the filename using the
mkdir "%destination_folder%\!filename!"command. - Moves the file to the corresponding subfolder using the
move "%%F" "%destination_folder%\!filename!"command.
Finally, the script displays a message indicating that the files have been moved successfully and waits for you to press any key to exit.
Save the batch script file and double-click on it to execute it. It will start moving the files into the corresponding folders with similar names. You can check the destination folder to see the organized files.
That's it! You have successfully created a batch script to move files into corresponding folders with similar names. This can be a handy tool for organizing your files and saving time.
| Reference | Link |
|---|---|
| Batch Scripting | https://en.wikipedia.org/wiki/Batch_file |
| mkdir command | https://ss64.com/nt/mkdir.html |
| move command | https://ss64.com/nt/move.html |