[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to test for missing parm with batch FOR command

Posted on 2004-08-30
2
Medium Priority
?
185 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 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 11936441
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
ID: 12048091
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this article I will be showing you how to subnet the easiest way possible for IPv4 (Internet Protocol version 4). This article does not cover IPv6. Keep in mind that subnetting requires lots of practice and time.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

865 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