Solved

How to test for missing parm with batch FOR command

Posted on 2004-08-30
2
157 Views
Last Modified: 2010-04-13
I have a batch script which uses the FOR command to read a file of parameters.  The parameters are 2 per line and separated by a semicolon.  How can determine if a semicolon was not used?  Here is what I've tried so far:
for /F "tokens=1,2,* skip=1 delims=;" %%A in (%1) do (if %%B == "" echo missing semicolon)  It would be nice if I could also distiguish between a missing semicolon and a missing second parameter.  

0
Comment
Question by:rdclk23
2 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Basically, there's not too much of a difference between a missing delimiter and a missing argument, but the check is possible.
Note that I used "in 'type %1'" instead of "in (%1)"; this prevents problems if %1 contains spaces.
And in case you see the "call :<label>" construction for the first time: this is the batch version of a subroutine call. Enter "help call" for more information.

@echo off
setlocal
for /f "skip=1 delims=" %%a in ('type %1') do (
  set Line=%%~a
  call :process
)
goto leave

:process
if "%Line%"=="%Line:;=%" (
  echo Missing delimiter: %Line%
  goto :eof
)
for /f "tokens=1,2 delims=;" %%a in ("%Line%") do (
  set Arg1=%%~a
  set Arg2=%%~b
)
if "%Arg2%"=="" (
  echo Missing argument:  %Line%
  goto :eof
)
echo Correct arguments: [%Arg1%][%Arg2%]
:: *** Handle your arguments here:
:: ...
goto :eof


:leave
0
 

Author Comment

by:rdclk23
Comment Utility
Thanks!  Worked Great!  It took me a while to figure out what "%Line:;=" was doing, but I now see that you are using string replacement.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now