Batch script to create files

I will like to creare a batch files that takes in input  2 tokens to be use as a file name and column name and create a script using the tokens

The tokens can be in a file (fnames.txt) as such:
AAA;COLA
AAA;COLB
BBB;COLA
CCC;COLA

The file created should have the following contents :

drop ind TABLE_NAME_COLUMN_IDX

CREATE UNIQUE INDEX TABLE_NAME_COLUMN_IDX
                 ON SCHEMA.TABLE_NAME  (COLUMN)
TABLESPACE INDEX_TXX;

The filename to be created should be the first token with an extension 'sql'

I have a script that does something similar but it functions when the in the filename file there is a single word on a line using the following instrution to create the files

for /f "delims=" %%f in (fnames.txt) do >"%dir%\%%~f.%ext%" (
  echo.XXXX
LVL 1
diteps06Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
So in the case of your 4 line example, would three files be created, AAA.sql, BBB.sql ands CCC.sql?

Where do you want to use the second value on each line of the text file in the generated file?

And should INDEX_TXX always be that literal string, or does the XX need to be an incrementiong number?

Are "TABLE_NAME" and "COLUMN" supposed to replaced in the generated output?

It would be very helpful if you provided an example output set of files for the example input file you provided.

It's easy enough to split the two items from each input line, much the same as you already did it, just do:

for /f "delims=; tokens=1-2" %%A in (fnames.txt) do (
  echo.%%A >>"%dir%\%%~A.%ext%"
  echo.%%B >>"%dir%\%%~A.%ext%"
)

Open in new window

~bp
0
oBdACommented:
Something like that? The files will be created in the same folder as fnames.txt
@echo off
setlocal enabledelayedexpansion
set InFile=fnames.txt
for %%a in ("%InFile%") do set InFileFolder=%%~dpa
for /f "tokens=1,2 delims=;" %%a in ('type "%InFile%"') do (
	set OutFile=%InFileFolder%%%a.sql
	 >"!OutFile!" echo drop ind TABLE_NAME_%%b_IDX
	>>"!OutFile!" echo.
	>>"!OutFile!" echo CREATE UNIQUE INDEX TABLE_NAME_%%b_IDX
	>>"!OutFile!" echo 				 ON SCHEMA.TABLE_NAME  ^(%%b^)
	>>"!OutFile!" echo TABLESPACE INDEX_TXX;
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
diteps06Author Commented:
Thanks guys you have enlighten me to the solution.
The main problem was to get and manipulate the tokens
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.