How not to hard coded in order to accommodate any number variations of the data files...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

How not to hard coded in order to accommodate any number variations of the data files...

#1 Post by goodywp » 05 Dec 2017 10:37

Hi all,

Due to my limit, I had following cases which made the code as hard coded instead of open for variations.

The logic is the following

1) pre-conditions: based upon the reading of a file (one script), I got some data output as data!n!.txt as below:
data1.txt
data2.txt
data3.txt
data4.txt (in this case 4, but may be more than 4 or less than 4)

2) I need to process these above data files to be used for the following script which is only for the first data1.txt processing, and I made 4 scripts similar to this one to be able to process data2.txt, data3.txt and data4.txt

Code: Select all

@echo off
cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp

if exist echo_data_1.txt (del echo_data_1.txt)
if exist echo_sch_1.txt (del echo_sch_1.txt)

for /f "tokens=1-9 delims=," %%A in (data1.txt) do call :EachLine %%A %%B %%C %%D %%E %%F %%G %%H %%I
 
:EachLine 
set a=%1
set b=%3
set c=%4
set c=%c:"=%
set d=%5
set e=%6
set f=%7
 
ECHO %d% >>echo_data_1.txt
ECHO %b% >>echo_data_1.txt
ECHO %c% >>echo_sch_1.txt

if %a% NEQ 0 (ECHO %1 >>echo_data_1.txt)
if %e% NEQ 0 (ECHO %6 >>echo_data_1.txt)
if %f% NEQ 0 (ECHO %7 >>echo_data_1.txt)
3) further processing the above data and got output as data_1.txt, data_2.txt, data_3.txt, data_4.txt in these cases
4) Now I would like these data files to be used in the below code

Code: Select all

@ECHO OFF
set [0] = data_1.txt
set [1] = data_2.txt
set [2] = data_3.txt
set [3] = data_4.txt

for /F "tokens=2 delims==" %%n in ('set [') do (
	cd C:\auto_pkg_build\Scripts\scheme_replace
	del pkg_data.txt
	cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data
	copy %%n C:\auto_pkg_build\Scripts\scheme_replace
	timeout /t 2 
	cd C:\auto_pkg_build\Scripts\scheme_replace
	ren %%n pkg_data.txt	
	timeout /t 1 
	call C:\auto_pkg_build\Scripts\scheme_replace\get_variable.cmd
	timeout /t 1 
	call C:\auto_pkg_build\Scripts\scheme_replace\pre_sch_extract.cmd
	timeout /t 1
	call C:\auto_pkg_build\Tools\PACKAGER\list_comp_sch.cmd
	timeout /t 1
	call C:\auto_pkg_build\Tools\PACKAGER\md_S3S_extract.cmd
	timeout /t 1
	cd C:\auto_pkg_build\Tools\PACKAGER
)
How not to hard coded in these cases, the two scripts I pasted in this post...? because the number of data files could be any, say 3, 4 in this case, or 5 or 6...

So the above code can accommodate any number variations of the data files...
I wish I could use only one scripts to do this job but due to the data processing is sort of complex....

Thanks
Last edited by goodywp on 05 Dec 2017 13:53, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How not to hard coded in order to accommodate any number variations of the data files...

#2 Post by penpen » 05 Dec 2017 11:20

It might be my fever speaking out of me, but i think you have given yourself the solution to your issues, but
it seems you don't see it:
goodywp wrote:
05 Dec 2017 10:37
data!n!.txt
Just proceed with using "data!n!.txt", "data_!n!.txt", ... as you Need it in these scripts; just make sure you never overwrite the environment variable n with unwanted values.

penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How not to hard coded in order to accommodate any number variations of the data files...

#3 Post by Squashman » 05 Dec 2017 11:27

Why can't you use the FOR command to get all of the DATA_ files for processing?

Also you have some bad coding mistakes.

Code: Select all

set c=%4
set c=%c:"=%
If you read the help for the FOR and CALL commands you can remove the quotes by using the TILDE.

Code: Select all

set c=%~4
Also this is a bad coding practice.

Code: Select all

set [0] = data_1.txt
set [1] = data_2.txt
set [2] = data_3.txt
set [3] = data_4.txt
You are essentially assigning a space to the variable name and a space to the variable content. Remove the spaces if you want to maintain some sanity.

Code: Select all

set [0]=data_1.txt

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How not to hard coded in order to accommodate any number variations of the data files...

