Solved

Storing information from input file in a batch file variable

Posted on 2012-04-05
4
431 Views
Last Modified: 2012-04-06
Hello,

Let my preface this by saying I have no experience with writing batch files at all so go easy on me. What I'm currently trying to do is read a single line from a .txt file and store that line into a variable in a batch file.

I'm using the following syntax to try to set the variable value:

set /p varname=<tempres.txt

I'm then trying to append the value of this variable to an output file name (within an osql statement) as follows:

osql -E -S .\[i]instance,port[/i] -d msdb -h-1 -n -w1000 -i "D:\batchtest\tables.sql" -o "D:\batchtest\newsqlexport_D%date:~-4,4%%date:~-7,2%%date:~-10,2%_T%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%_0%_0%varname%"

Open in new window


The output file creates successfully and has everything in the file name that it should up to the "_0". What am I doing wrong that the value of varname isn't being appended to the file name?

Thanks in advance for the help!
0
Comment
Question by:bluegrasscell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 37815852
mybatch.cmd
--- start of batch --
set  /p varname= %1
osql -E -S .\instance,port -d msdb -h-1 -n -w1000 -i "D:\batchtest\tables.sql" -o "D:\batchtest\newsqlexport_D%date:~-4,4%%date:~-7,2%%date:~-10,2%_T%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%_0%_0%varname%"
--- end of batch --
mybatch < testme.txt
0
 
LVL 38

Accepted Solution

by:
BillDL earned 500 total points
ID: 37815917
Hi bluegrasscell

The   _0   (that's a zero) you are referring to has been used twice at the end of that long command which creates the file name, and it has a % sign that seems to be misplaced:

_0%_0%varname%

Here's the complete section that concerns the creation of the file name, and it has been split into each different element:

newsqlexport_D
%date:~-4,4%
%date:~-7,2%
%date:~-10,2%
_T
%time:~0,2%
%time:~3,2%
%time:~6,2%
%time:~9,2%
_0
%_0 <-------------------------------- *****
%varname%

That % symbol in   %_0   shouldn't really be there because it does nothing.

There are lots of ways to get the current date from the built-in %DATE% variable, but if these commands executed in a "DOS" window:
echo _D%date:~-4,4%%date:~-7,2%%date:~-10,2%
echo _T%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
accurately reflects what you need, then you could make the final command line that creates the new file a little shorter like this:
set OutFile=D:\batchtest\newsqlexport
set CurrDate=%date:~-4,4%%date:~-7,2%%date:~-10,2%
set CurrTime=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%

osql <command params here> "%OutFile%_D%CurrDate%_T%CurrTime%_0%varname%"

Open in new window

That's just aesthetic though.

There are also quite a few ways to parse a text file.  You say that the text file has just one line?

Try this and see what happens.  If it correctly displays the command on the screen, then remove the   echo  from the start of the osql command:
@echo off

set TmpFile=tempres.txt
set InFile=D:\batchtest\tables.sql
set OutFile=D:\batchtest\newsqlexport
set CurrDate=%date:~-4,4%%date:~-7,2%%date:~-10,2%
set CurrTime=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
set OsqlParams=-E -S .\[i]instance,port[/i] -d msdb -h-1 -n -w1000 -i 

if not exist "%TmpFile%" (
    echo %TmpFile% not found.
    echo Press any key to quit ...
    pause > nul
    goto :EOF
)

if not exist "%InFile%" (
    echo %InFile% not found.
    echo Press any key to quit ...
    pause > nul
    goto :EOF
)

for /f "tokens=*" %%A in ('type %TmpFile%') do set varname=%%A

echo osql %OsqlParams% "%InFile%" -o "%OutFile%_D%CurrDate%_T%CurrTime%_0%varname%"

echo.
pause

Open in new window

NOTE: The above will not work if the "tempres.txt" file contains more than one line.
NOTE: I have not tested this.

Bill
0
 

Author Closing Comment

by:bluegrasscell
ID: 37816915
I didn't test the first answer posted, but this one went above and beyond. Not only did my original question get answered, but I got a few extra tidbits for making the file better overall.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 37817145
Thank you bluegrasscell.  I hope it does work for you.

I don't have the OSQL program to test this with, so all I could do was create a text file with one line of text (containing spaces, backslashes, forward slashes, and some other characters that might prove to be problematic down the line), and then look at the echoed command line to see if the variables were expanded accurately again at runtime.

Setting the variables up at the top of a batch file is really just a good way of having a batch file where you only have to change a value in one line rather than change it in several places throughout a large and complex batch file.  In your case you just have one command, so it's hardly a necessity to do this.  It might prove useful if you ever extend that batch file, or certainly for any future ones you may write that use a file path, text string, or something else  which is repeated several times and would be better storing as a variable for later re-use.

If you wondered why I used   set CurrDate=   and   set CurrTime=   as opposed to Date and Time, it's because you should never use the names of any of system or user variables that already exist.  You can see these by opening a CMD window and just typing   SET

Be aware that by setting the %CurrTime% variable at the start of the batch file, the time at that moment is stored.  If the batch file did other things that took a few seconds or longer before getting to the point where the %CurrTime% variable is used, and if this "wrong time" could be important, then it would be better to get the time at the point of execution rather than the one already stored.  You can do this either by not setting the %CurrTime% variable at the start of the batch file and just using the variable as the command is executed, or you could use something called Delayed Variable Expansion
http://ss64.com/nt/setlocal.html
http://ss64.com/nt/delayedexpansion.html
whereby you would use !CurrTime! instead of %CurrTime%.  That's another topic though, and I would assume that a second or two variance in the name of the file will not really matter.

There is just one thing that I would normally tend to specify in a batch file.  In this case it is assumed that the file "tempres.txt" is in the same directory as the batch file, and therefore does not need to be used with the path to it.  I usually specify the path rather than leave it to chance, and to get the current directory of the batch file wherever it happens to be, I use the variable  %~dp0  (that's a zero) which is just a modification of   %0

%0 will always contain the fully qualified path to the batch file when it runs.
%1 (as was being used in ve3ofa's suggestion) will contain the first parameter passed to the batch file
%2 will contain the 2nd parameter passed to the batch file.

By inserting a  ~  after the % you can then use a number of variable modifiers which you will see if you open a CMD window and type   FOR /?   and then page down right to the end.  In this case  %~dp0  will be expanded to only the Drive Letter (the dmodifier) and the Path (the p modifier) of the batch file, but without the file name of the batch file.  Unfortunately it is suffixed by a trailing backslash, but this can be eliminated in exactly the same way as you modified your %Date% and %Time% variables by choosing where to start and how many characters to include (in this case start at the first character from the left of what's stored in the variable and move in one from the rightmost character):

set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%
set TmpFile=%CurrDir%\tempres.txt

It leaves less to chance if there happened to be a file of that name already in some other recognised path.

End of tutorial ;-)
Glad you found the extra little bits of interest.

Bill
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My purpose is to describe the basic concepts of virtual memory as implemented in a modern Windows-based operating system. I will also describe the problems inherent in older systems and how virtual memory solves them. The dark ages - before virtu…
This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question