Batch Script: Resize Image, Delete Unwanted Files, and Organize Music Files
In this article, we will create a batch script to perform several tasks, including resizing images, deleting unwanted files, and organizing music files in a specified folder. The focus of this article is to provide a detailed explanation of the key concepts involved in the script, as well as its implementation.
Resizing Images
The first part of the script will resize images in a specified folder using the ImageMagick software. The following code block demonstrates how to resize images using the convert command:
@echo off
setlocal enabledelayedexpansion
set "input_folder=C:\input_folder"
set "output_folder=C:\output_folder"
set "width=800"
for /R "%input_folder%" %%A in (*.jpg, *.jpeg, *.png) do (
set "filename=%%~nA"
set "fileext=%%~xA"
set "output_file=%output_folder%\!filename!-resized!%fileext%"
convert "%%A" -resize !width!x!width! "!output_file!"
)
In the above code block, we first set the input and output folders, as well as the desired width for the resized images. We then use a for loop to iterate through all the image files in the input folder. For each file, we set the output file name and use the convert command to resize the image to the desired width.
Deleting Unwanted Files
The second part of the script will delete unwanted files from a specified folder. The following code block demonstrates how to delete files based on a specified file extension:
set "input_folder=C:\input_folder"
set "extension=.txt"
for /R "%input_folder%" %%A in (*%extension%) do (
del "%%A"
)
In the above code block, we first set the input folder and the file extension to be deleted. We then use a for loop to iterate through all the files in the input folder with the specified extension and delete them.
Organizing Music Files
The final part of the script will organize music files in a specified folder. The following code block demonstrates how to move music files into subfolders based on the artist name:
set "input_folder=C:\input_folder"
set "output_folder=C:\output_folder"
for /R "%input_folder%" %%A in (*.mp3) do (
set "filename=%%~nA"
set "artist=!filename:~0,10!"
set "output_file=!output_folder!\!artist!\!filename!.mp3"
if not exist "!output_file!\.." md "!output_file!\.."
move "%%A" "!output_file!"
)
In the above code block, we first set the input and output folders. We then use a for loop to iterate through all the music files in the input folder. For each file, we extract the artist name from the file name and set the output file name. We then check if the output folder exists and create it if it doesn't. Finally, we move the music file to the output folder.
- We have created a batch script to resize images, delete unwanted files, and organize music files in a specified folder.
- We have explained the key concepts involved in the script, including resizing images, deleting files, and organizing music files.
- We have provided code blocks for each part of the script, including resizing images, deleting unwanted files, and organizing music files.