Variables are not behaving as expectedRotating a log based on certain size into a date/time-stamped copyBatch File For loop not workingReturn cscript output to batchGetting message 0 was unexpected at this time batch scriptTime environment variable not working as expectedVariable is not setUse a counting variable within a FOR command in BATCHVariables not set in the batch fileBatch file variable value not workingGuessing game with only 10 tries in batchProper variable scope in windows batch scriptHow to store variables within variables in Batch?Batch command to pull variables from a text fileHow do I concatenate the value of a variable into the name of another in a batch file?Is it possible to get a variable from a string in batch?Batch file FOR loop using incrementing variableHow can i include a pipe in a variable?CMD Batch Variable Won't Save FFprobe OutputReading from a file in a for loop with batch in windowsBatch file string variable
The term for the person/group a political party aligns themselves with to appear concerned about the general public
Why does the UK have more political parties than the US?
What is the most important characteristic of New Weird as a genre?
Why does my electric oven present the option of 40A and 50A breakers?
Are there practical reasons to NOT use a stepper motor with lead screw for the X and or Y axes?
Bringing Food from Hometown for Out-of-Town Interview?
Does Peach's float negate shorthop knockback multipliers?
Select row of data if next row contains zero
Order by does not work as I expect
Can you keep a readied action even through incapacitation?
Alleged sexist comments charges presented toward me
Why use water tanks from a retired Space Shuttle?
Is there a way to save this session?
How was Apollo supposed to rendezvous in the case of a lunar abort?
Why were the Night's Watch required to be celibate?
How do I get a list of only the files (not the directories) from a package?
Strange math syntax in old basic listing
Cryptography and patents
If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
What TV show or movie did I watch on TV years ago where diseased people are exiled to a spaceship?
Modern approach to radio buttons
Estimate related to the Möbius function
Working in the USA for living expenses only; allowed on VWP?
Variables are not behaving as expected
Rotating a log based on certain size into a date/time-stamped copyBatch File For loop not workingReturn cscript output to batchGetting message 0 was unexpected at this time batch scriptTime environment variable not working as expectedVariable is not setUse a counting variable within a FOR command in BATCHVariables not set in the batch fileBatch file variable value not workingGuessing game with only 10 tries in batchProper variable scope in windows batch scriptHow to store variables within variables in Batch?Batch command to pull variables from a text fileHow do I concatenate the value of a variable into the name of another in a batch file?Is it possible to get a variable from a string in batch?Batch file FOR loop using incrementing variableHow can i include a pipe in a variable?CMD Batch Variable Won't Save FFprobe OutputReading from a file in a for loop with batch in windowsBatch file string variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working.
- The variable
i
is not getting incremented each time I do it. - Concatenation on
strc
doesn't seem to concatenate.
Here is my code:
set i=0
set "strc=concat:"
for %%f in (*.mp4) do (
set /a i+=1
set "str=intermediate%i%.ts"
set strc="%strc% %str%|"
ffmpeg -i "%%f" -c copy -bsf:v h264_mp4toannexb -f mpegts "%str%"
)
set strc="%strc:-1%"
ffmpeg -i "%strc%" -c copy -bsf:a aac_adtstoasc Output.mp4
batch-file delayedvariableexpansion
add a comment |
I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working.
- The variable
i
is not getting incremented each time I do it. - Concatenation on
strc
doesn't seem to concatenate.
Here is my code:
set i=0
set "strc=concat:"
for %%f in (*.mp4) do (
set /a i+=1
set "str=intermediate%i%.ts"
set strc="%strc% %str%|"
ffmpeg -i "%%f" -c copy -bsf:v h264_mp4toannexb -f mpegts "%str%"
)
set strc="%strc:-1%"
ffmpeg -i "%strc%" -c copy -bsf:a aac_adtstoasc Output.mp4
batch-file delayedvariableexpansion
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23
add a comment |
I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working.
- The variable
i
is not getting incremented each time I do it. - Concatenation on
strc
doesn't seem to concatenate.
Here is my code:
set i=0
set "strc=concat:"
for %%f in (*.mp4) do (
set /a i+=1
set "str=intermediate%i%.ts"
set strc="%strc% %str%|"
ffmpeg -i "%%f" -c copy -bsf:v h264_mp4toannexb -f mpegts "%str%"
)
set strc="%strc:-1%"
ffmpeg -i "%strc%" -c copy -bsf:a aac_adtstoasc Output.mp4
batch-file delayedvariableexpansion
I've been wrestling trying to get the syntax right on this batch file and I cannot figure out why some things aren't working.
- The variable
i
is not getting incremented each time I do it. - Concatenation on
strc
doesn't seem to concatenate.
Here is my code:
set i=0
set "strc=concat:"
for %%f in (*.mp4) do (
set /a i+=1
set "str=intermediate%i%.ts"
set strc="%strc% %str%|"
ffmpeg -i "%%f" -c copy -bsf:v h264_mp4toannexb -f mpegts "%str%"
)
set strc="%strc:-1%"
ffmpeg -i "%strc%" -c copy -bsf:a aac_adtstoasc Output.mp4
batch-file delayedvariableexpansion
batch-file delayedvariableexpansion
edited Jan 30 at 9:31
double-beep
3,12551632
3,12551632
asked May 17 '15 at 2:35
Eric MarcinowskiEric Marcinowski
9516
9516
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23
add a comment |
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23
add a comment |
1 Answer
1
active
oldest
votes
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (
and )
).
Delayed variables are referenced with !var!
instead of %var%
.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
With delayed expansion:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
Both methods can also be used to display array-like variables:
(This is often asked like "variable which contains another variable" or "nested variables")
Here is a collection for using such array-like variables in different situations:
With delayed expansion:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
without delayed expansion:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- In batch files
- When the cmd was started with delayed expansion enabled (cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
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%2f30282784%2fvariables-are-not-behaving-as-expected%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
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (
and )
).
Delayed variables are referenced with !var!
instead of %var%
.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
With delayed expansion:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
Both methods can also be used to display array-like variables:
(This is often asked like "variable which contains another variable" or "nested variables")
Here is a collection for using such array-like variables in different situations:
With delayed expansion:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
without delayed expansion:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- In batch files
- When the cmd was started with delayed expansion enabled (cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
add a comment |
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (
and )
).
Delayed variables are referenced with !var!
instead of %var%
.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
With delayed expansion:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
Both methods can also be used to display array-like variables:
(This is often asked like "variable which contains another variable" or "nested variables")
Here is a collection for using such array-like variables in different situations:
With delayed expansion:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
without delayed expansion:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- In batch files
- When the cmd was started with delayed expansion enabled (cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
add a comment |
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (
and )
).
Delayed variables are referenced with !var!
instead of %var%
.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
With delayed expansion:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
Both methods can also be used to display array-like variables:
(This is often asked like "variable which contains another variable" or "nested variables")
Here is a collection for using such array-like variables in different situations:
With delayed expansion:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
without delayed expansion:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- In batch files
- When the cmd was started with delayed expansion enabled (cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last).
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (
and )
).
Delayed variables are referenced with !var!
instead of %var%
.
Reason is the way, cmd
parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batch files to demonstrate:
setlocal EnableDelayedExpansion
set "var=hello"
if 1==1 (
set "var=world"
echo %var% !var!
)
setlocal EnableDelayedExpansion
for /L %%i in (1,1,5) do (
echo %random% !random!
)
Note: A line is also treated as a block:
set "var=old"
set "var=new" & echo %var%
With delayed expansion:
setlocal EnableDelayedExpansion
set "var=old"
set "var=new" & echo !var!
Delayed expansion is per default turned off at the command prompt. If you really need it, you can do:
cmd /V:ON /C "set "var=hello" & echo !var!"
Also there is a way to do the same without delayed expansion (but call
costs some time, so it's slower, but if for some reason you can't / don't want to use delayed expansion, it's an alternative):
setlocal DISabledelayedexpansion
for /L %%i in (1 1 5) do (
call echo %random% %%random%%
)
Both methods can also be used to display array-like variables:
(This is often asked like "variable which contains another variable" or "nested variables")
Here is a collection for using such array-like variables in different situations:
With delayed expansion:
setlocal ENableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
echo plain delayed: !var[%num%]!
for /L %%i in (4 1 4) do (
echo for delayed: !var[%%i]!
set a=%%i
call echo for delayed with variable: %%var[!a!]%%
)
without delayed expansion:
setlocal DISableDelayedExpansion
set "num=4"
set "var[%num%]=HELLO"
call echo plain called: %%var[%num%]%%
for /L %%i in (4 1 4) do (
call echo FOR called: %%var[%%i]%%
set a=%%i
call echo FOR called with variable: %%var[%a%]%%
)
Note: setlocal
has no effect outside of batchfiles, so delayedexpansion
works only:
- In batch files
- When the cmd was started with delayed expansion enabled (cmd /V:ON
) (by default, the cmd runs with delayed expansion disabled)
edited Mar 12 at 20:22
Gerhard Barnard
10.3k31433
10.3k31433
answered May 17 '15 at 6:29
StephanStephan
37k43457
37k43457
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
add a comment |
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
3
3
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
@NayanaAdassuriya: most people dont like to read about the technical background or even the advanced technical stuff
– Stephan
Jun 14 '15 at 6:46
add a comment |
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%2f30282784%2fvariables-are-not-behaving-as-expected%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
Possible duplicate of How to set a variable inside a loop for /F
– melpomene
Mar 24 at 11:23