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;
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
add a comment |
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
4
Possible duplicate of Variables are not behaving as expected
– Compo
Mar 26 at 10:53
you need toenabledelayedexpansion
however, you could get away with it if you usegoto
orcall
statements and not have everything inside of the code blocks.
– Gerhard Barnard
Mar 26 at 11:37
add a comment |
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
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
batch-file copy
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 toenabledelayedexpansion
however, you could get away with it if you usegoto
orcall
statements and not have everything inside of the code blocks.
– Gerhard Barnard
Mar 26 at 11:37
add a comment |
4
Possible duplicate of Variables are not behaving as expected
– Compo
Mar 26 at 10:53
you need toenabledelayedexpansion
however, you could get away with it if you usegoto
orcall
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
add a comment |
1 Answer
1
active
oldest
votes
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
)
)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
)
)
add a comment |
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
)
)
add a comment |
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
)
)
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
)
)
edited Mar 26 at 13:25
community wiki
5 revs, 2 users 89%
Compo
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 usegoto
orcall
statements and not have everything inside of the code blocks.– Gerhard Barnard
Mar 26 at 11:37