Dynamic Variable and IF Defined

Hi there,

My following batch file reads an XML file and limits the data length depending on the XML tag.

However, the "IF DEFINED" and "Dynamic variable" functions does no work.

Thanks for fixing it.

Cheers,
Rene
@ECHO OFF

SETLOCAL EnableDelayedExpansion

IF "%~1" == "" (ECHO PLEASE DRAG AND DROP XLM & PAUSE & EXIT)

SET XML=%~1
SET LineNb=0

FOR /F "usebackq tokens=1-3* delims=><" %%A in ("%~1") DO (
  REM Cleaning tag to use as a variable
    SET Tag=%%B
    SET Tag=!Tag:di:=!
    SET Tag=!Tag:"=!
    SET Tag=!Tag: =!
    FOR /F "tokens=1,2 delims==" %%a in ("!Tag!") DO SET TagTmp=!TagTMP!%%a%%b
    SET Tag=!TagTmp!
    SET TagTmp=
  
  IF "%%C" NEQ "" (
    SET Data=%%C
    IF DEFINED Length[!Tag!] CALL :Trim "Data" "!Tag!" "!Data!"
    IF DEFINED Space[!Tag!] SET Data=!Data: =!
    ECHO "!Tag!" "!Data!"
  ) ELSE (
  SET Data=
  )
)

PAUSE
EXIT


:Trim
SET Index=!Length[%~2]!
SET %~1=!%~3:~0,%Index%!
EXIT /b


:DefineTest
REM Defining data Length

SET Length[XMLTagName1]=20
SET Length[XMLTagName2]=20
SET Length[XMLTagName3]=5

EXIT /b

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Rene,

Am I missing something, I don't see where you are setting Length[!Tag!] or Space[!Tag!] ?

~bp
0
ReneGeAuthor Commented:
Hey Bill, its been a while.  Nice hearing from you!!

I recently moved in Mississauga,On from Montreal,Qc and I have not enough of 48h per day.  That makes me make do stupid mistakes like forgetting to call subs in my batch files :)

Length[!Tag!] is defined in :DefineTest. However, I did not CALLed it. Just added it in line 6.

Space[!Tag!] is not defined yet.  So don't bother about this variable.

Both "Trim" and "IF DEFINED" functions still not work.

Cheers,
Rene
0
Bill PrewCommented:
Can you provide a sample input file, and the desired results?

~bp
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

ReneGeAuthor Commented:
@Bill,
I will provide data sample later today.  Thanks

@Modalot,
%D is the remaining stuff after %%C because I put * after 3.

For example:
for /f "tokens=1* delims=," %%a in ("a,b,c,d") do echo [%%a] [%%b]

I prefer do do this in a batch file.

Thanks and cheers,
Rene
0
ReneGeAuthor Commented:
Hi Bill,

Please refer to the following script.

At line 37, the following does not work:
SET Data=!Data:~0,%Length[!Tag!]%!

I also tried replacing line 37 with:
IF DEFINED Length[!Tag!] (CALL :SetTrim "Length[!Tag!]" & SET Data=!Data:~0,%Trim%!)

By also adding:
:SetTrim
SET Trim=!%~1!
EXIT /b

Thanks for your help.

Cheers,
Rene

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET XML=%~n0.xml

REM CREATING TEST INPUT FILE
ECHO ^<di:LoadHeader^>>"%XML%"
ECHO   ^<di:XMLTagName1^>123456789^</di:value1^>>>"%XML%"
ECHO   ^<di:XMLTagName2^>1234567890^</di:value2^>>>"%XML%"
ECHO ^</di:LoadHeader^>>>"%XML%"

CALL :DefineLength

ECHO.
ECHO.
ECHO EXPECTED OUTPUT
ECHO ---------------------
ECHO "XMLTagName1" : BEFORE="123456789" AFTER="12345"
ECHO "XMLTagName2" : BEFORE="1234567890" AFTER="123456"

