Home > Mobile >  How to get multiple cases from text file with batch?
How to get multiple cases from text file with batch?

Time:04-05

I have a config text file witch contains cases and i have to call another batch script for each case. My config txt looks like this, i have multiple cases with the same properties that are optional:

main:
  - case1:
     a: 123
     b: 321
     c: 654
  - case2: 
     b: 523
     d: 736
     a: 834
  - case3: 
     c: 231
     d: 246

I have to call the other script with the properties as arguments, (case3):

call script2.bat null null 231 246

I tried going through the text file with a for loop but i don't know how to separate the cases and call the second script with the right values. If possible i would like to use only batch.

setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (test.txt) do (
   set line=%%a
   if "!line:~0,1!"=="a" (
      set a=!line:a: =!
   )
)

Thanks in advance

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q71733362.txt"

SET "parmlist=a b c d"

FOR /f "usebackqtokens=1,2delims=:- " %%b IN ("%filename1%") DO (
 IF /i "%%b"=="main" FOR %%b IN (%parmlist% invalidlist) DO SET "%%b=null"
 FOR %%e IN (%parmlist%) DO IF /i "%%e"=="%%b" SET "%%e=%%c"&SET "invalidlist="
 SET "case=%%b"
 IF /i "!case:~0,4!"=="case" IF NOT DEFINED invalidlist (
  SET "params="
  FOR %%e IN (%parmlist%) DO SET "params=!params! !%%e!"&SET "%%e=null"
  ECHO CALL script2!params!
  SET "invalidlist=Y"
 )
)

IF NOT DEFINED invalidlist (
 SET "params="
 FOR %%e IN (%parmlist%) DO SET "params=!params! !%%e!"
 ECHO CALL script2!params!
)

GOTO :EOF

The usebackq option is only required because I chose to add quotes around the source filename.

Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces.

I've use if /i throughout to make the string-match case-insensitive. Omit the /i if you want a case-sensitive match.

Set parmlist to a list of the parameters you wish to pass to script2.

Read the file using : - and Space as delimiters, assigning the first token found to %%b and the second to %%c (see for /? from the prompt or search a multitude of SO responses for documentation)

If the first token is main, set all of the variables in parmlist and the flag invalidlist to the value null.

Attempt to match the first token to the values in parmlist. on a match, set the value of the parameter found to the second token and set invalidlist to nothing which undefines invalidlist.

set case to the value of the first parameter, and examine the first 4 characters. If these match case and invalidlist is undefined (so a parameter has been defined) then build params by adding Spacethe value found (or null if no value has been found for that case) for each of the parameters in the list. and set the parameter value back to null.

I've echoed the script2 call for verification purposes. Remove the echo keyword to actually execute script2.

The parameter values have now all been reset to null, so set invalidlist to a value since the list is now invalid.

At the end, if invalidlist is not set, we've got the last case built, so generate the call for that last case.

Note: I've assumed that the values for the parameters are never negative, for lack of information otherwise.


In response to comment - 'case' may be some other string

SET "invalidlist=Y"
FOR /f "usebackqtokens=1,2delims=:- " %%b IN ("%filename1%") DO (
 IF "%%c" equ "" (
  IF /i "%%b"=="main" (
   FOR %%b IN (%parmlist% invalidlist) DO SET "%%b=null"
  ) ELSE (
   IF NOT DEFINED invalidlist (
    SET "params="
    FOR %%e IN (%parmlist%) DO SET "params=!params! !%%e!"&SET "%%e=null"
    ECHO CALL script2!params!
    SET "invalidlist=Y"
   )
  )
 ) ELSE FOR %%e IN (%parmlist%) DO IF /i "%%e"=="%%b" SET "%%e=%%c"&SET "invalidlist="

)

IF NOT DEFINED invalidlist (
 SET "params="
 FOR %%e IN (%parmlist%) DO SET "params=!params! !%%e!"
 ECHO CALL script2!params!
)

The above procedure in place of the original for loop and clean-up should allow any single word (except main) to be used in place of case

A line containing a single word with any number of leading or trailing delimiters will cause %%b to contain the word and %%c to be an empty string, so test for that condition and test %%b for main - initialise variables or something-else - output any accumulated data.

If %%c contains something, then try to put it into the appropriate variable as before.

CodePudding user response:

Here is my take on this:

@echo off
set cnt=0 & set casen=0
setlocal enabledelayedexpansion
for /f "tokens=1-3*delims=: " %%i in ('type test.txt ^| findstr /V "main:"') do (
    set "line=%%i %%j"
    set /a cnt =1
    if "%%i" == "-" (
       set "case=!casen!"
       set /a casen =1
       set cnt=
       for %%f in (a b c d) do set "_%%f=null"
  )
  for %%f in (a b c d) do if "%%f" == "%%i" set "_%%f=%%j"
  set "case!casen!=!_a! !_b! !_c! !_d!"
)
for %%e in (1,1,           
  • Related