set command inside for loop does not work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fltbuff1792
Posts: 5
Joined: 15 Aug 2020 05:35

set command inside for loop does not work

#1 Post by fltbuff1792 » 15 Aug 2020 05:48

I reposted corrected version of code, as soon as approved it will post
Last edited by fltbuff1792 on 15 Aug 2020 14:16, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: set command inside for loop does not work

#2 Post by aGerman » 15 Aug 2020 09:02

Only variables enclosed in !! are expanded to their new value in a parenthesized block of command lines. And the body of a FOR loop is such a parenthesized block.
Also a closing parenthesis in a string might be interpreted as the closing parenthesis of the block. Rather escape it like that in the string: ^)

Steffen

fltbuff1792
Posts: 5
Joined: 15 Aug 2020 05:35

Re: set command inside for loop does not work

#3 Post by fltbuff1792 » 15 Aug 2020 13:39

Sorry, I posted my full code and result below so maybe you can help....
Input file..............

log lvars (2020_02_11 09_09_10 UTC).lua
fixwindows10 (2020_02_11 09_09_10 UTC).bat
quick fsx se config file (2020_05_27 10_49_35 UTC).bat
fsx se normal config file (2020_05_27 10_49_35 UTC).bat
quick fsx config file (2020_02_11 09_09_10 UTC).bat

Results of echos........................
222savlinechk===loglvars"&set"right1=2020_02_1109_09_10UTC).lua. ==== save name===loglvars"&set"right1=2020_02_1109_09_10UTC).lua. ====
222savlinechk===fixwindows10"&set"right1=2020_02_1109_09_10UTC).bat.==== save name===fixwindows10"&set"right1=2020_02_1109_09_10UTC).bat.====
222savlinechk===quickfsxseconfigfile"&set"right1=2020_05_2710_49_35UTC).bat.==== save name===quickfsxseconfigfile"&set"right1=2020_05_2710_49_35UTC).bat.====
222savlinechk===fsxsenormalconfigfile"&set"right1=2020_05_2710_49_35UTC).bat.==== save name===fsxsenormalconfigfile"&set"right1=2020_05_2710_49_35UTC).bat.====
222savlinechk===quickfsxconfigfile"&set"right1=2020_02_1109_09_10UTC).bat.==== save name===quickfsxconfigfile"&set"right1=2020_02_1109_09_10UTC).bat.====
222savlinechk===.==== save name===.====


Desired result would be ................

loglvars.lua
fixwindows10.bat
quickfsxseconfigfile.bat
sxsenormalconfigfile.bat

etc........


Code...............................

Code: Select all

FOR /F "usebackq tokens=*" %%G IN ("%_FilePath%%_WrkFile%") DO (  
        set _LineChk=%%G 
        set _NewRec=!_LineChk! 
        set base=!_NewRec! 
        set left1=!base:(=" & set "right1=!
        set left2=!base:.=" & set "right2=!  
        set left1=!left1: =!
        set right2=!right2: =!
        set right=
        set whole=!left1!.!right2!
        if !firstsw!==y set savname=!whole!   
     rem   echo 222 savlinechk===!whole=== save name===!savname!===   
       if  !whole! == !savname! (  
           rem SET _NewRec=!_LineChk!
           set _NewRec=!_NewRec:~0,-1! 
           echo !_NewRec!        
           )
          set firstsw=n
          set savname=!whole!
          echo 222savlinechk===!whole!=== save name===!savname!===   
     ) >> "%_FilePath%%_FileName%"  
Any help greatly appreciated!!!
Last edited by aGerman on 15 Aug 2020 15:08, edited 1 time in total.
Reason: code formatting

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: set command inside for loop does not work

#4 Post by aGerman » 15 Aug 2020 15:30

Not sure what you're doing in your code but it seems to be overcomplicated. Based on your desired output this may work for you

Code: Select all

setlocal DisableDelayedExpansion
>"out.txt" (
  FOR /F "usebackq tokens=*" %%G IN ("test.txt") DO (
    FOR /F "delims=(" %%H in ("%%~nG") do set "basename=%%H"
    set "extension=%%~xG"
    setlocal EnableDelayedExpansion
    echo !basename:~,-1!!extension!
    endlocal
  )
)
Steffen

fltbuff1792
Posts: 5
Joined: 15 Aug 2020 05:35

Re: set command inside for loop does not work

#5 Post by fltbuff1792 » 15 Aug 2020 15:58

don't thourouhly understand it yet, but after dinner I will give it a try and check out results

fltbuff1792
Posts: 5
Joined: 15 Aug 2020 05:35

Re: set command inside for loop does not work

#6 Post by fltbuff1792 » 15 Aug 2020 15:59

thaks Stefin

fltbuff1792
Posts: 5
Joined: 15 Aug 2020 05:35

Re: set command inside for loop does not work

#7 Post by fltbuff1792 » 16 Aug 2020 03:25

You are a friggin' genius !!!

Thanks


While I do not thouroughly understand "how" the code does what it does, but it does

and even took care of some weird file names like

"file name (utc........).bak.txt"

thanks again Steffen !

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: set command inside for loop does not work

#8 Post by aGerman » 16 Aug 2020 04:48

Yeah, okay I should have explained it a little.
First of all have a look at the help message of FOR. There are modifiers that make life easier. The ~n modifier expands to the file name (without extension) and the ~x modifier expands to the file extension. The outer loop (%%G) is used for that. In the inner loop (%%H) I just cut off the name at the opening parenthesis. So, in the end we have "log lvars " in variable basename and ".lua" in variable extension. The only thing you still have to do is removing the trailing space from basename. String manipulation !basename:~,-1! (or !basename:~0,-1! if that helps to understand it) is doing that. The help message of SET explains how it wokrs,

Steffen

Post Reply