Variable not being set properly in a nested for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mithril28
Posts: 7
Joined: 17 Jan 2018 16:54

Variable not being set properly in a nested for loop

#1 Post by mithril28 » 25 Jan 2018 16:53

I have searched countless posts about this topic and changed the (!Now!) code many times based on what I have read ((%Now%) doesn't work at all), but the end result of using the following code is always that the myfiledateNow variable is blank.

Code: Select all

@SETLOCAL ENABLEDELAYEDEXPANSION
set themes=CDTNEW,CFTNEW
for %%z in ("%themes:,=" "%") do (
set Now=C:\Files_Helper\%%z_Now.csv
for %%a in (!Now!) do (
  set myfiledateNow=%%~ta
)
)
When I use the following it works

Code: Select all

@SETLOCAL ENABLEDELAYEDEXPANSION
set Now=C:\Files_Helper\CDTNEW_Now.csv
for %%a in (%Now%) do (
  set myfiledateNow=%%~ta
)
I am missing something about a nested for loop. Any suggestions would be appreciated, thank you.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Variable not being set properly in a nested for loop

#2 Post by dbenham » 25 Jan 2018 17:30

Your problem is your %%z value is quoted.

So your Now variable = C:\Files_Helper\"CDTNEW"_Now.csv

Normally those internal quotes would not cause a problem. For example, if you DIR /B !NOW! you will find the file.

Your %~ta expression must also find the file so as to get the timestamp. But for some reason, the FOR variable expansion is intolerant of the internal quotes - it fails to find the file.

You can fix your code by removing the quotes with %%~z

Code: Select all

set "Now=C:\Files_Helper\%%~z_Now.csv"

Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Variable not being set properly in a nested for loop

#3 Post by Squashman » 25 Jan 2018 17:38

Not understanding why you are bothering with the string replacement. Why don't you just use the variable directly?

Code: Select all

for %%z in (%themes%) do

mithril28
Posts: 7
Joined: 17 Jan 2018 16:54

Re: Variable not being set properly in a nested for loop

#4 Post by mithril28 » 26 Jan 2018 13:15

Thank you both! Unfortunately, the variable is still blank. I thought maybe if I sent the results that might help.
Here is the updated code:

Code: Select all

@SETLOCAL ENABLEDELAYEDEXPANSION
cls

set themes=CDTNEW,CFTNEW
for %%z in (%themes%) do (
set "Now=C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora\Files_Helper\%%~z_Now.csv"

for %%a in (!Now!) do (
  set myfiledateNow=%%~ta
)
)
Here are the results:

C:\Files_Helper>set themes=CDTNEW,CFTNEW

C:\Files_Helper>for %z in (CDTNEW C
FTNEW) do (
set "Now= C:\Files_Helper\Files_Help
er\%~z_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Files_Helper>(
set "Now= C:\Files_Helper\Files_Help
er\CDTNEW_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Files_Helper>(set myfiledateNow=
)

C:\Files_Helper>(
set "Now= C:\Files_Helper\Files_Help
er\CFTNEW_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Files_Helper>(set myfiledateNow=
)
C:\Files_Helper>

I think that some of the parens would have been on the same line, but the file path I used was much longer and I replaced it for the purposes of this post.
Than you!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Variable not being set properly in a nested for loop

#5 Post by Squashman » 26 Jan 2018 13:43

Your output does not match your code example.

mithril28
Posts: 7
Joined: 17 Jan 2018 16:54

Re: Variable not being set properly in a nested for loop

#6 Post by mithril28 » 26 Jan 2018 14:43

As stated below, "I think that some of the parens would have been on the same line, but the file path I used was much longer and I replaced it for the purposes of this post."
But if it will help

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>set themes=CDTNEW,C
FTNEW

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>for %z in ("CDTNEW"
"CFTNEW") do (
set "Now=C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora\Files_Help
er\%~z_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>(
set "Now=C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora\Files_Help
er\CDTNEW_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>(set myfiledateNow=
)

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>(
set "Now=C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora\Files_Help
er\CFTNEW_Now.csv"
for %a in (!Now!) do (set myfiledateNow=%~ta )
)

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>(set myfiledateNow=
)

C:\Users\psnider\Documents\Stuff\MISCELLANEOUS\ArchiveAurora>Pause
Press any key to continue . . .

Post Reply