Batch File Question

Hi,

I have a batch file that will create a set of folders for me.

The batch file lives at D:\Check\System\Utilities

I want the folders to be created in D:\Check\System\Utilities\<folder-name>\

How can I prompt when I open the batch file to enter the folder name, then these folders will be created in <folder-name>\

md folder1
md folder2
LVL 3
Computer GuyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kyodaiCommented:
Try it like this:

FOR /F "tokens=*" %%A IN ('TYPE FOLDER NAME') DO SET INPUT=%%A
ECHO You typed: "%INPUT%"
MD "%INPUT%"
sarabandeCommented:
The batch file lives at D:\Check\System\Utilities
so far I understand what you want to do.

can you make a sample of the directory tree before, tell what the batch should ask for, a sample for the user's input and the directory tree after execution.

Sara
kyodaiCommented:
AIght error in the previous code, this should work:

FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET INPUT=%%A
ECHO You typed: "%INPUT%"
MD "%INPUT%"
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

kyodaiCommented:
Other way to do it would be easier i think:

set /p pathName=Enter Folder name:%=%
MD %pathName%
Steve KnightIT ConsultancyCommented:
You can get prettier if you want using GUI... this one adjusted of mine below from my script site below save as whatever.cmd or whatever.bat, it has a bit of VBScript in it which brings up a "choose a folder" box starting in the folder you set at the top.  You can either make a new folder or choose an existing then.

http://scripts.dragon-it.co.uk/links/batch-gui-folder-mk2

@echo off
Set StartPath=D:\Check\System\Utilities\

set folder=
call :getfolder
if "%folder%"=="" exit /b
pushd "%folder%
echo Folder is %cd%

REM Put your code in here, remove the above echo and Pause if wanted.

MD folder1
MD folder2

PAUSE

exit /b

:GetFolder
(echo Set objShell = CreateObject^( "Shell.Application" ^)
echo set objFolder = objShell.BrowseForFolder^( 0, "Select a folder", ^&H10^&, "%startpath%"^)
echo if objFolder is nothing then wscript.quit
echo wscript.echo objFolder.Self.Path)> "%temp%\getpath.vbs"

for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getpath.vbs"') do set folder=%%a

exit /b

Open in new window


If you just want to prompt for a folder name then without showing:

http://scripts.dragon-it.co.uk/links/batch-gui-inputbox

@echo off
Set StartPath=D:\Check\System\Utilities\

set input=
call :inputbox "Please enter new folder name" "Utilities"
if "%input%"=="" exit /b
if exist "%startpath%\%input%" (
  echo That folder already exists
  pause
  exit /b
)

pushd "%startpath%\%input%"
echo Folder is %cd%
pause

REM Put your code in here, remove the above echo and Pause if wanted.
MD FOLDER1
MD FOLDER2

exit /b

:InputBox
set input=
set heading=%~2
set message=%~1
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set input=%%a
exit /b

Open in new window


Steve
Bill PrewIT / Software Engineering ConsultantCommented:
Here's how I might approach this.  I'd use the SET /P to prompt for a destination folder path as long as that will suffice.  It's not "pretty" but it is the easiest thing to do in a BAT.  I've added a few additional bits to the process to try and make sure all goes well.

Make sure you clear the variable first, otherwise it could have a pre-existing value
Allow them a value (in my case blank) to enter to end the script
Clean up the path entered, since some people could end it with a "\", others might not.
"Edit" the entered value to make sure it's a valid path.  The easiest way to do that here seemed to be to try and create the directory they specified and trap if errors occurred, and ext.

@echo off
setlocal

REM Clear prompt variable for SET /P below
set DestFolder=

REM Prompt for destination folder
set /p "DestFolder=Enter destination folder (blank to exit): "

REM Quit script if they didn't enter a folder
if "%DestFolder%" EQU "" exit /b

REM Make sure folder path doesn't end in a "\"
if "%DestFolder:~-1%" EQU "\" (
  set DestFolder=%DestFolder:~0-1%
)

REM If the folder doesn't exist, try to create it to make sure they specified a valid path
if not exist "%DestFolder%\" (
  md "%DestFolder%" || (
    echo *ERROR* Could not create destination folder
    pause
    exit /b
  )
)

REM Create desired folders
md "%DestFolder%\folder1"
md "%DestFolder%\folder2"

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.