Capture the list of parameters of the CMD batch file; write to file for later processing
I wrote a batch file that the program launches for post-processing. The batch file reads ~ 24 parameters provided by the calling program, stores them in variables, and then writes them to various text files.
Since the maximum input variable in CMD is% 9, you must use the "shift" command to read and store this data multiple times, separately for named variables. Since the program outputs several similar batch files, the result opens several CMD windows in sequence, assigning variables and writing data files. It takes too long to bind the calling program.
It occurs to me that I can free the calling program much faster if perhaps there is a way to write a very simple batch file that can write all the command parameters to a text file where I can process them later. Basically, just take the parameter list, write and do.
Q: Is there a way to process a whole series of parameter data as one big text string and write it into one big variable ... and then repeat the whole big thing into one text file? Then later read the line in% n variables if there is no program waiting to resume?
The parameter list is something like 25 - 30 words, less than 200 characters.
Example of a list of parameters:
"First name" "Last name" "123 Steet Name Way" "City name" ST 12345 1004968 06/01/2010 "First and last name" 101738 "On the account" 20.67 xy-1z 1 8.95 3, 00 1.39 0 0 239 8.95
Quoted items are treated as string variables. The list is a blank.
Any suggestions?
a source to share
echo %* 1>args.txt
% * refers to all arguments:% 1% 2% 3 ...
It also works with subroutines.
call :test 1 2 3
goto :eof
:test
echo 1: %1
echo 2: %2
echo 3: %3
echo *: %*
exit /b
exit:
1: 1
2: 2
3: 3
*: 1 2 3
For more information, see the following website: http://ss64.com/nt/syntax-args.html
a source to share
Interesting post. This piqued my interest.
I also need something that can take parameters, and while this is probably not useful to you right now, I thought it might be useful at a later date.
My solution is less straightforward - because there simply isn't an elegant way to do it.
Basically, in this example, "-" can be used to identify the parameter, and it is assumed that the following space is set to a value.
Legal information:
So this is all my code and I don't care how or where you decide to use it. Anyway, you don't need to use me as an example.
Example:
Microsoft package: Start Copy below and save as filename.bat
@ECHO OFF
REM USAGE: this-batch-name.bat -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
SET __CURRENT_WORKING_DIRECTORY__=%~dp1
ECHO.__CURRENT_WORKING_DIRECTORY__=%__CURRENT_WORKING_DIRECTORY__%
REM # Clear Previous Variables
SET PACKAGING_BUILD_NUMBER=
SET PACKAGING_JOB_NAME=
SET GO_DEEPER=
SET RUN_COMMAND=
REM ## In order to read variables set while in a "FOR" loop
REM ## you have to set the 'ENABLEDELAYEDEXPANSION' with 'SETLOCAL'.
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
REM ## Capture Command line parameters here with a %*
FOR %%A IN (%*) DO (
REM ## If we found something with a '-' in previous pass run GO_DEEPER will be defined and thus set to the command line argument.
IF DEFINED GO_DEEPER (
REM ## When ENABLEDELAYEDEXPANSION is Set with setlocal command you have to use exclamation: i.e. '^!'
IF /I "-BUILD"=="!GO_DEEPER!" SET PACKAGING_BUILD_NUMBER=%%A
IF /I "-JOB"=="!GO_DEEPER!" SET PACKAGING_JOB_NAME=%%A
IF /I "-RUN"=="!GO_DEEPER!" SET RUN_COMMAND=%%A
SET SET GO_DEEPER=
)
IF /I "%%A" GEQ "-" (
REM ## Wow we found your command line argument that started with a '-' so set the GO_DEEPER Var
SET GO_DEEPER=%%A
) ELSE (
SET SET GO_DEEPER=
)
)
REM ## Time to grab the variables set while in delayed expansion mode
ENDLOCAL && SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER% && SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME% && SET RUN_COMMAND=%RUN_COMMAND%
REM ## Sucks, but you have to clear the '"' and "'" if it exists.
IF DEFINED RUN_COMMAND (
SET RUN_COMMAND=%RUN_COMMAND:"=%
SET RUN_COMMAND=%RUN_COMMAND:'=%
)
IF DEFINED PACKAGING_JOB_NAME (
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:"=%
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:'=%
)
IF DEFINED PACKAGING_BUILD_NUMBER (
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:"=%
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:'=%
)
REM ## Now we can try to run the command function if the -run was used...
IF DEFINED RUN_COMMAND (
CALL:--%RUN_COMMAND% "'%PACKAGING_JOB_NAME%'","'%PACKAGING_BUILD_NUMBER%'"
) ELSE (
ECHO Try running:
ECHO %0 -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
)
GOTO DONE
:--RUN_FUNCTION
ECHO running... %~0
SET VARPASSED1=%~1
SET VARPASSED2=%~2
IF DEFINED VARPASSED1 ECHO VARPASSED1 was %VARPASSED1%
IF DEFINED VARPASSED2 ECHO VARPASSED2 was %VARPASSED2%
ECHO Add your code to process here...
GOTO:EOF
:DONE
ECHO We got the following results...
IF DEFINED PACKAGING_JOB_NAME ECHO PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME%
IF DEFINED PACKAGING_BUILD_NUMBER ECHO PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER%
IF DEFINED RUN_COMMAND ECHO RUN_COMMAND=%RUN_COMMAND%
</pre> </code>
Microsoft
:
__CURRENT_WORKING_DIRECTORY __ = C:\DEV\\\Sysprep \
running...: - RUN_FUNCTION
VARPASSED1 "..."
VARPASSED2 "..."
...
PACKAGING_JOB_NAME = " "
PACKAGING_BUILD_NUMBER = "1.2.3 build 405"
RUN_COMMAND = RUN_FUNCTION
>
a source to share