parsing strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
TekFreak260
Posts: 1
Joined: 04 Nov 2022 14:40

parsing strings

#1 Post by TekFreak260 » 04 Nov 2022 14:50

I am trying to write a batch file that basically parses a string and determines a few things based on the filename. So for example, a test filename would be A01B_NCS_Tab_1102.pdf. My task is to read in all file names from the current directory and determine if the number (2nd and 3rd characters which form a page number) are odd or even. Then find out if the 4th character, is a B or a C to be able to filter things further. Lastly Be able to look to see if the filename has the word "Tab" in it. Then depending on what those variables, would determine where the file gets moved to. I tried using a "dir /b *.pdf" and dumping to a file to get just the filenames from the current directory then reading in the file using
FOR /F "Tokens=1" %%G in (filename.txt) do (
set str=%%G
set PgNum=%str:~1,2%
echo %PgNum% >>filename2.txt
)

but the file is empty and i get "echo is off" on my dos window. Which tells me for some reason the variables are all empty. What am i doing wrong?

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: parsing strings

#2 Post by T3RRY » 04 Nov 2022 23:30

When working with variables in loops or code blocks, Variables are parsed with the value the had prior to the commencement of the codeblock
To access their current value, Delayed expansion is required to be enabled and variables expanded using the ! reference character, IE: !variable!

Code: Select all

@Echo off & CD /D "%~dp0"

	REM clear any pre-existing definitions of IsPROPERTY; supress error if no definitions.
	2> nul (For /f "tokens=1 Delims==" %%G in ('Set Is')Do Set "%%G=")

for /F "tokens=1* Delims=_" %%G in ('Dir /B *_*_*_*.pdf')Do (
	Set "Lead=%%G"
	Set "filename=%%G_%%H"

	REM enable environment for delayed expansion. Enabled during each loop in order to preserve ! characters in filenames if present
	Setlocal EnableDelayedExpansion

	REM enable below line to see filename being processed
	REM Set filename

	REM check filename for _Tab_ string; define IsTab if True
	If not "!filename:_Tab_=!" == "!filename!" Set "IsTab=True"

	REM Isolate Last character of first _ delimited token and assign true state as IsCHAR
	Set "Is!Lead:~3,1!=true"

	REM remove first and last characters of first _ delimited token to isolate page number
	Set "Lead=!Lead:~1,-1!"

	REM Test if leading character is 0; remove if true
	If "!Lead:~0,1!" == "0" Set "Lead=!Lead:~1,1!"

	REM Use modulous operation on the page number in an expression designed to trigger a divide by zero error if true
	2> nul Set /A "1/(Lead %% 2)" && (
	REM conditional operation if no divide by zero error
		Set "IsOdd=true"
	) || (
	REM conditional operation upon divide by zero error
		Set "IsEven=true"
	)
	Rem Displays states. Use combinations of If defined IsPROPERTY to enact required logic.
	Set Is

	Rem perform any logic required for each file prior to Endlocal
	Endlocal
)

Pause
Goto:Eof 

Post Reply