Skip to content

Instantly share code, notes, and snippets.

@krowe
Created October 19, 2014 01:36
Show Gist options
  • Save krowe/df5552a818b1644258e6 to your computer and use it in GitHub Desktop.
Save krowe/df5552a818b1644258e6 to your computer and use it in GitHub Desktop.
A batch file for creating numbered directories. Such as: dir1, dir2, dir3, dir4
@echo off
SETLOCAL
:: Help is a special type of flag which is also
:: the default which has many ways to trigger.
IF "%1"=="" ( SET hflag_help=1 ) ELSE ( SET hflag_help=0)
IF "%1"=="/?" ( SET hflag_help=1 & SHIFT )
IF "%1"=="-?" ( SET hflag_help=1 & SHIFT )
IF "%1"=="--help" ( SET hflag_help=1 & SHIFT )
:: The other flags are all similar.
IF "%1"=="-f" ( SET hflag_force=1 & SHIFT ) ELSE ( SET hflag_force=0)
IF "%1"=="--force" ( SET hflag_force=1 & SHIFT )
IF "%1"=="-r" ( SET flag_remove=1 & SHIFT ) ELSE ( SET flag_remove=0)
IF "%1"=="--remove" ( SET hflag_force=1 & SHIFT )
:: The other parameters all have default values.
IF "%1"=="" ( SET param_prefix=dir) ELSE ( SET param_prefix=%1)
IF "%2"=="" ( SET param_stop=10) ELSE ( SET param_stop=%2)
IF "%3"=="" ( SET param_start=1) ELSE ( SET param_start=%3)
IF "%4"=="" ( SET param_step=1) ELSE ( SET param_step=%4)
:: An annoyingly verbose way to check that the parameters are numbers
IF %hflag_help% EQU 0 (
SET "TypeErr="&for /f "delims=0123456789" %%i in ("%param_stop%") DO ( SET "TypeErr=%%i" )
IF DEFINED TypeErr (
ECHO.
ECHO.ERROR: Stop must be a number.
ECHO.
SET hflag_help=1
)
SET "TypeErr="&for /f "delims=0123456789" %%i in ("%param_start%") DO ( SET "TypeErr=%%i" )
IF DEFINED TypeErr (
ECHO.
ECHO.ERROR: Start must be a number.
ECHO.
SET hflag_help=1
)
SET "TypeErr="&for /f "delims=0123456789" %%i in ("%param_step%") DO ( SET "TypeErr=%%i" )
IF DEFINED TypeErr (
ECHO.
ECHO.ERROR: Step must be a number.
ECHO.
SET hflag_help=1
)
)
:: Display the help screen
IF %hflag_help% EQU 1 (
ECHO.-------------------------
ECHO. MKDIR_RANGE.BAT
ECHO.-------------------------
ECHO.
ECHO.This script creates a set of directories with an ordered numeric suffix.
ECHO.This script may also be used to remove directories using the same patterns.
ECHO.
ECHO.Useage: MKDIRRANGE.BAT [--help] [-f] [-r] [prefix] [end] [start] [step]
ECHO.
ECHO.Flags (order is important^):
ECHO. --help: Show this message and exit.
ECHO. -f, --force: Do not show extra information or ask to continue.
ECHO. -r, --remove: Remove directories instead of creating them.
ECHO.
ECHO.Parameters:
ECHO. prefix: The base folder name [default: dir].
ECHO. end: The last numbered directory [default: 10].
ECHO. start: The first numbered directory [default: 1].
ECHO. step: The amount by which to increment the numbered
ECHO. directory value each iteration [default: 1].
GOTO ENDBAT
)
ECHO.
:: Output the verbose information
IF "%hflag_force%"=="0" (
ECHO.--------------------------------
SET flag_
SET param_
ECHO.--------------------------------
ECHO.
FOR /L %%G IN (%param_start%,%param_step%,%param_stop%) DO (
IF "%flag_remove%" == "0" (
ECHO. CREATE: %param_prefix%%%G
) ELSE (
ECHO. REMOVE: %param_prefix%%%G
)
)
ECHO.
ECHO.
CHOICE /C YN /M "Perform this operation"
IF ERRORLEVEL 2 (
ECHO.User quit.
GOTO ENDBAT
)
)
:: Do the actual work
FOR /L %%G IN (%param_start%,%param_step%,%param_stop%) DO (
IF "%flag_remove%" == "0" (
mkdir %param_prefix%%%G
) ELSE (
rmdir %param_prefix%%%G
)
)
:ENDBAT
ENDLOCAL
EXIT /B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment