locked
user input being passed to robocopy RRS feed

  • Question

  • Hi,

    What I'm trying to do - My dad has an external hard drive that he keeps at the bank as a disaster recovery for his personal files.  I want to have a batch file that will ask him what the drive letter is of the DRP external hard drive, because the drive letter could change due to different things plugged in, and pass that to the robocopy commands I'm using.  The first possible drive letter is F, and thought I would give it 5 possible option in order. (ie, F, G, H, I or J.)

    Needless to say I'm not a programmer, and can do this with a set of copy commands for each input option, but think there has to be a way to use his input and only have one copy routine.

    Thanks for any help you can give.
    Pat

    Tuesday, February 12, 2013 1:14 AM

Answers

  • Bill is right about the use of the environment variable, %Drv%, rather than the FOR statement's %%D variable in your copy command.

    On the other problem related to the SET statement:  I failed to take note of the fact that jrv's PS statement contained two characters that cause problem in batch processes - the greater-than and less-than symbols.  These are used at the command prompt to indicate redirection, so using them in a simple string display requires they either be quoted or 'escaped'.  You figured out the quoting.  Here is the 'escaped' statement ...

       set /p Drv=Please enter "F,G,H,I,J" and press ^<Enter^>?


    Tom Lavedas

    • Marked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    • Unmarked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    • Marked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    Wednesday, February 13, 2013 1:25 PM

All replies

  • $drive=Read-Host 'Please enter "F,G,H,I,J" plus <enter>?'

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 12, 2013 5:37 AM
    Tuesday, February 12, 2013 5:37 AM
  • Or, if you don't want to use the PowerShell solution that jrv provided, you could use this batch file solution, instead ...

     @echo off
     :Input
      set /p Drv=Please enter "F,G,H,I,J" plus <enter>?
      if not defined Drv echo Try again & goto :Input
      for %%D in (F,G,H,I,J) do if %DRV%==%%D goto :Continue
      echo Invalid input. & goto :Input
     :Continue
    :: Your robocopy command goes here

    I also added some error handling stuff.  I set it to require input.  You can change it to just exit with no input or faulty input by changing the GOTO statements to Exit /B statements, instead.


    Tom Lavedas


    • Edited by Tom Lavedas Tuesday, February 12, 2013 1:40 PM
    • Proposed as answer by Bill_Stewart Wednesday, February 13, 2013 3:20 AM
    Tuesday, February 12, 2013 1:39 PM
  • Tom,

    I think it's close, but not quite there.  Here is a test sample of what I want it to do:

    :Input
       set /p Drv="Please enter F,G,H,I,J and press <Enter>?"
       if not defined Drv echo Try again & goto :Input
       for %%D in (F,G,H,I,J) do if %DRV%==%%D goto :Continue
       echo Invalid input. & goto :Input
      :Continue
      xcopy E:\test1\*.* %%D:\test\

    On your original suggestion, I was getting the error "The system cannot find the file specified.", so I moved the "" from around the F,G,H,I,J to the whole line and that seemed to work.

    What doesn't work is getting the answer F,G,H,I,J and getting it in the copy command.

    Thanks for your help,
    Pat

    PS, jrv, Thanks for your answer, but Tom's got to more of what I was looking for.

    Wednesday, February 13, 2013 12:34 AM
  • That's because your xcopy command needs to say:

    %DRV%:\test\

    not

    %%D:\test\

    Bill

    Wednesday, February 13, 2013 12:45 AM
  • Bill is right about the use of the environment variable, %Drv%, rather than the FOR statement's %%D variable in your copy command.

    On the other problem related to the SET statement:  I failed to take note of the fact that jrv's PS statement contained two characters that cause problem in batch processes - the greater-than and less-than symbols.  These are used at the command prompt to indicate redirection, so using them in a simple string display requires they either be quoted or 'escaped'.  You figured out the quoting.  Here is the 'escaped' statement ...

       set /p Drv=Please enter "F,G,H,I,J" and press ^<Enter^>?


    Tom Lavedas

    • Marked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    • Unmarked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    • Marked as answer by Pat3506 Thursday, February 14, 2013 11:51 PM
    Wednesday, February 13, 2013 1:25 PM