set command inside for loop does not work
Moderator: DosItHelp
-
- Posts: 5
- Joined: 15 Aug 2020 05:35
set command inside for loop does not work
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.
Re: set command inside for loop does not work
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
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
-
- Posts: 5
- Joined: 15 Aug 2020 05:35
Re: set command inside for loop does not work
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...............................
Any help greatly appreciated!!!
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%"
Last edited by aGerman on 15 Aug 2020 15:08, edited 1 time in total.
Reason: code formatting
Reason: code formatting
Re: set command inside for loop does not work
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
Steffen
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
)
)
-
- Posts: 5
- Joined: 15 Aug 2020 05:35
Re: set command inside for loop does not work
don't thourouhly understand it yet, but after dinner I will give it a try and check out results
-
- Posts: 5
- Joined: 15 Aug 2020 05:35
Re: set command inside for loop does not work
thaks Stefin
-
- Posts: 5
- Joined: 15 Aug 2020 05:35
Re: set command inside for loop does not work
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 !
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
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
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