Trouble Setting Variable in For Loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Trouble Setting Variable in For Loop

#1 Post by ALbino » 27 Oct 2014 19:46

Hey all,

I mentioned in my last post that I'm still very inexperienced with this, so I'm sure I'm doing something basic wrong, but for some reason I can't get what I think is simple code to work.

In this example there are several text files in a directory with names like:

Example File--1000.txt
Another File--2500.txt
One More--3879.txt

Every filename is a different length, but they all end with a four digit number separated by "--" (i.e. file--0000.txt). My goal is to extract those 4 digits as a variable so that I can manipulate it and rename the files based on what number they end with. Here's the part of the code I'm struggling with:

Code: Select all

@echo off

for /F "tokens=1,2 delims=--" %%G in ('dir /b *.txt') do (

   echo Result: %%H
   echo.

   set DelimitedFilename=%%H

   echo DelimitedFilename: %DelimitedFilename%
   echo.

   set FourDigitNumber=%DelimitedFilename:~1,4%

   echo Four Digit Number: %FourDigitNumber%
   echo.
)



I'm able to get 0000.txt, but I can't seem to set the first variable, and so it ends up with blanks:

Code: Select all

Result: 1000.txt

DelimitedFilename:

Four Digit Number:

Result: 2500.txt

DelimitedFilename:

Four Digit Number:

Result: 3879.txt

DelimitedFilename:

Four Digit Number:


Any help would be appreciated... as dumbed down as possible :) Thanks!

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Trouble Setting Variable in For Loop

#2 Post by Yury » 27 Oct 2014 20:30

Code: Select all

@echo off

for /f "delims=" %%G in ('dir /a-d/b *.txt') do (
 set Filename=%%~nG
 for /f "tokens=2 delims=|" %%H in ('call set /p"=%%Filename:--=|%%"^<nul') do (
  echo Four Digit Number: %%H
  )
 )

pause>nul
exit /b
Last edited by Yury on 27 Oct 2014 20:48, edited 3 times in total.

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#3 Post by ALbino » 27 Oct 2014 20:38

Thanks for responding! That works, but I end up with the same problem if I try and set the variable. Modifying your code to set the variable and then echo it:

Code: Select all

for /f "delims=" %%G in ('dir /a-d/b *.txt') do (
 set Filename=%%~nG
 for /f "tokens=2 delims=|" %%H in ('call set /p"=%%Filename:--=|%%"^<nul') do (
  set FourDigitNumber=%%~H
  echo Four Digit Number: %FourDigitNumber%
  )
 )


Results in:

Code: Select all

Four Digit Number:
Four Digit Number:
Four Digit Number:


I can get the number, I just can't make the variable set.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Trouble Setting Variable in For Loop

#4 Post by Yury » 27 Oct 2014 20:52

Code: Select all

for /f "delims=" %%G in ('dir /a-d/b *.txt') do (
 set Filename=%%~nG
 for /f "tokens=2 delims=|" %%H in ('call set /p"=%%Filename:--=|%%"^<nul') do (
  set FourDigitNumber=%%H
  call echo Four Digit Number: %%FourDigitNumber%%
  )
 )

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#5 Post by ALbino » 27 Oct 2014 20:55

That works! But... why does it work and the other one doesn't? :) I want to understand what's actually happening so I can avoid problems like these in the future. Appreciate it!

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#6 Post by ALbino » 27 Oct 2014 20:59

Also.. since I plan on doing a bunch of manipulation of that variable would I then have to add "call" to every line? How would I do the arithmetic and renaming with that scheme?
-----
I tested this out, I can't seem to make it work other than the echo.

I guess I'm really just back to my original question, was why doesn't my code in the first post work? What am I doing wrong that I can can echo %%H, but I can't set the variable %DelimitedFilename% to %%H?
Last edited by ALbino on 27 Oct 2014 21:12, edited 1 time in total.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Trouble Setting Variable in For Loop

#7 Post by Yury » 27 Oct 2014 21:12

The another way:


Code: Select all

@echo off

setlocal enabledelayedexpansion
for /f "delims=" %%G in ('dir /a-d/b *.txt') do (
 set Filename=%%~nG
 for /f "tokens=2 delims=|" %%H in ("!Filename:--=|!") do (
  set FourDigitNumber=%%H
  echo Four Digit Number: !FourDigitNumber!
  )
 )

endlocal
pause>nul
exit /b



.




EnableDelayedExpansion

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#8 Post by ALbino » 27 Oct 2014 21:14

That works as well, and seemingly more cleanly. Thanks so much, I'm going to read up on "setlocal enabledelayedexpansion".

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#9 Post by ALbino » 27 Oct 2014 21:27

While this does work, I'm still completely mystified as to why my original code doesn't work:

Code: Select all

for /F "tokens=1,2 delims=--" %%G in ('dir /b *.txt') do (

   echo Result: %%H

   set DelimitedFilename=%%H

   echo DelimitedFilename: %DelimitedFilename%

   set FourDigitNumber=%DelimitedFilename:~1,4%

   echo Four Digit Number: %FourDigitNumber%
)


Can anybody offer an explanation or point me in the right direction?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Trouble Setting Variable in For Loop

#10 Post by ShadowThief » 27 Oct 2014 21:31

The short version is that delayed expansion allows the variables inside the for loop to update.

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Trouble Setting Variable in For Loop

#11 Post by ALbino » 27 Oct 2014 21:37

ShadowThief wrote:The short version is that delayed expansion allows the variables inside the for loop to update.


That's true for his example I think, but not for mine, right?

If I do something simple like this it works fine:

Code: Select all

for /F "delims=" %%a in ('dir /b *.txt') do set FoundFile="%%~fa" & echo %FoundFile%


But this doesn't work:

Code: Select all

for /F "tokens=1,2 delims=--" %%G in ('dir /b *.txt') do set FoundFile=%%H & echo Found File: %FoundFile%


Why does one work, but not the other?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Trouble Setting Variable in For Loop

#12 Post by Yury » 27 Oct 2014 21:38

ALbino wrote:<...> or point me in the right direction?



Code: Select all

setlocal enabledelayedexpansion

for /F "tokens=1,2 delims=-" %%G in ('dir /b *.txt') do (

   echo Result: %%H

   set DelimitedFilename=%%H

   echo DelimitedFilename: !DelimitedFilename!

   set FourDigitNumber=!DelimitedFilename:~1,4!

   echo Four Digit Number: !FourDigitNumber!

)

Post Reply