Batch File Magic: Effortlessly Create Sequential Folders

Batch File Magic: Effortlessly Create Sequential Folders

In the world of computing, automation is key to streamlining repetitive tasks and boosting productivity. One such task that can quickly become tedious is creating multiple folders with sequential naming conventions. Fear not, for we have a nifty batch file script that will handle this task with ease, saving you precious time and effort.

@echo off
setlocal enabledelayedexpansion

set "name=Backup"
set "start=1"
set "end=10"

for /l %%i in (!start!, 1, !end!) do (
    set "folder_name=!name!%%i"
    if not exist "!folder_name!" (
        mkdir "!folder_name!"
        echo Created folder: !folder_name!
    ) else (
        echo Folder !folder_name! already exists.
    )
)

endlocal

This batch file script is designed to create a series of folders with a prefix followed by a sequential number. In the provided example, it will create 10 folders named “Backup1” through “Backup10”. However, you can easily modify the script to suit your specific needs by changing the values of the name, start, and end variables.

Here’s a breakdown of what the script does:

  1. The script starts by suppressing the echoing of commands to the console with @echo off.
  2. setlocal enabledelayedexpansion enables delayed expansion of environment variables, which is necessary for the loop to work correctly.
  3. The name, start, and end variables are set to the desired values.
  4. The for loop iterates from the start value to the end value, incrementing by 1 with each iteration.
  5. Inside the loop, the folder_name variable is constructed by concatenating the name value with the current loop counter.
  6. An if statement checks if the folder with the constructed name already exists. If it doesn’t, the mkdir command creates the folder, and a message is printed to the console confirming the creation.
  7. If the folder already exists, a message is printed to the console indicating that the folder already exists.
  8. Finally, endlocal restores the previous environment variable settings.

This script can be incredibly useful in various scenarios, such as creating backup folders for different dates or versions, organizing files based on categories or projects, or any situation where you need to create a series of folders with a consistent naming pattern.

Now, let’s take a look at the Linux/Mac and Python versions of the script:

Linux/Mac Bash Script

#!/bin/bash

name="Backup"
start=1
end=10

for ((i=$start; i<=$end; i++))
do
    folder_name="$name$i"
    if [ ! -d "$folder_name" ]; then
        mkdir "$folder_name"
        echo "Created folder: $folder_name"
    else
        echo "Folder $folder_name already exists."
    fi
done

Python Script

import os

name = "Backup"
start = 1
end = 10

for i in range(start, end + 1):
    folder_name = f"{name}{i}"
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
        print(f"Created folder: {folder_name}")
    else:
        print(f"Folder {folder_name} already exists.")

These versions follow the same logic as the batch file script but adapt the syntax to the respective languages and platforms.

Whether you’re a Windows user relying on batch files or a Linux/Mac user working with Bash scripts, or a Python developer, this simple yet powerful script can save you valuable time and effort when dealing with the creation of sequential folders. Give it a try and let the automation do the work for you!

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate ยป