Page 1 of 1

set command inside for loop does not work

Posted: 15 Aug 2020 05:48
by fltbuff1792
I reposted corrected version of code, as soon as approved it will post

Re: set command inside for loop does not work

Posted: 15 Aug 2020 09:02
by aGerman
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

Re: set command inside for loop does not work

Posted: 15 Aug 2020 13:39
by fltbuff1792
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!!!

Re: set command inside for loop does not work

Posted: 15 Aug 2020 15:30
by aGerman
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

Re: set command inside for loop does not work

Posted: 15 Aug 2020 15:58
by fltbuff1792
don't thourouhly understand it yet, but after dinner I will give it a try and check out results

Re: set command inside for loop does not work

Posted: 15 Aug 2020 15:59
by fltbuff1792
thaks Stefin

Re: set command inside for loop does not work

Posted: 16 Aug 2020 03:25
by fltbuff1792
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 !

Re: set command inside for loop does not work

Posted: 16 Aug 2020 04:48
by aGerman
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