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. echo +++ Backing Up Data +++ echo. echo. %backupcmd% "Directory*.*" "%backupdir%Data" timeout /t 2 cls echo Backup Complete echo. echo. timeout /t 2 cls echo. echo +++ Now taking hidden and system attributes off of folders +++ echo. echo. echo. attrib -s -h "DestinationData" echo. echo. 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.
%backupcmd% "%1*.*" "%backupdir%Data"
2) Use the Prompt function (
Set to prompt the user for info to populate the variable with:
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
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 http://ss64.com/nt/set.html
@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 goto:eof
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.