Copy in batch-file works only at second runVariables are not behaving as expectedHow can I pass arguments to a batch file?Batch file to delete files older than N daysSplit long commands in multiple lines through Windows batch fileHow do I copy a file in Python?How can I echo a newline in a batch file?Windows batch files: .bat vs .cmd?How to run multiple .BAT files within a .BAT fileHow to sleep for five seconds in a batch file/cmdBatch File Infinitely RunningHow to copy a file in endless running batch file only if file not existing in target directory?

How important are the Author's mood and feelings for writing a story?

Demographic consequences of closed loop reincarnation

Three Subway Escalators

Why can't I hear fret buzz through the amp?

Why do space operations use "nominal" to mean "working correctly"?

Proof that every field is perfect???

How do you send money when you're not sure it's not a scam?

Why is Google approaching my VPS machine?

How to not confuse readers with simultaneous events?

When designing an adventure, how can I ensure a continuous player experience in a setting that's likely to favor TPKs?

How was Luke's prosthetic hand in Episode V filmed?

Did Hitler say this quote about homeschooling?

Does unblocking power bar outlets through short extension cords increase fire risk?

What makes MOVEQ quicker than a normal MOVE in 68000 assembly?

Do pedestrians imitate auto traffic?

Wait or be waiting?

literal `0` beeing a valid candidate for int and const string& overloads causes ambiguous call

Why didn't Doctor Strange restore Tony Stark after he used the Stones?

Could a US citizen born through "birth tourism" become President?

Is it possible to invoke "super" with less ambiguous results?

Manager asking me to eat breakfast from now on

Applying for jobs with an obvious scar

BritRail England Passes compared to return ticket for travel in England

Improving an O(N^2) function (all entities iterating over all other entities)



Copy in batch-file works only at second run


Variables are not behaving as expectedHow can I pass arguments to a batch file?Batch file to delete files older than N daysSplit long commands in multiple lines through Windows batch fileHow do I copy a file in Python?How can I echo a newline in a batch file?Windows batch files: .bat vs .cmd?How to run multiple .BAT files within a .BAT fileHow to sleep for five seconds in a batch file/cmdBatch File Infinitely RunningHow to copy a file in endless running batch file only if file not existing in target directory?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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:ISPPTTASK%NoOfTask%
mkdir C:AutomaticTestsDEV%NoOfTask%
set destination=C:AutomaticTestsDEV%NoOfTask%

if %Process%==PRE (

copy %source%fileA.mod %destination%

)
)









share|improve this question

















  • 4





    Possible duplicate of Variables are not behaving as expected

    – Compo
    Mar 26 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 Barnard
    Mar 26 at 11:37


















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:ISPPTTASK%NoOfTask%
mkdir C:AutomaticTestsDEV%NoOfTask%
set destination=C:AutomaticTestsDEV%NoOfTask%

if %Process%==PRE (

copy %source%fileA.mod %destination%

)
)









share|improve this question

















  • 4





    Possible duplicate of Variables are not behaving as expected

    – Compo
    Mar 26 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 Barnard
    Mar 26 at 11:37














0












0








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:ISPPTTASK%NoOfTask%
mkdir C:AutomaticTestsDEV%NoOfTask%
set destination=C:AutomaticTestsDEV%NoOfTask%

if %Process%==PRE (

copy %source%fileA.mod %destination%

)
)









share|improve this question














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:ISPPTTASK%NoOfTask%
mkdir C:AutomaticTestsDEV%NoOfTask%
set destination=C:AutomaticTestsDEV%NoOfTask%

if %Process%==PRE (

copy %source%fileA.mod %destination%

)
)






batch-file copy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 10:43









roubalikmroubalikm

11 bronze badge




11 bronze badge







  • 4





    Possible duplicate of Variables are not behaving as expected

    – Compo
    Mar 26 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 Barnard
    Mar 26 at 11:37













  • 4





    Possible duplicate of Variables are not behaving as expected

    – Compo
    Mar 26 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 Barnard
    Mar 26 at 11:37








