Solved Analyze arguments - batch file

Hewlett-packard / Pavilion hpe
April 5, 2015 at 05:55:01
Specs: Windows 7 Professional x64, 3.6GHz 16GB
I'm working on a batch file that will recieve arguments from the command line. I figured I should put some handlers if invalid or too few/many arguments are provided. I came up with this:

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION
SET CNT=1

:CHECKARGS

:: e.g. set arg1 to %1 (argument 1)
SET ARG!CNT!=%!CNT!

:: Idunno why I put it here, but I have a tingly feeling I need it
IF !ARG%CNT%!==!CNT! (
GOTO MAIN
)
ECHO !ARG%CNT%!
SET /A CNT=%CNT%+1
GOTO CHECKARGS

:MAIN
ECHO ARGUMENTS RECIEVED

PAUSE > NUL

The problem is that it won't process %1 as an argument, but instead processes it as a string.
How would I fix this?

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ


See More: Analyze arguments - batch file

Report •

#1
April 5, 2015 at 10:00:07
Here a more efficient way ro perform your task

@echo off & setlocal EnableDelayedExpansion
set cnt=1
:LOOP
  set arg_!cnt!=%1
  if defined arg_!cnt! (set /A cnt+=1 & shift & goto :LOOP)
set /A cnt-=1
echo.
echo.Parameter(s): !cnt!
for /L %%k in (1 1 !cnt!) do echo.  arg_%%k : !arg_%%k!


Report •

#2
April 5, 2015 at 15:11:18
I don't have the opportunity to test this before tuesday. I'll pop back and mark this as solved then, if it works.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#3
April 6, 2015 at 04:07:43
✔ Best Answer
Well, I tested my script under Windows XP SP3 and, as you can see, I had to code %%k instead of %k as batch theory teaches about FOR loops to force it to run.

So here another equivalent solution perfectly according to batch theory

@echo off & setlocal EnableDelayedExpansion
set cnt=1
:LOOP1
  set arg_!cnt!=%1
  if defined arg_!cnt! (set /A cnt+=1 & shift & goto :LOOP1)
set /A cnt-=1
echo.
echo.Parameter(s): !cnt!
:LOOP2
  if !cnt! gtr 0 (echo.  arg_!cnt!: !arg_%cnt%! & set /A cnt-=1 & goto :LOOP2)


Report •
Related Solutions


Ask Question