Solved

How to test for missing parm with batch FOR command

Posted on 2004-08-30
2
174 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
[X]
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
2 Comments
 
LVL 85

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
If you are IT support and need to work after hours to resolve customer issues then here are a few tips on how to handle after hours support
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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