Why isn't this for loop working? Batch
I have a for loop in this script: https://github.com/ITCMD/Explorer--/blob/master/Explorer--.bat
That looks like this: (:ModificationID
line 477
, called from lines 308
and 329
in :OtherVersions
)
set _ForString=%~1
set _ForString=!_ForString:=\!
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=1,2* delims==" %%a in (out.temp) do (set %~2=%%b & echo hello)
where %~1 is equal to a file path. It outputs fine into out.temp as:
(2 blank lines)
LastModified=20181019082634.596899-240
(3 blank lines)
But the for loop never runs. It never even runs the echo command. Why?
command-line batch wmic
add a comment |
I have a for loop in this script: https://github.com/ITCMD/Explorer--/blob/master/Explorer--.bat
That looks like this: (:ModificationID
line 477
, called from lines 308
and 329
in :OtherVersions
)
set _ForString=%~1
set _ForString=!_ForString:=\!
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=1,2* delims==" %%a in (out.temp) do (set %~2=%%b & echo hello)
where %~1 is equal to a file path. It outputs fine into out.temp as:
(2 blank lines)
LastModified=20181019082634.596899-240
(3 blank lines)
But the for loop never runs. It never even runs the echo command. Why?
command-line batch wmic
add a comment |
I have a for loop in this script: https://github.com/ITCMD/Explorer--/blob/master/Explorer--.bat
That looks like this: (:ModificationID
line 477
, called from lines 308
and 329
in :OtherVersions
)
set _ForString=%~1
set _ForString=!_ForString:=\!
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=1,2* delims==" %%a in (out.temp) do (set %~2=%%b & echo hello)
where %~1 is equal to a file path. It outputs fine into out.temp as:
(2 blank lines)
LastModified=20181019082634.596899-240
(3 blank lines)
But the for loop never runs. It never even runs the echo command. Why?
command-line batch wmic
I have a for loop in this script: https://github.com/ITCMD/Explorer--/blob/master/Explorer--.bat
That looks like this: (:ModificationID
line 477
, called from lines 308
and 329
in :OtherVersions
)
set _ForString=%~1
set _ForString=!_ForString:=\!
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=1,2* delims==" %%a in (out.temp) do (set %~2=%%b & echo hello)
where %~1 is equal to a file path. It outputs fine into out.temp as:
(2 blank lines)
LastModified=20181019082634.596899-240
(3 blank lines)
But the for loop never runs. It never even runs the echo command. Why?
command-line batch wmic
command-line batch wmic
asked Jan 24 at 23:15
Mark DevenMark Deven
589322
589322
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The way I was able to get it to work per my understanding was to use the Type
command and then pipe the file over to the Find
command and tell it to only show lines containing an =
symbol, so that essentially parsed out the trailing and leading blank line around the value of interest file content wise.
I'm not certain if it was the way the tokens
and delims
were set per your script snippet method or if the FOR /F
loop really had an issue with the blank lines when iterated or if the trailing blank lines did it but those are the things that seem logical to me. I'm sure there are a few ways to handle this too.
Script (Workaround Solution)
setlocal enabledelayedexpansion
set "_ForString=%~1"
set "_ForString=!_ForString:=\!"
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=2 delims==" %%a in ('type out.temp ^| find "="') do (set %~2=%%a)
Note: I tested with ('find "=" out.temp ^| find "="')
and it seems to do the job too.
Further Resources
- Type
- Find
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to theFOR /F
loop and reading from the file with the blank spaces.
– Pimp Juice IT
Jan 25 at 0:00
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
|
show 1 more comment
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f1398126%2fwhy-isnt-this-for-loop-working-batch%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
The way I was able to get it to work per my understanding was to use the Type
command and then pipe the file over to the Find
command and tell it to only show lines containing an =
symbol, so that essentially parsed out the trailing and leading blank line around the value of interest file content wise.
I'm not certain if it was the way the tokens
and delims
were set per your script snippet method or if the FOR /F
loop really had an issue with the blank lines when iterated or if the trailing blank lines did it but those are the things that seem logical to me. I'm sure there are a few ways to handle this too.
Script (Workaround Solution)
setlocal enabledelayedexpansion
set "_ForString=%~1"
set "_ForString=!_ForString:=\!"
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=2 delims==" %%a in ('type out.temp ^| find "="') do (set %~2=%%a)
Note: I tested with ('find "=" out.temp ^| find "="')
and it seems to do the job too.
Further Resources
- Type
- Find
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to theFOR /F
loop and reading from the file with the blank spaces.
– Pimp Juice IT
Jan 25 at 0:00
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
|
show 1 more comment
The way I was able to get it to work per my understanding was to use the Type
command and then pipe the file over to the Find
command and tell it to only show lines containing an =
symbol, so that essentially parsed out the trailing and leading blank line around the value of interest file content wise.
I'm not certain if it was the way the tokens
and delims
were set per your script snippet method or if the FOR /F
loop really had an issue with the blank lines when iterated or if the trailing blank lines did it but those are the things that seem logical to me. I'm sure there are a few ways to handle this too.
Script (Workaround Solution)
setlocal enabledelayedexpansion
set "_ForString=%~1"
set "_ForString=!_ForString:=\!"
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=2 delims==" %%a in ('type out.temp ^| find "="') do (set %~2=%%a)
Note: I tested with ('find "=" out.temp ^| find "="')
and it seems to do the job too.
Further Resources
- Type
- Find
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to theFOR /F
loop and reading from the file with the blank spaces.
– Pimp Juice IT
Jan 25 at 0:00
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
|
show 1 more comment
The way I was able to get it to work per my understanding was to use the Type
command and then pipe the file over to the Find
command and tell it to only show lines containing an =
symbol, so that essentially parsed out the trailing and leading blank line around the value of interest file content wise.
I'm not certain if it was the way the tokens
and delims
were set per your script snippet method or if the FOR /F
loop really had an issue with the blank lines when iterated or if the trailing blank lines did it but those are the things that seem logical to me. I'm sure there are a few ways to handle this too.
Script (Workaround Solution)
setlocal enabledelayedexpansion
set "_ForString=%~1"
set "_ForString=!_ForString:=\!"
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=2 delims==" %%a in ('type out.temp ^| find "="') do (set %~2=%%a)
Note: I tested with ('find "=" out.temp ^| find "="')
and it seems to do the job too.
Further Resources
- Type
- Find
The way I was able to get it to work per my understanding was to use the Type
command and then pipe the file over to the Find
command and tell it to only show lines containing an =
symbol, so that essentially parsed out the trailing and leading blank line around the value of interest file content wise.
I'm not certain if it was the way the tokens
and delims
were set per your script snippet method or if the FOR /F
loop really had an issue with the blank lines when iterated or if the trailing blank lines did it but those are the things that seem logical to me. I'm sure there are a few ways to handle this too.
Script (Workaround Solution)
setlocal enabledelayedexpansion
set "_ForString=%~1"
set "_ForString=!_ForString:=\!"
wmic datafile where name="!_ForString!" get LastModified /format:list>out.temp
For /f "tokens=2 delims==" %%a in ('type out.temp ^| find "="') do (set %~2=%%a)
Note: I tested with ('find "=" out.temp ^| find "="')
and it seems to do the job too.
Further Resources
- Type
- Find
edited Jan 26 at 0:38
answered Jan 24 at 23:40
Pimp Juice ITPimp Juice IT
25k114177
25k114177
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to theFOR /F
loop and reading from the file with the blank spaces.
– Pimp Juice IT
Jan 25 at 0:00
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
|
show 1 more comment
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to theFOR /F
loop and reading from the file with the blank spaces.
– Pimp Juice IT
Jan 25 at 0:00
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
I have it enabled for the entire script at the top.
– Mark Deven
Jan 24 at 23:53
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
Ok thanks, I’ll make the change. Any idea why the old one wasn’t working?
– Mark Deven
Jan 24 at 23:59
It seems to be related to the
FOR /F
loop and reading from the file with the blank spaces.– Pimp Juice IT
Jan 25 at 0:00
It seems to be related to the
FOR /F
loop and reading from the file with the blank spaces.– Pimp Juice IT
Jan 25 at 0:00
1
1
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
Odd. Usually they ignore them. Thanks
– Mark Deven
Jan 25 at 0:01
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
It looks like it will, I’ll respond tomorrow when I test it. Thanks!
– Mark Deven
Jan 25 at 0:20
|
show 1 more comment
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1398126%2fwhy-isnt-this-for-loop-working-batch%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