Discussion Locked

Batch Script - Create Directory, User Prompted

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have the following batch script which creates a new directory after asking user to enter the desired folder name:

@echo off
set /p "folder=Enter the folder name to be created: "
mkdir "%folder%\A-"
mkdir "%folder%\Investigation"
mkdir "%folder%\Photos"

However, I'd like multiple user prompts to force the user in the naming convention of the folder for consistency. Example, instead of only one question asking the folder name: "2024-08-22" and then the folder is created 2024-08-22. We would prefer multiple prompts such as:
"Please enter 4 digit year" user would only enter 2024
"Please enter 2 digit month" user would only enter 08
"Please enter 2 digit day" user would enter 22

The folder would then be named appropriately 2024-08-22
Suggestions?

5 replies

by kees_b

Instead of asking for %folder% ask for %year%, %month% and %day%
Then in stead of creating %folder% create %year%-%month%-%day%

But it would be preferable to write a vb-script, so you can check that they don't enter July or 7 or 13, only valid month numbers. And the folder shouldn't yet exist.

by wheeler.bradley edited

Thanks for your quick response. Unfortunately, I'm a script wanna-be and just learning. Are you able to give me an example how that would look in a script?

by kees_b

I see vbscript will be phased out, so that's not a very good idea.

For the moment, in your new batch file, use prompt like "Enter month (2 digits):" to prevent people answering "July", which is a perfect month, but not good for the purpose.

The future for system management tools like this seems to be PowerShell. See https://learn.microsoft.com/en-us/powershell/scripting/samples/sample-scripts-for-administration?view=powershell-7.4 .
Assuming it's for work, ask your boss to pay a PowerShell course for you.

by basika0911 edited

You can modify your batch script to include separate prompts for year, month, and day, then combine them to create the folder name. Here's an updated version:

batch
@echo off
set /p "year=Please enter 4 digit year: "
set /p "month=Please enter 2 digit month: "
set /p "day=Please enter 2 digit day: "
set "folder=%year%-%month%-%day%"

mkdir "%folder%\A-"
mkdir "%folder%\Investigation"
mkdir "%folder%\Photos"

echo Folder created: %folder%
pause
This will prompt the user to enter each part separately and then create the folder with the correct format (e.g., 2024-08-22).

This discussion is closed to new replies.