Why this condition clause does not work as expected?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Why this condition clause does not work as expected?

#1 Post by goodywp » 17 Nov 2017 10:19

I have following code to do the job as
1) When fk.txt not empty, and filter the missing ones from the comparison between fk.txt and fk_found.txt and
output to missed_fk_schemes.txt

2) When fk.txt not empty, but fk_found.txt empty, then just type fk.txt to missed_fk_schemes.txt

Code: Select all

call C:\auto_pkg_build\Scripts\scheme_replace\varconfig.cmd
call C:\auto_pkg_build\Scripts\scheme_replace\schpak.cmd

set VAR16=%VAR16: =%
set VARfk=%VARfk: =%

cd C:\auto_pkg_build\Sources\Source_schemes\%VAR16%\temp

if exist missed_fk_schemes.txt (del missed_fk_schemes.txt)

(for /f %%i in ("fk.txt") do set size=%%~zi

if %size% gtr 0 type fk.txt|findstr /vig:fk_found.txt >>missed_fk_schemes.txt

for /f %%i in ("fk_found.txt") do set size=%%~zi

if %size% equ 0 type fk.txt >>missed_fk_schemes.txt)
(for /f %%i in ("fk.txt") do set size=%%~zi

if %size% gtr 0 type fk.txt|findstr /vig:fk_found.txt >>missed_fk_schemes.txt

for /f %%i in ("fk_found.txt") do set size=%%~zi

if %size% equ 0 type fk.txt >>missed_fk_schemes.txt)


I thought this bracket above in bold should work as one block??

Thanks
Last edited by goodywp on 17 Nov 2017 15:51, edited 1 time in total.

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

Re: Why this condition clause does not work as expected?

#2 Post by ShadowThief » 17 Nov 2017 12:25

Why are you putting that code in a code block? You only need code blocks if you're grouping commands for if statements or for loops. What do you expect the code block to be doing that it wouldn't be doing when the commands are run one line at a time?

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

Re: Why this condition clause does not work as expected?

#3 Post by Squashman » 17 Nov 2017 13:02

As ShadowThief has pointed out already, there is absolutely no need for you to create a parenthesized code block around all that code. Any time you are inside a parenthesized code block, any variables that have been defined inside that code block need to be referenced with delayed expansion.

But if you really want to use the parenthesized code block, then use the FOR variables to your advantage so that you don't need to utilize delayed expansion

Code: Select all

(
	for /f %%G in ("fk.txt") do (
		if %%G gtr 0 type fk.txt|findstr /vig:fk_found.txt >>missed_fk_schemes.txt
	)

	for /f %%H in ("fk_found.txt") do (
		if %%H equ 0 type fk.txt >>missed_fk_schemes.txt
	)
)
But like we said, I see no reason to put all this code inside a parenthesized code block.

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Why this condition clause does not work as expected?

#4 Post by goodywp » 17 Nov 2017 14:33

Finally figured it out as this and it works

Code: Select all

for /f %%i in ("fk.txt") do set sizei=%%~zi
for /f %%j in ("fk_found.txt") do set sizej=%%~zj
if %sizei% gtr 0 (
	if %sizej% gtr 0 (
		type fk.txt|findstr /vig:fk_found.txt >>missed_fk_schemes.txt
			 )
		 )
if %sizei% gtr 0 (
	if %sizej% equ 0 (
		type fk.txt >>missed_fk_schemes.txt
			 )
		 )

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

Re: Why this condition clause does not work as expected?

#5 Post by Squashman » 17 Nov 2017 14:47

You could shorten that up.

Code: Select all

for /f %%i in ("fk.txt") do set sizei=%%~zi
for /f %%j in ("fk_found.txt") do set sizej=%%~zj
if %sizei% gtr 0 (
	if %sizej% gtr 0 (
		type fk.txt|findstr /vig:fk_found.txt >>missed_fk_schemes.txt
	) else (
		type fk.txt >>missed_fk_schemes.txt
	)
)

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Why this condition clause does not work as expected?

#6 Post by goodywp » 17 Nov 2017 15:32

Thanks Squashman :D

Post Reply