We help IT Professionals succeed at work.

bat file to parse text

chetan1981
chetan1981 asked
on
1,981 Views
Last Modified: 2008-02-07
Hi,

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 ..

MSSQLSERVER\$UAT\$DEV\$STG

Any inputs are greatly appreciated. Thanks

Comment
Watch Question

Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
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
)
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

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

chetan1981,
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
Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

MSSQLSERVER\0UAT\0DEV\0STG

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

Thanks for helping..
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Works gr8...thx!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.