Dan M
Dan M

Reputation: 113

How do I stop my script overwriting?

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

Upvotes: 0

Views: 578

Answers (2)

michael_heath
michael_heath

Reputation: 5372

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

Upvotes: 2

Magoo
Magoo

Reputation: 79982

  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.

Upvotes: 2

Related Questions