How can I allow a user to choose directories to be copied?

Posted on


I’m trying to write a batch file that is basically used for copying destinations. Now, I want to make it to where other people can use it without having to go in and edit the directories and destinations. Is there a way for me to write the batch file to where it prompts, asking for the directory the user would like to copy, and to ask for the drive that the user would like to copy it to?

This is what I’ve been using for a while now.

@echo off
:: variable
set backupdir="Destination"
set backupcmd=xcopy /e /h /f /y /v /c /i /r /g /k /d
echo +++ Backing Up Data +++
%backupcmd% "Directory*.*" "%backupdir%Data"
timeout /t 2
echo Backup Complete
timeout /t 2
echo +++ Now taking hidden and system attributes off of folders +++
attrib -s -h "DestinationData"
timeout /t 3

And is there any way that I can improve this with using xcopy?


There’s a couple options:

1) Use command-line arguments.

The user would run it like backup.bat c:SourcePath d:DestinationPath

Then use %1 and %2 (etc.) in the batch file to use the provided arguments.

Something like

set backupdir=%2


%backupcmd% "%1*.*" "%backupdir%Data"

More info: How to Pass Command Line Parameters in Batch File

2) Use the Prompt function (/P) of Set to prompt the user for info to populate the variable with:

Example: SET /P variable=[promptString]

So you could do something like:

set /P backupdir="Enter Destination Path: "
set /P sourcedir="Enter Source Path: "

When it gets to those lines in the batch it will stop and prompt the user for input.

More info from set /?:

The /P switch allows you to set the value of a variable to a line of
input entered by the user. Displays the specified promptString before
reading the line of input. The promptString can be empty.

You can use set /p

An example from

@echo off
Set /P _dept=Please enter Department || Set _dept=NothingChosen
If "%_dept%"=="NothingChosen" goto :sub_error
If /i "%_dept%"=="finance" goto sub_finance
If /i "%_dept%"=="hr" goto sub_hr

Use the link above to see more options and examples. you can also do Set /? within command prompt to see the options.

Keep in mind, interactive batch files get messy really fast and you might want to look into other ways of programming this depending upon how complex your script is and who is the target audience. If you are familiar with VB, VBScript might be simpler to program and CScript can be used to run it on command line.

Leave a Reply

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