0

Below is a batch script that I have put together which copies the content of another folder and pastes it into a new folder that it creates called IC_Transfer.

@echo off
@break off
@title IC Transfer

setlocal EnableDelayedExpansion

if not exist "C:\Temp\IC_Transfer" (
    md "C:\Temp\IC_Transfer"
    ROBOCOPY /-y "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir 
    if "!errorlevel!" EQU "0" (
        echo Transfer Successful! ) 
        ) else (
        if exist "C:\Temp\IC_Transfer" (
        echo Error Transferring File 
        echo File Already Exists
        :Choice
        set /P c=Would You Like To Overwrite[Y/N]?
            if /I "%c%" EQU "Y" goto Overwrite
            if /I "%c%" EQU "N" goto Stop
        ) )
:Overwrite
    md "C:\Temp\IC_Transfer"
    ROBOCOPY "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir 
    if "!errorlevel!" EQU "0" (
    echo Transfer Successful )
    goto End

:Stop
    echo Transfer Cancelled
    goto End

:End 
    pause
    exit

pause
exit

The make directory and robocopy functions work as they should by purging the directory, re-creates it and pastes the contents. What I cannot get working is the choice command.

Regardless of whether I choose Y or N it overwrites the file contents.

I'm new to batch scripts so any help would be appreciated

Dan M
  • 103
  • 1
  • 9
  • 2
    you know, there *is* a [choicd](https://ss64.com/nt/choice.html) command? (would even be better here than your `set /p` approach). Don't use `:labels` inside a code block. Add `goto :eof` a line before `:Overwrite` to let the script stop there instead of running through. And of course [delayed expansion](https://stackoverflow.com/a/30284028/2152082) for your `c` variable. – Stephan Jun 15 '18 at 09:46

2 Answers2

2
@echo off
@break off
@title IC Transfer

setlocal

if not exist "C:\Temp\IC_Transfer" (
    md "C:\Temp\IC_Transfer"
    ROBOCOPY /-y "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
    if not errorlevel 1 (
        echo Transfer Successful!
    ) else (
        echo Error Transferring File
        echo File Already Exists
        call :Choice
    )
)

exit /b

:Choice
    setlocal
    set "c="
    set /P "c=Would You Like To Overwrite[Y/N]? "
    if /I "%c%" == "Y" (
        call :Overwrite
    ) else if /I "%c%" == "N" (
        call :Stop
    ) else call :Stop
exit /b

:Overwrite
    ROBOCOPY "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
    if not errorlevel 1 echo Transfer Successful
    pause
exit /b

:Stop
    echo Transfer Cancelled
    pause
exit /b

To fix the issue, labels inside parenthese code blocks is a bad idea and prone to error. So call the label instead.

I replaced the goto's with calls as :Choice will work with a call. This makes :End obsolete.

You used delayed expansion for errorlevel, though you could just use i.e. if not errorlevel 1 to check if integer value is less than 1. Delayed expansion has been removed.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
2
  1. Labels within code blocks (parenthesised series of commands) cause problems (:choice) and are better regarded as illegal.

  2. You have invoked delayedexpansion and recognised that errorlevel may change with the code block. Your set /p changes the variable c, but you are using the parse-time value of c (%c%) in your if statements, not the run-time value (!c!)

  3. What happens should the user enter not-YN? Hint: See the choice command - choice /? from the prompt.

Since c is not set at the start of the code, "%c%" will evaluate to "" which is neither "Y" nor "N" so the ifs fail and the code will then proceed to the next statement (:overwrite)

Note that md will create a directory or generate an error message if it does not exist, hence

md directoryname 2>nul

should create the directory and suppress error messages (like 'directory already exists') obviating the test-for-existence gating the md.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you for your constructive criticism. After reading in to what you said and another contributors answer I've got it working and understand why. – Dan M Jun 15 '18 at 10:21