Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

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

Avatar of Bill Prew
Bill Prew

Rene,

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

~bp
Avatar of ReneGe

ASKER

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
Can you provide a sample input file, and the desired results?

~bp
Avatar of ReneGe

ASKER

@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
Avatar of ReneGe

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

The solution I found was provided by both oBdA and gerwinjansen in:
https://www.experts-exchange.com/questions/28232914/String-manipulation-Variables.html

Cheers
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
Avatar of ReneGe

ASKER

Thanks Bill!

Until next time :)

Cheers
Avatar of ReneGe

ASKER

I selected my own solution as being the best solution because it is.
I awarded the points to Bill because he deserves them.
Avatar of ReneGe

ASKER

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
@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
Avatar of ReneGe

ASKER

As recommended by Bill.

Thanks and cheers,
Rene