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
Solved

How to test for missing parm with batch FOR command

Posted on 2004-08-30
2
166 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 84

Accepted Solution

by:
oBdA earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Win 2000 Pro - RDP Connection 2008 R2 Terminal Service 4 539
Recovering backup .Qic files on Windows 7 6 2,267
Windows WEb Server sp2 13 520
Windows 2000  Domain controller 3 500
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…
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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