Why isn't this for loop working? Batch

1

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?

Mark Deven

Posted 2019-01-24T23:15:31.363

Reputation: 1 101

Answers

2

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

Pimp Juice IT

Posted 2019-01-24T23:15:31.363

Reputation: 29 425

I have it enabled for the entire script at the top. – Mark Deven – 2019-01-24T23:53:38.420

Ok thanks, I’ll make the change. Any idea why the old one wasn’t working? – Mark Deven – 2019-01-24T23:59:06.810

It seems to be related to the FOR /F loop and reading from the file with the blank spaces. – Pimp Juice IT – 2019-01-25T00:00:50.450

It looks like it will, I’ll respond tomorrow when I test it. Thanks! – Mark Deven – 2019-01-25T00:20:38.300

This ended up working: 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). You should see an update for the linked file in the next week or so. – Mark Deven – 2019-01-25T14:43:16.003