How do you read a filename and substring it to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sanders_2503
Posts: 3
Joined: 05 Oct 2011 11:33

How do you read a filename and substring it to variables

#1 Post by Sanders_2503 » 05 Oct 2011 11:58

I am very new to DOS programming but have been doing Unix for a while.

This may sound a very basic problem to the experts, so please excuse my naivity.

Heres the problem -

Say I have 500 files in a folder with their file names in 1-12345-12345678 format. I got to read each file names to a single variable and pass it on to a pl/sql script. The pl/sql script will then update a table on sql server based on this variable (one of the columns of the table is a primary key based on this file name).

My questions are -
1. How do i read each file name to a variable in my batch script and then run it into a loop for as many files as present. (basically executing the pl/sql script as many times as there are number of files).
2. How do i pass the variable to the call for executing the pl/sql script from my batch script.

I appreciate your time and inputs on this and Thanks in advance for any response.

Thanks,
Sanders.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How do you read a filename and substring it to variables

#2 Post by aGerman » 07 Oct 2011 15:06

Um, pl/sql sounds like Oracle. Are you sure you could use Windows Batch in this environment?

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How do you read a filename and substring it to variables

#3 Post by dbenham » 09 Oct 2011 14:23

Sure aGerman - sqlplus is a command line utility that works great in Windows Batch.

Sanders - There are many variations for reading file names from a directory into variables, almost always using some form of the FOR command.

Here is the simplest form:

Code: Select all

for %%F in (*) do sqlplus userName/password@instance @script.sql %%F

The asterisk wild card is expanded to all file names in the current working directory using the FOR loop, with one name per pass in the %%F variable. (Note - the % is doubled up for %%F only within a script. If executed directly from the command line then you would only use a single percent before the F).

Presumably your sql script will have an EXIT or QUIT command so that it terminates, thus enabling the FOR loop to go on to the next iteration.

From an interactive command prompt you can enter HELP FOR to get help on the many variations and options available to the FOR command.

Dave Benham

Post Reply