Skip to content
Fix Code Error

How to use if – else structure in a batch file?

March 13, 2021 by Code Error
Posted By: Anonymous

I have a question about if – else structure in a batch file. Each command runs individually, but I couldn’t use “if – else” blocks safely so these parts of my programme doesn’t work. How can I do make these parts run? Thank you.

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )
ELSE IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

ELSE IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )
ELSE IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )

Solution

Your syntax is incorrect. You can’t use ELSE IF. It appears that you don’t really need it anyway. Simply use multiple IF statements:

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )

IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )

IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )

Great batch file reference: http://ss64.com/nt/if.html

Answered By: Anonymous

Related Articles

  • How to calculate means for multiple groups of rows in one…
  • Default FirebaseApp is not initialized
  • How many times the loss function is triggered from .fit()…
  • Edit a specific Line of a Text File in C#
  • Problems Installing CRA & NextJS from NPM (Error:…
  • What is going wrong when Visual Studio tells me "xcopy…
  • What is the copy-and-swap idiom?
  • Difference between xcopy and robocopy
  • What is your most productive shortcut with Vim?
  • Node.js Best Practice Exception Handling

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to use *ngIf else?

Next Post:

How do I put a variable inside a string?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error