4




4





Possible duplicate of Variables are not behaving as expected

– Compo
Mar 26 at 10:53





Possible duplicate of Variables are not behaving as expected

– Compo
Mar 26 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 Barnard
Mar 26 at 11:37






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 Barnard
Mar 26 at 11:37













1 Answer
1






active

oldest

votes


















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:ISPPTTASK%NoOfTask%"
Set "destination=C:AutomaticTestsDEV%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:AutomaticTestsDEV%~1" 2>Nul
If "%~2"=="PRE" (
Copy /Y "C:ISPPTTASK%~1fileA.mod" "C:AutomaticTestsDEV%~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:ISPPTTASK%NoOfTask%"
Set "destination=C:AutomaticTests%TypeOfTask%%NoOfTask%"

If "%TypeOfTask%"=="DEV" (
MD "%destination%" 2>Nul
If "%Process%"=="PRE" (
Copy /Y "%source%fileA.mod" "%destination%">Nul
)
)





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355182%2fcopy-in-batch-file-works-only-at-second-run%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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:ISPPTTASK%NoOfTask%"
    Set "destination=C:AutomaticTestsDEV%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:AutomaticTestsDEV%~1" 2>Nul
    If "%~2"=="PRE" (
    Copy /Y "C:ISPPTTASK%~1fileA.mod" "C:AutomaticTestsDEV%~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:ISPPTTASK%NoOfTask%"
    Set "destination=C:AutomaticTests%TypeOfTask%%NoOfTask%"

    If "%TypeOfTask%"=="DEV" (
    MD "%destination%" 2>Nul
    If "%Process%"=="PRE" (
    Copy /Y "%source%fileA.mod" "%destination%">Nul
    )
    )





    share|improve this answer





























      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:ISPPTTASK%NoOfTask%"
      Set "destination=C:AutomaticTestsDEV%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:AutomaticTestsDEV%~1" 2>Nul
      If "%~2"=="PRE" (
      Copy /Y "C:ISPPTTASK%~1fileA.mod" "C:AutomaticTestsDEV%~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:ISPPTTASK%NoOfTask%"
      Set "destination=C:AutomaticTests%TypeOfTask%%NoOfTask%"

      If "%TypeOfTask%"=="DEV" (
      MD "%destination%" 2>Nul
      If "%Process%"=="PRE" (
      Copy /Y "%source%fileA.mod" "%destination%">Nul
      )
      )





      share|improve this answer



























        0












        0








        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:ISPPTTASK%NoOfTask%"
        Set "destination=C:AutomaticTestsDEV%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:AutomaticTestsDEV%~1" 2>Nul
        If "%~2"=="PRE" (
        Copy /Y "C:ISPPTTASK%~1fileA.mod" "C:AutomaticTestsDEV%~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:ISPPTTASK%NoOfTask%"
        Set "destination=C:AutomaticTests%TypeOfTask%%NoOfTask%"

        If "%TypeOfTask%"=="DEV" (
        MD "%destination%" 2>Nul
        If "%Process%"=="PRE" (
        Copy /Y "%source%fileA.mod" "%destination%">Nul
        )
        )





        share|improve this answer















        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:ISPPTTASK%NoOfTask%"
        Set "destination=C:AutomaticTestsDEV%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:AutomaticTestsDEV%~1" 2>Nul
        If "%~2"=="PRE" (
        Copy /Y "C:ISPPTTASK%~1fileA.mod" "C:AutomaticTestsDEV%~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:ISPPTTASK%NoOfTask%"
        Set "destination=C:AutomaticTests%TypeOfTask%%NoOfTask%"

        If "%TypeOfTask%"=="DEV" (
        MD "%destination%" 2>Nul
        If "%Process%"=="PRE" (
        Copy /Y "%source%fileA.mod" "%destination%">Nul
        )
        )






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 26 at 13:25


























        community wiki





        5 revs, 2 users 89%
        Compo



















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355182%2fcopy-in-batch-file-works-only-at-second-run%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현