ECHO.
ECHO.
ECHO RESULT
ECHO ---------------------
FOR /F "usebackq tokens=1-3* delims=><" %%A in ("%XML%") DO (
  REM Cleaning tag to use as a variable
    SET Tag=%%B
    SET Tag=!Tag:di:=!
    SET Tag=!Tag:"=!
    SET Tag=!Tag: =!
    FOR /F "tokens=1,2 delims==" %%a in ("!Tag!") DO SET TagTmp=!TagTMP!%%a%%b
    SET Tag=!TagTmp!
    SET TagTmp=
  IF "%%C" NEQ "" (
    SET Data=%%C
    IF DEFINED Length[!Tag!] SET Data=!Data:~0,%Length[!Tag!]%!
    ECHO "!Tag!" : BEFORE="%%C" AFTER="!Data!"
  ) ELSE (
  SET Data=
  )
)

ECHO.
PAUSE
EXIT


:DefineLength
REM Defining data Length
ECHO DEFINED LENGTHS
ECHO ---------------------
SET Length[XMLTagName1]=5
SET Length[XMLTagName2]=6
SET Length
EXIT /b 

Open in new window

0
ReneGeAuthor Commented:
Problem solved.

@Bill
Do you have a better idea, or should I just close this question by giving points immediatly?

Thanks and cheers,
Rene

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET XML=%~n0.xml

REM CREATING TEST INPUT FILE
ECHO ^<di:LoadHeader^>>"%XML%"
ECHO   ^<di:XMLTagName1^>123456789^</di:value1^>>>"%XML%"
ECHO   ^<di:XMLTagName2^>1234567890^</di:value2^>>>"%XML%"
ECHO ^</di:LoadHeader^>>>"%XML%"

CALL :DefineLength

ECHO.
ECHO.
ECHO EXPECTED OUTPUT
ECHO ---------------------
ECHO "XMLTagName1" : BEFORE="123456789" AFTER="12345"
ECHO "XMLTagName2" : BEFORE="1234567890" AFTER="123456"

ECHO.
ECHO.
ECHO RESULT
ECHO ---------------------
FOR /F "usebackq tokens=1-3* delims=><" %%A in ("%XML%") DO (
  REM Cleaning tag to use as a variable
    SET Tag=%%B
    SET Tag=!Tag:di:=!
    SET Tag=!Tag:"=!
    SET Tag=!Tag: =!
    FOR /F "tokens=1,2 delims==" %%a in ("!Tag!") DO SET TagTmp=!TagTMP!%%a%%b
    SET Tag=!TagTmp!
    SET TagTmp=
  IF "%%C" NEQ "" (
    SET Data=%%C
    IF DEFINED Length[!Tag!] CALL :Trim "Length[!Tag!]"
    ECHO "!Tag!" : BEFORE="%%C" AFTER="!Data!"
  ) ELSE (
  SET Data=
  )
)

ECHO.
PAUSE
EXIT

:Trim
SET Trim=!%~1!
SET Data=!Data:~0,%Trim%!
EXIT /b

:DefineLength
REM Defining data Length
ECHO DEFINED LENGTHS
ECHO ---------------------
SET Length[XMLTagName1]=5
SET Length[XMLTagName2]=6
SET Length
EXIT /b 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ReneGeAuthor Commented:
The solution I found was provided by both oBdA and gerwinjansen in:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28232914.html

Cheers
0
Bill PrewCommented:
Rene,

I was intending to look at this, had some ideas, but have been swamped lately and probably will be for a while, I'd say go ahead with what you have.

~bp
0
ReneGeAuthor Commented:
Thanks Bill!

Until next time :)

Cheers
0
ReneGeAuthor Commented:
I selected my own solution as being the best solution because it is.
I awarded the points to Bill because he deserves them.
0
ReneGeAuthor Commented:
Hi Qlemo,

Well, Bill did pointed out a bug in ID: 39472260.

Would you mind re-opening it so i can give him the points?

Cheers,
Rene
0
Bill PrewCommented:
@Rene,

I'd recommend closing with just the accept on your http:#a39482873, 0 points.

In this question my contribution was minor and didn't really relate directly to your puzzling out a solution.

It's great that we build relationships here on EE, but in terms of solution recognition we want to separate the true significant contributions and solutions from the general conversation. Often a hard and subjective decision, but in this case I think I hadn't really gotten in to truly contributing to the solution to the problem posed.

See my profile if you want to connect outside of EE on other topics, I do enjoy our interactions.

~bp
0
ReneGeAuthor Commented:
As recommended by Bill.

Thanks and cheers,
Rene
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.