A few months back, Atonio helped me with a chunk of code that determined "min" and "max" month in a data file. I expanded it a bit more to except months in the following formats:
It's working fine. However, performance is not so great. 1600 lines takes a few minutes to run and I have data files that are 40k rows at times. Is there any way to speed up this code? I assume to the call to the string length function causes this to slow down quite a bit?1,01,Jan and January
Sample file:
Code:"Years"|"Period"|"Scenario"|"Version"|"Plan Element"|"Account"|"Entity"|"Funding"|"State"|"Segment"|"Department"|"Product"|"Amount"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"7020"|"150"|"No Fund"|"No State"|"No Segment"|"150ADCO152"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"7140"|"150"|"No Fund"|"No State"|"No Segment"|"150ADCO152"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"7750"|"150"|"No Fund"|"No State"|"No Segment"|"150ADCO152"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCN550"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCO100"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCO101"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCO105"|"No Product"|"100"
"2020"|"Jan"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCO150"|"No Product"|"100"
"2020"|"Feb"|"Actual"|"PreAlloc"|"TLoad"|"6010"|"100"|"No Fund"|"No State"|"No Segment"|"100HCCO154"|"No Product"|"100"
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A "MAXM=-99999", "MINM=99999"
FOR /F "skip=1 USEBACKQ tokens=1-2 delims=|" %%a IN ("test.txt") DO (
SET "MONTH=%%~b"
ECHO "!MONTH!"| FINDSTR /r "^[1-9][0-9]*$">NUL || (
SET "MONTH=!MONTH:~0,3!"
IF "!MONTH!"=="Jan" SET "MONTH=01"
IF "!MONTH!"=="Feb" SET "MONTH=02"
IF "!MONTH!"=="Mar" SET "MONTH=03"
IF "!MONTH!"=="Apr" SET "MONTH=04"
IF "!MONTH!"=="May" SET "MONTH=05"
IF "!MONTH!"=="Jun" SET "MONTH=06"
IF "!MONTH!"=="Jul" SET "MONTH=07"
IF "!MONTH!"=="Aug" SET "MONTH=08"
IF "!MONTH!"=="Sep" SET "MONTH=09"
IF "!MONTH!"=="Oct" SET "MONTH=10"
IF "!MONTH!"=="Nov" SET "MONTH=11"
IF "!MONTH!"=="Dec" SET "MONTH=12"
) && (
CALL :STRLEN RESULT MONTH
IF "!RESULT!"=="1" SET "MONTH=0%%~b"
)
IF !MONTH! GTR !MAXM! SET "MAXM=!MONTH!"
IF !MONTH! LSS !MINM! SET "MINM=!MONTH!"
)
echo %MINM%
echo %MAXM%
pause
:STRLEN <resultVar> <stringVar>
(
SET "S=!%~2!#"
SET "LEN=0"
FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
IF "!S:~%%P,1!" NEQ "" (
SET /a "LEN+=%%P"
SET "S=!S:~%%P!"
)
)
)
(
ENDLOCAL
SET "%~1=%LEN%"
EXIT /B
)