0

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 "Destination\Data"
echo.
echo.
timeout /t 3

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

2
  • Type set /? and see how to use SET /P variable=[promptString] to obtain user input.
    – Karan
    Dec 5, 2012 at 17:59
  • also command line paramaters are referenced as %0 (for the actual program or command called) and %1-%9 for parameters passed. So you can create a new batch file with the line call backup.bat c:\source c:\destination and use %1 to reference source and %2 to reference destination. Not an answer because it doesn't handle prompting. Robocopy is a great tool as well for backup purposes and is standard on windows 7 install.
    – horatio
    Dec 5, 2012 at 18:15

2 Answers 2

1

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

and

%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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .