Can someone help me understand?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Can someone help me understand?

#1 Post by SIMMS7400 » 06 Mar 2018 19:50

Hi Folks -

I feel silly asking this but can someone help me understand these two commands?

And if there's the ability, is there is a way to shorten them up and make them more efficient?

Code: Select all

echo|set /p=>%LocationOffsetNew%set cntProject=0set file=%LocationOffset%set /a newProjects=0for /f %%A in ('type "%file%"^|find "" /v /c') do set /a newProjects=%%AECHO NEWPROJECTSCOUNT %newProjects% FOR /F "tokens=1-8 delims=," %%A IN (%LocationOffset%) DO (	SET /a cntProject=0	FOR /F "tokens=1-2 delims=," %%Y IN (%MappingFile%) DO (		IF %%Y==%%E (			ECHO %%A^,%%B^,%%C^,%%D^,%%E^,%%F^,%%G^,%%H^,%%Z>>%LocationOffsetNew%		) 	))



::-- Mapping of MP to LocationByCC file --::

echo|set /p=>%LocationByCCNew%set cntProject=0set file=%LocationByCC%set /a newProjects=0for /f %%A in ('type "%file%"^|find "" /v /c') do set /a newProjects=%%AECHO NEWPROJECTSCOUNT %newProjects% FOR /F "tokens=1-8 delims=," %%A IN (%LocationByCC%) DO (	SET /a cntProject=0	FOR /F "tokens=1-2 delims=," %%Y IN (%MappingFile%) DO (		IF %%Y==%%E (			ECHO %%A^,%%B^,%%C^,%%D^,%%E^,%%F^,%%G^,%%H^,%%Z>>%LocationByCCNew%		) 	))


Thank you!

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

Re: Can someone help me understand?

#2 Post by ShadowThief » 06 Mar 2018 20:36

Does the code actually look like that or did your newlines magically disappear? Because there's no way that that code possibly works the way it's currently written all on one line like that.

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

Re: Can someone help me understand?

#3 Post by ShadowThief » 06 Mar 2018 23:04

Assuming the newlines are just missing, your code does this (both sets of code do basically the same thing, so I'll only describe the first one):

Code: Select all

REM Zeroizes the file whose name is stored in %LocationOffsetNew%
echo|set /p=>%LocationOffsetNew%

REM Sets %cntProject% to 0. I'm not sure why, since you never use this variable.
set cntProject=0

REM Sets %file% to %LocationOffset%. Arguably redundant since neither of these
REM variables changes value from here on.
set file=%LocationOffset%

REM Sets newProjects to 0. Again, totally redundant since we're setting it once
REM in the next line.
set /a newProjects=0

REM Sets %newProjects% to the number of non-blank lines in the file
REM whose name is stored in %file%
for /f %%A in ('type "%file%"^|find "" /v /c') do set /a newProjects=%%A

REM Display this number
ECHO NEWPROJECTSCOUNT %newProjects%

REM Iterates over each line in %LocationOffset%, grabbing the first 8 tokens
REM of a comma-delimited string and storing them in %%A through %%H
FOR /F "tokens=1-8 delims=," %%A IN (%LocationOffset%) DO (
	
	REM Sets %cntProject% to 0 for some reason. Again, we never use this variable.
	SET /a cntProject=0
	
	REM Iterates over each line in %MappingFile%, grabbing the first two tokens
	REM of a comma-delimited string and storing them in %%Y and %%Z
	FOR /F "tokens=1-2 delims=," %%Y IN (%MappingFile%) DO (
		
		REM If %%Y is equal to %%E
		IF %%Y==%%E (
			REM Echo all tokens from the %LocationOffset% string and the
			REM second token from the %MappingFile% string to the
			REM %LocationOffsetNew% file. The carets before the commas are
			REM unnecessary.
			ECHO %%A^,%%B^,%%C^,%%D^,%%E^,%%F^,%%G^,%%H^,%%Z>>%LocationOffsetNew%
		)
	)
)
They can both be simplified like this:

Code: Select all

for /f %%A in ('find /v /c "" %LocationOffset%') do echo NEWPROJECTSCOUNT %%A

type nul >%LocationOffsetNew%
for /f "tokens=1-8 delims=," %%A in (%LocationOffset%) do (
	for /f "tokens=1-2 delims=," %%Y in (%MappingFile%) do (
		if "%%Y"=="%%E" (
			echo %%A,%%B,%%C,%%D,%%E,%%F,%%G,%%H,%%Z >>%LocationOffsetNew%
		)
	)
)

:: -- Mapping of MP to LocationByCC file --
for /f %%A in ('find /v /c "" %LocationByCC%') do echo NEWPROJECTSCOUNT %%A

type nul >%LocationByCCNew%
for /f "tokens=1-8 delims=," %%A in (%LocationByCC%) do (
	for /f "tokens=1-2 delims=," %%Y in (%MappingFile%) do (
		if "%%Y"=="%%E" (
			echo %%A,%%B,%%C,%%D,%%E,%%F,%%G,%%H,%%Z >>%LocationByCCNew%
		)
	)
)
If I saw some sample data, I could maybe optimize it further.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Can someone help me understand?

#4 Post by Compo » 07 Mar 2018 12:20

I suppose if delayed expansion isn't a problem, you could run them both together like this:

Code: Select all

For %%A In (LocationOffset LocationByCC) Do (
	For /F "Delims=:" %%B In ('"Type "!%%A!"|Find "" /V /C" 2>Nul"'
	) Do Echo NEWPROJECTSCOUNT %%B
	(	For /F "UseBackQ Tokens=1-8 Delims=," %%B In ("%LocationOffset%"
		) Do For /F "UseBackQ Tokens=1-2 Delims=," %%J In ("%MappingFile%"
		) Do If /I "%%J"=="%%F" Echo %%B,%%C,%%D,%%E,%%F,%%G,%%H,%%I,%%K
	)>"!%%ANew!")

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Can someone help me understand?

#5 Post by SIMMS7400 » 07 Mar 2018 17:48

Shadow -

Thank you so much! Works like a charm and I appreciate the explanations!!

Comp - I'm going to see if that will work too - thanks!

Very much appreciated!!!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Can someone help me understand?

#6 Post by Compo » 14 Mar 2018 06:12

…I know I didn't specifically write SetLocal EnableDelayedExpansion in my answer, because I had no idea if it was already set in the not posted portion of the code, but you have been back here since and provided no feedback!

Post Reply