#4 Post by goodywp » 05 Dec 2017 12:08

penpen wrote:
05 Dec 2017 11:20
It might be my fever speaking out of me, but i think you have given yourself the solution to your issues, but
it seems you don't see it:
goodywp wrote:
05 Dec 2017 10:37
data!n!.txt
Just proceed with using "data!n!.txt", "data_!n!.txt", ... as you Need it in these scripts; just make sure you never overwrite the environment variable n with unwanted values.

penpen
The environment variable n is changing from 1~4 in the above cases, so how can I use the same environment variable n in the different scripts unless I have a condition FOR to loop all the data files in each script ... how to code such FOR loop in this case... :oops:

FOR /L %%parameter IN (start,step,end) DO command (here the end is unknown to me how to do the loop in this case??)

Thanks

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How not to hard coded in order to accommodate any number variations of the data files...

#5 Post by penpen » 05 Dec 2017 13:21

I hope this example solves
"test.bat":

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
for /l %%n in (1, 1, 9) do (
	rem create data files, or whatever you need
)

:: after that just add another loop processing the needed file (loop over all values of %%n, set n to actual value, and process files)
for /l %%n in (1, 1, 9) do (
	set "n=%%~n"
	call test2.bat
)
endlocal
goto :eof
"test2.bat":

Code: Select all

echo Processing: "data!n!", "data_!n!", ... finished.
Result:

Code: Select all

Z:\>test.bat
Processing: "data1", "data_1", ... finished.
Processing: "data2", "data_2", ... finished.
Processing: "data3", "data_3", ... finished.
Processing: "data4", "data_4", ... finished.
Processing: "data5", "data_5", ... finished.
Processing: "data6", "data_6", ... finished.
Processing: "data7", "data_7", ... finished.
Processing: "data8", "data_8", ... finished.
Processing: "data9", "data_9", ... finished.
penpen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How not to hard coded in order to accommodate any number variations of the data files...

#6 Post by goodywp » 05 Dec 2017 13:49

penpen wrote:
05 Dec 2017 13:21
I hope this example solves
"test.bat":

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
for /l %%n in (1, 1, 9) do (
	rem create data files, or whatever you need
)

:: after that just add another loop processing the needed file (loop over all values of %%n, set n to actual value, and process files)
for /l %%n in (1, 1, 9) do (
	set "n=%%~n"
	call test2.bat
)
endlocal
goto :eof
"test2.bat":

Code: Select all

echo Processing: "data!n!", "data_!n!", ... finished.
Result:

Code: Select all

Z:\>test.bat
Processing: "data1", "data_1", ... finished.
Processing: "data2", "data_2", ... finished.
Processing: "data3", "data_3", ... finished.
Processing: "data4", "data_4", ... finished.
Processing: "data5", "data_5", ... finished.
Processing: "data6", "data_6", ... finished.
Processing: "data7", "data_7", ... finished.
Processing: "data8", "data_8", ... finished.
Processing: "data9", "data_9", ... finished.
penpen
I tried this but give me:
The syntax of the command is incorrect.

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
for /l %%n in (1, 1, 9) do (
	for /f "tokens=1-9 delims=," %%A in (data!n!.txt) do call :EachLine %%A %%B %%C %%D %%E %%F %%G %%H %%I
 
:EachLine 

set a=%1
set b=%3
set c=%4
set c=%c:"=%
set d=%5
set e=%6
set f=%7
 

ECHO %d% >>echo_data_!n!.txt
ECHO %b% >>echo_data_!n!.txt
ECHO %c% >>echo_sch_!n!.txt

if %a% NEQ 0 (ECHO %1 >>echo_data_!n!.txt)
if %e% NEQ 0 (ECHO %6 >>echo_data_!n!.txt)
if %f% NEQ 0 (ECHO %7 >>echo_data_!n!.txt)

)

endlocal
goto :eof

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How not to hard coded in order to accommodate any number variations of the data files...

#7 Post by penpen » 06 Dec 2017 03:33

I assume that the (actually) last closing parenthesis (')') is at the wrong position.
You may want to close the first for loop in the 5th line of the code you posted.

penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How not to hard coded in order to accommodate any number variations of the data files...

#8 Post by Squashman » 06 Dec 2017 10:18

You are essentially trying to CALL a Label within a FOR CODE BLOCK so it will essentially try to execute it twice.

Post Reply