Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

https://stackoverflow.com/a/19988069/5079799

https://stackoverflow.com/questions/63498946/pass-named-parameters-to-batch-script-with-special-characters/63509485#63509485

Note: This allows you to use as many variables as you want, no need to worry about only 9!

@echo off
setlocal enabledelayedexpansion
CLS

IF "%~1"=="" GOTO endparse
set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
   set "argVec[!argCount!]=%%~x"
   set %%x
)
:endparse
ECHO %A%
ECHO %B%
ECHO %C%
ECHO %D%
ECHO %E%
ECHO %F%
ECHO %G%
ECHO %H%
ECHO %I%
ECHO %J%
ECHO %K%
PAUSE

This was for handling WILDCARDS in the variable `FILEPATH` and then finding files:

@ECHO OFF
CLS
::SET Named Arguments
SET argCount=0
for %%x in (%*) do (
   SET /A argCount+=1
   SET "argVec[!argCount!]=%%~x"
   SET %%x
)

::Wildcards in FilePath?
(SET "%~1" & SET "%~2" & SET "%~3" & SET "%~4" & SET "%~5" & SET "%~6" & SET "%~7" & SET "%~8" & SET "%~9")>nul
SET argCount=0
IF DEFINED FILEPATH (
  FOR %%x IN ("%FILEPATH%") DO (
     SET /A argCount+=1
     SET "argVec[!argCount!]=%%~x"
 )
 CALL :FindFileWildCard "%FILEPATH%" FILEPATH
) ELSE (
   ECHO No "FILEPATH=C:\path\print.doc" Defined!
   PAUSE
   GOTO:EOF
)

ECHO %FILEPATH%
ECHO %VAR%
ECHO %VAR2%
ECHO %VAR3%
ECHO %VAR4%
ECHO %VAR5%
ECHO %VAR6%
ECHO %VAR7%
ECHO %VAR8%
ECHO %VAR9%
ECHO %VAR10%
GOTO :EOF

::Functions
:FindFileWildCard
::Does Path contain WildCards?
ECHO "%~1" | FIND /i "*" >nul
IF %ERRORLEVEL% EQU 0 (
 FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
 SET %2=%%F
 EXIT /B
 )
)
ECHO "%~1" | FIND /i "?" >nul
IF %ERRORLEVEL% EQU 0 (
 FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
 SET %2=%%F
 EXIT /B
 )
)
EXIT /B

:EOF


Test:

C:\tmp\namedargs.cmd "A=1" "B=2" "C=3" "D=4" "E=5" "F=6" "G=7" "H=8" "I=9" "J=10" "K=11"

Result:

1
2
3
4
5
6
7
8
9
10
11
Press any key to continue . . .
  • No labels