user input being passed to robocopy

Answered user input being passed to robocopy

  • Tuesday, February 12, 2013 1:14 AM
     
     

    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

All Replies

  • Tuesday, February 12, 2013 5:37 AM
     
     

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

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


    ¯\_(ツ)_/¯


  • Tuesday, February 12, 2013 1:39 PM
    Moderator
     
     Proposed

    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


  • Wednesday, February 13, 2013 12:34 AM
     
     

    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:45 AM
    Moderator
     
      Has Code

    That's because your xcopy command needs to say:

    %DRV%:\test\

    not

    %%D:\test\

    Bill

  • Wednesday, February 13, 2013 1:25 PM
    Moderator
     
     Answered

    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
    •