6

Do someone knows how to add the action to be triggered when calling my batch file with the /? argument ? I've always used the -h to display usages, but for once I need my -h arg for something else.

EDIT : Actually I've tried by parsing attributes like this

for %%i in (%*) do ....

But the /? argument was skipped, I'll try yours solutions to see if it's different.

Btw, why when you parse %%i the /? args is skipped ?

4 Answers 4

6

The /? seems to be simply skipped by the for %%i in (%*) but it's the wildcard functionality of the for-loop, it tries to find a file that matches /? which will fail.

You can not use ? or * in a "normal" for-loop, without modifying the result.

You could use the SHIFT command to access all your parameters.

:parameterLoop
if "%~1"=="/?" call :help
if "%~1"=="-h" call :help
if "%~1"=="-o" call :other
shift
if not "%~1"=="" goto :parameterLoop

If you also want to display the selected option, you got a problem with the echo command, as this will normally show the help instead of /?.

You can avoid this by using echo(%1 instead of echo %1.

Sign up to request clarification or add additional context in comments.

Comments

4

You check your command line arguments (%1, %2 etc) against the /? string and if true then print help using ECHO command. For example,

@ECHO OFF
IF "%1"=="/?" (
    ECHO "Help Line 1"
    ECHO "Help Line 2"
) ELSE (
    ECHO "Do Your Action"
)

Comments

1

You could try this:

@echo off
if "%1"=="/?" goto print_help
goto normal_start

:print_help
echo Here is your help
goto end

:normal_start
echo I'm working

:end

Comments

0

Here's an actual use case for those visuals out there!

@ECHO OFF

:parameterLoop
IF /I "%1"=="/install" GOTO install
IF /I "%1"=="/uninstall" GOTO uninstall
IF /I "%1"=="/repair" GOTO repair
IF /I "%1"=="" (
    ECHO.
    ECHO Please use the following commands to proceed:
    ECHO.
    ECHO Use /INSTALL to setup the software
    ECHO.
    ECHO Use /UNINSTALL to remove the software
    ECHO.
    ECHO Use /REPAIR to repair the software
    ECHO.
    GOTO end
)

:install
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
MSIEXEC /I "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
COPY /Y "production.xml" "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
GOTO end

:uninstall
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
DEL /F /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
MSIEXEC /X "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
RMDIR /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client"
GOTO end

:repair
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
DEL /F /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
MSIEXEC /X "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
RMDIR /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client"
MSIEXEC /I "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
COPY /Y "production.xml" "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
GOTO end

:end 
EXIT /B

REM Src https://stackoverflow.com/questions/8179425/adding-switch-in-batch

Essentially with that command I can install my software using the /install or /uninstall or /repair switches... way useful in intune and making custom MSI's with AdvancedInstaller!

Also if you type nothing a little help menu appears instructing the user which commands are available :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.