0

My problem is, that the .bat file does not copy the existing file fileA.mod when run for the first time. But when I run the .bat file again, it copies the file.

Interesting is, that when the file does not include the if conditions, the copy command works well for the first time.

@echo off

:: Run command: SO_script.bat DEV PRE 5617295

:: DEV, TEST or PROD
set TypeOfTask=%1
:: PRE, INTER or POST
set Process=%2
:: Identification number of investigated task
set NoOfTask=%3


if %TypeOfTask%==DEV (

set source=C:\ISPPT\TASK\%NoOfTask%
mkdir C:\AutomaticTests\DEV\%NoOfTask%
set destination=C:\AutomaticTests\DEV\%NoOfTask%

if %Process%==PRE (

copy %source%\fileA.mod %destination%

)
)
roubalikm
  • 23
  • 3
  • 4
    Possible duplicate of [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – Compo Mar 26 '19 at 10:53
  • you need to `enabledelayedexpansion` however, you could get away with it if you use `goto` or `call` statements and not have everything inside of the code blocks. – Gerhard Mar 26 '19 at 11:37

1 Answers1

0

This question is a duplicate please do not mark this answer as accepted.


I have posted it to show you an example of using delayed expansion and to offer possible alternatives.

Additionally it shows other best practices which you should use, such as indentation, proper commenting, and the recommended syntax for setting and comparing variables and strings.

@Echo Off
Rem Run command: SO_script.bat DEV PRE 5617295

Rem DEV, TEST or PROD
Set "TypeOfTask=%~1"

Rem PRE, INTER or POST
Set "Process=%~2"

Rem Identification number of investigated task
Set "NoOfTask=%~3"

If "%TypeOfTask%"=="DEV" (
    Set "source=C:\ISPPT\TASK\%NoOfTask%"
    Set "destination=C:\AutomaticTests\DEV\%NoOfTask%"
    SetLocal EnableDelayedExpansion
    MD "!destination!" 2>Nul
    If "%Process%"=="PRE" (
        Copy /Y "!source!\fileA.mod" "!destination!">Nul
    )
    EndLocal
)

I would also suggest that you add some sort of verification to the script to ensure that it receives all of the required input parameters, in the correct order, and with values matching the acceptable data.


If you're only using the variable names for the purposes of what's shown in your script, you could of course not set any of those variables at all:

@Echo Off
Rem Run command: SO_script.bat DEV PRE 5617295

If "%~1"=="DEV" (
    MD "C:\AutomaticTests\DEV\%~1" 2>Nul
    If "%~2"=="PRE" (
        Copy /Y "C:\ISPPT\TASK\%~1\fileA.mod" "C:\AutomaticTests\DEV\%~1">Nul
    )
)

Alternatively you could set the variables before the parenthesised block:

@Echo Off
Rem Run command: SO_script.bat DEV PRE 5617295

Rem DEV, TEST or PROD
Set "TypeOfTask=%~1"

Rem PRE, INTER or POST
Set "Process=%~2"

Rem Identification number of investigated task
Set "NoOfTask=%~3"

Rem Setting source and destination variables
Set "source=C:\ISPPT\TASK\%NoOfTask%"
Set "destination=C:\AutomaticTests\%TypeOfTask%\%NoOfTask%"

If "%TypeOfTask%"=="DEV" (
    MD "%destination%" 2>Nul
    If "%Process%"=="PRE" (
        Copy /Y "%source%\fileA.mod" "%destination%">Nul
    )
)
Compo
  • 36,585
  • 5
  • 27
  • 39