bat file to parse text

Posted on 2007-10-03
Medium Priority
Last Modified: 2008-02-07

I need some help with a little DOS scripting, any help is much appreciated. I am writing a BAT script that will parse a text file for sql instance names and loop through the names to run an osql query. The input text is in this format with SQL instances separated by '\$', once it picks the instance name it will call another bat file 'runsql.bat'  to run the osql ..


Any inputs are greatly appreciated. Thanks

Question by:chetan1981
  • 2
  • 2
  • 2
LVL 97

Expert Comment

by:Lee W, MVP
ID: 20009797
Try this (allows for up to 8 instances):

for /f "tokens=1,2,3,4,5,6,7,8,9 delims=\$" %%a in (textfilename.txt) do (
   If "%%b" == "" call runsql.bat %%a %%b
   If "%%c" == "" call runsql.bat %%a %%c
   If "%%d" == "" call runsql.bat %%a %%d
   If "%%e" == "" call runsql.bat %%a %%e
   If "%%f" == "" call runsql.bat %%a %%f
   If "%%g" == "" call runsql.bat %%a %%g
   If "%%h" == "" call runsql.bat %%a %%h
   If "%%i" == "" call runsql.bat %%a %%i
LVL 85

Expert Comment

ID: 20010205
sorry, but that won't work; it'll start (8-NumberOfInstances) times "call runsql.bat MSSQLSERVER"

slightly different approach, no (reasonable) limit to the number of instances: it'll read the string from the file specified in the variable "InputFile", strip the leading MSSQLSERVER from it, then start runsql.bat for the different instances with the instance name as first argument.
If you need the MSSQLSERVER as well, replace the line starting with for /f with this:
for /f "delims=" %%a in ('type "%InputFile%"') do set InstanceList=%%a

@echo off
set InputFile=test.txt
for /f "tokens=1* delims=\$" %%a in ('type "%InputFile%"') do set InstanceList=%%b
for %%a in (%InstanceList:\$= %) do call runsql.bat %%a
LVL 97

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 600 total points
ID: 20010320
My scripting is rusty, the == should have been NEQ instead:

for /f "tokens=1,2,3,4,5,6,7,8,9 delims=\$" %%a in (textfilename.txt) do (
   If "%%b" NEQ "" call runsql.bat %%a %%b
   If "%%c" NEQ "" call runsql.bat %%a %%c
   If "%%d" NEQ "" call runsql.bat %%a %%d
   If "%%e" NEQ "" call runsql.bat %%a %%e
   If "%%f" NEQ "" call runsql.bat %%a %%f
   If "%%g" NEQ "" call runsql.bat %%a %%g
   If "%%h" NEQ "" call runsql.bat %%a %%h
   If "%%i" NEQ "" call runsql.bat %%a %%i

Though oBdA's should not have a limit of 8 instances as mine.
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database


Author Comment

ID: 20013151
Anyway to use \0 as a delimiter instead of \$ in case the string is


I tried to replace with \$ with \0 but then it split wherever it saw \ or 0, not '\0' as I wanted.

Thanks for helping..
LVL 85

Accepted Solution

oBdA earned 1400 total points
ID: 20013229
My solution should work with \0 as well, provided you change the "\$" in both lines where it appears:

@echo off
set InputFile=test.txt
for /f "tokens=1* delims=\0" %%a in ('type "%InputFile%"') do set InstanceList=%%b
for %%a in (%InstanceList:\0= %) do call runsql.bat %%a

Author Comment

ID: 20017540
Works gr8...thx!

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 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