Roy R
Roy R

Reputation: 31

Batch Programming: read txt and keep variables with a subroutine

I am writing a bactch-file for the first time and I have a problem that I couldn't solve yet. (I'm not a programmer but just a mechanical engineer, so please forgive me for my clumsiness.. And my english: I'm not a native speaker...)

I want to write a batch file (readlist.bat) that amongst other things should read in a txt-file (Text.txt). This batch-file then should be able to be called in another batch file with "call readlist.bat". What is clear to me is the following: I have to define variables within the readlist.bat not only locally if I want them to be stored after readlist.bat finishes. I tried to realize this in the attached files at least for some of the variables. (Of course the final goal is to keep all the variables read in from the txt-file.) But I cant manage this.

The Text.txt only contains "test variables" readlist.btat should read in all rows in columns (this works). But the variables need to be defined "not locally". Therefore for testing reasons I added the following commands:

endlocal  
set "job1fld=%job1fld%"  
set "job1dat=%job1dat%"  
set "job1kyw=%job1kyw%"  
set "job2fld=%job2fld%"  
set "job2dat=%job2dat%"  
set "job2kyw=%job2kyw%"  
set "job3fld=%job3fld%"  
set "job3dat=%job3dat%"  
set "job3kyw=%job3kyw%"  

set vartest1=test1  
set vartest2=test2  
set vartest3=test3  
set vartest4=test4  
set vartest5=test5  
set vartest5=test6  
set vartest6=test7 

the second block seems to work. Or lets say: Those variables are handed over to the callreadlist. But those are only manually set test-variables...

The first block however doesn't work and I can't figureout why...

How do I manage that all the variables read in are kept after readlist.bat finishes? Because I am an absolute greenhorn when it comes to batch-files, I would be glad if you don't only give me tipps but codes ;-)

Thank you in advance.

Roy

CALLREADLIST.BAT:

@echo off
call readlist.bat
set
pause

READLIST.BAT:

REM =================================
REM =================================
REM READ TXT
@echo off
setlocal ENABLEDELAYEDEXPANSION
REM START: Read 1. Column (Ordner)
set ColNo=1
set cntr1=0
for /F "delims=; tokens=%ColNo%" %%A in (Text.txt) do (
    set /a cntr1=!cntr1! + 1
    set job!cntr1!fld=%%A
)
REM END: Read 1. Column (Ordner)
REM START: Read 2. Column (Ordner)
set ColNo=2
set cntr2=0
for /F "delims=; tokens=%ColNo%" %%A in (Text.txt) do (
    SET /A cntr2=!cntr2! + 1
    set job!cntr2!dat=%%A
)
REM END: Read 2. Column (Ordner)
REM START: Read 3. Column (Ordner)
set ColNo=3
set cntr3=0
for /F "delims=; tokens=%ColNo%" %%A in (Text.txt) do (
    SET /A cntr3=!cntr3! + 1
    set job!cntr3!kyw=%%A
)
REM END: Read 3. Column (Ordner)
endlocal
set "job1fld=%job1fld%"
set "job1dat=%job1dat%"
set "job1kyw=%job1kyw%"
set "job2fld=%job2fld%"
set "job2dat=%job2dat%"
set "job2kyw=%job2kyw%"
set "job3fld=%job3fld%"
set "job3dat=%job3dat%"
set "job3kyw=%job3kyw%"
set vartest1=test1
set vartest2=test2
set vartest3=test3
set vartest4=test4
set vartest5=test5
set vartest5=test6
set vartest6=test7

TXT-FILE:

Ordner1;Job1;Input1
Ordner2;Job2;Input2
Ordner3;Job3;Input3
Ordner4;Job4;Input4
Ordner5;Job5;Input5
Ordner6;Job6;Input6
Ordner7;Job7;Input7
Ordner8;Job8;Input8
Ordner9;Job9;Input9
Ordner10;Job10;Input10
Ordner11;Job11;Input11
Ordner12;Job12;Input12
Ordner13;Job13;Input13
Ordner14;Job14;Input14
Ordner15;Job15;Input15
Ordner16;Job16;Input16
Ordner17;Job17;Input17
Ordner18;Job18;Input18
Ordner19;Job19;Input19
Ordner20;Job20;Input20
Ordner21;Job21;Input21
Ordner22;Job22;Input22
Ordner23;Job23;Input23
Ordner24;Job24;Input24
Ordner25;Job25;Input25
Ordner26;Job26;Input26
Ordner27;Job27;Input27
Ordner28;Job28;Input28
Ordner29;Job29;Input29
Ordner30;Job30;Input30
Ordner31;Job31;Input31
Ordner32;Job32;Input32
Ordner33;Job33;Input33
Ordner34;Job34;Input34
Ordner35;Job35;Input35
Ordner36;Job36;Input36
Ordner37;Job37;Input37
Ordner38;Job38;Input38
Ordner39;Job39;Input39

Upvotes: 2

Views: 213

Answers (3)

Endoro
Endoro

Reputation: 37569

nice stuff:

@echo off
for /f %%a in ('^< Text.txt find /c /v ""') do set /a lines=%%a
<Text.txt (
for /l %%a in (1 1 %lines%) do (
    set "line="
    set /p "line="
    for /f "tokens=1-3delims=;" %%d in ('call echo %%line%%') do (
        set "job%%afld=%%d"&set "job%%asat=%%e"&set "job%%akyw=%%f"
    )
))
set "line="&set "lines="
set "job"

This is easy, the real challenge is to give each job-variable a unique number (here 1-117) without delayed expansion.

Upvotes: 3

noize
noize

Reputation: 93

You want to read each of the columns in Text.txt and store it in variables, right?

Easily:

@echo off
set i=1
for /f "delims= ; tokens=1-3" %%a in (Text.txt) do (
    set ordner%i%=%%a
    set job%i%=%%b
    set input%i%=%%c
    set /a i=i+1
)

This code will set variables like this:

set ordner1=Ordner1
set job1=Job1
set input1=Input1

set ordner2=Ordner2
set job2=Job2
set input2=Input2

...

From your calling file (CALLREADLIST.BAT) you can access any column on any line by simply using %columnLine% (e.g: %job26% where "job" is your column and "26" the line number).

For instance, if you want CALLREADLIST.BAT to output the "Input" at line 45 all you'd need would be:

@echo off
readlist
echo %input45%

The first block is not working just because you're assigning to variable names their own values, which is null (set var=%var%, i.e. set var= which undefines the variable).

Upvotes: 0

Magoo
Magoo

Reputation: 80003

Try:

...
REM END: Read 3. Column (Ordner)
endlocal&(
set "job1fld=%job1fld%"
set "job1dat=%job1dat%"
set "job1kyw=%job1kyw%"
set "job2fld=%job2fld%"
set "job2dat=%job2dat%"
set "job2kyw=%job2kyw%"
set "job3fld=%job3fld%"
set "job3dat=%job3dat%"
set "job3kyw=%job3kyw%"
set vartest1=test1
set vartest2=test2
set vartest3=test3
set vartest4=test4
set vartest5=test5
set vartest5=test6
set vartest6=test7
)

Essentially, what should happen is that the entire last line is parsed from the ENDLOCAL through to the closing parenthesis. It is THEN executed, but with the %var%s replaced by their values at the time the endlocal&(etc) was encountered. & allows multiple commands on one physical line...

Upvotes: 2

Related Questions