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

18
2014-05
  • JustinD

    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?

  • Answers
  • techie007

    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.

  • amit_g

    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.


  • Related Question

    windows - How can I loop through all sub folder of FolderA and execute o.bat in each when I execute a.bat?
  • Daniel Gartmann

    How can I loop through all sub folders of FolderA and execute o.bat in each when I execute a.bat?

    /FolderA/a.bat
    /FolderA/FolderB/o.bat
    /FolderA/FolderC/o.bat
    /FolderA/FolderD/o.bat
    

  • Related Answers
  • Windos

    This should work for you:

    for /f %%f in ('dir /ad /b') do start %%f\o.bat
    

    If you need to run the batch file from elsewhere then specify the path to foldera like:

    for /f %%f in ('dir /ad /b c:\foldera\') do start %%f\o.bat