Read text file and output to another file - depending on batch file logic

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Read text file and output to another file - depending on batch file logic

#1 Post by SIMMS7400 » 12 Apr 2016 18:08

Folks -

I have a need to do the following:

Read the first line of a text file, grab that value, and then spool to another file as the value of the variable (shown below), based off conditions specified in the batch file and then go to 2nd line of text file and so on and so forth. The amount of lines in the text file will vary each week, so I'd rather not hard code.

Here is my process and its working fine for the first line of the file. Now I need help looping back up and performing the same process for each line of the text file.

NOTE: Each line of the text file is only 1 value/string. For instance, it looks like this:

Value1
Value2
Value3
Value4

The below code works, but only gets the first line of the text file and outputs. I need to loop back through the remaining lines of the text file.

Code: Select all



@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (00_TableNames.txt) do (
if !myvar!'==' set myvar=%%a
)
echo myvar is set to !myvar!
echo --table creation > "00_%myvar%.sql"
echo SET WRAP OFF; >> "00_%myvar%.sql"
echo SET NEWPAGE NONE; >> "00_%myvar%.sql"
echo SET PAGESIZE 0; >> "00_%myvar%.sql"
echo SET SPACE 0; >> "00_%myvar%.sql"
echo SET LINESIZE 16000; >> "00_%myvar%.sql"
echo SET ECHO OFF; >> "00_%myvar%.sql"
echo SET FEEDBACK OFF; >> "00_%myvar%.sql"
echo SET VERIFY OFF; >> "00_%myvar%.sql"
echo SET HEADING OFF;" >> "00_%myvar%.sql"
echo SET TERMOUT OFF; >> "00_%myvar%.sql"
echo SET TRIMOUT ON; >> "00_%myvar%.sql"
echo SET TRIMSPOOL ON; >> "00_%myvar%.sql"
echo SET COLSEP ','; >> "00_%myvar%.sql"
echo spool 00_%myvar%.txt >> "00_%myvar%.sql"
echo select * from %myvar%; >> "00_%myvar%.sql"


Tablename should be replaced by each value of the text file. Therefore, if there are 24 values (lines), there should be 24 separate files with the following values in them:

--table creation
SET WRAP OFF;
SET NEWPAGE NONE;
SET PAGESIZE 0;
SET SPACE 0;
SET LINESIZE 16000;
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET TERMOUT OFF;
SET TRIMOUT ON;
SET TRIMSPOOL ON;
SET COLSEP ',';
spool 00_VALUE1.txt
select * from VALUE1;


Thank you!

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Read text file and output to another file - depending on batch file logic

#2 Post by Aacini » 13 Apr 2016 09:05

Code: Select all

@echo off

for /F %%a in (00_TableNames.txt) do (

   (
   echo --table creation
   echo SET WRAP OFF;
   echo SET NEWPAGE NONE;
   echo SET PAGESIZE 0;
   echo SET SPACE 0;
   echo SET LINESIZE 16000;
   echo SET ECHO OFF;
   echo SET FEEDBACK OFF;
   echo SET VERIFY OFF;
   echo SET HEADING OFF;
   echo SET TERMOUT OFF;
   echo SET TRIMOUT ON;
   echo SET TRIMSPOOL ON;
   echo SET COLSEP ',';
   echo spool 00_%%a.txt
   echo select * from %%a;
   ) > "00_%%a.sql"

)

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Read text file and output to another file - depending on batch file logic

#3 Post by SIMMS7400 » 13 Apr 2016 16:39

That is perfect, and simple!

Thank you very much!!!

Post Reply