2

I have a shell script (.sh) that works in Unix but I'd like to convert it into a Windows batch file (.bat):

cat >flog.ctl <<_EOF
LOAD DATA
INFILE '$1.log' "str ';'"
APPEND INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
filename constant '$1'
,num  char
,data char
)
_EOF

sqlldr <username>/<password>@<instance> control=flog.ctl data=$1.log

I'm not too knowledgable on batch files but if an answer is some hints rather than a complete solution then I'm sure I'll muddle through. This is to help with an answer to another question here.

2 Answers 2

3

Windows batch files do not support inlining data this way. You'll have to

ECHO firstLine > flog.ctl
ECHO additionalLine >> flog.ctl

Windows batch files denote variables in ECHO statements using % signs, i.e. %1%.

So you're resulting file would be something like:

@ECHO OFF
ECHO LOAD DATA > flog.ctl
ECHO INFILE '%1%.log' "str ';'" >> flog.ctl
ECHO APPEND INTO TABLE flog >> flog.ctl
ECHO fields terminated by '=' TRAILING NULLCOLS >> flog.ctl
ECHO ( >> flog.ctl
ECHO filename constant '%1%' >> flog.ctl
ECHO ,num  char >> flog.ctl
ECHO ,data char >> flog.ctl
ECHO ) >> flog.ctl

sqlldr <username>/<password>@<instance> control=flog.ctl data=%1%.log 
Sign up to request clarification or add additional context in comments.

6 Comments

Is there a difference between %1% and %1 as used in @voithos's code? I've tested both and both work.
@JohnDoyle: The second % is not necessary. I think James might be used to using two signs to delimit variables in batch files, like shown here. But the "correct" way to reference command line arguments, as far as I know, is to use a single %.
Hi, @JohnDoyle. Neither way is more correct than the other. It's true that you can always reference the argument variables 0-9 with a single % because they get special treatment when expanded. I make always using the second % a habit because when using named variables, ECHO %Asometext%B will have the wrong output and ECHO %A%sometext%B% will not. Give it a try some time and you'll see what I mean.
@voithos See my comment above.
@James: I still prefer the single %. The double % works because command-line arguments get mapped to environment variables of the same name. I like differentiating between arguments and environment variables, and I know of at least one reason to do so: %* can be used a shorthand for "all the arguments", whereas %*% is an error, and will just give *. In short, even though you can use either one most of the time, they're not equivalent.
|
2

There are some complicated solutions as far as the inlining goes, but as was already mentioned, a simple solution would be to use echo.

@echo off

echo LOAD DATA > flog.ctl
echo INFILE '%1.log' "str ';'" >> flog.ctl
echo APPEND INTO TABLE flog >> flog.ctl
echo fields terminated by '=' TRAILING NULLCOLS >> flog.ctl
echo ( >> flog.ctl
echo filename constant '%1' >> flog.ctl
echo ,num  char >> flog.ctl
echo ,data char >> flog.ctl
echo ) >> flog.ctl

sqlldr <username>/<password>@<instance> control=flog.ctl data=%1.log

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.