Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!
:exec
%*
goto :eof
@echo off
setlocal
set _i=0
for %%a in (*) do (
call :exec set /a _i+=1
call :exec echo %%_i%%: %%a
)
endlocal
goto :eof
:exec
%*
goto :eof
goto :eof
The function of this batch file is to number all the files in the current directory in order!
@echo off
setlocal
set _i=0
for %%a in (*) do (
call :exec set /a _i+=1
call :exec echo %%_i%%: %%a
)
endlocal
goto :eof
:exec
%*
goto :eof
goto :eof
After removing part of the code, the execution result is the same; so the :exec label in the batch file above should be redundant! When calling the :exec label, no parameters are being passed
%* is %1 %2 %3 ...
@echo off
setlocal
set _i=0
for %%a in (*) do (
call set /a _i+=1
call echo %%_i%%: %%a
)
endlocal
::@echo off
:: Separate the first argument in the command-line arguments from the remaining arguments
:: file: detach.cmd
:: Example: detach "123 456" abc xyz
::
:: Example: detach 123 " 456 abc" xyz
::
setlocal
set argList=%*
call :detachArg1 argList
echo
goto done
:: if no quotes, this is much simpler:
:: Because FOR/F's lexical analysis does not escape quotation marks, spaces inside quotes cannot be hidden
:: FOR /f "tokens=1*" %%a IN ("%*") DO echo
:: detachArg1 arglistName
:: (split first arg from given argList)
:: detachArg1 = firstArg(*arglistName)
:: *arglistName = argTail(*arglistName)
:detachArg1
set _detachArg1=
if %1.==. goto :eof
call call :firstArg %%%1%%
call set %%^^%0%%=%firstArg%
:: Store the value of firstArg in an environment variable named after the current subroutine label, detachArg
:: The environment variable name does not contain the colon in the label (after the colon is modified by %^ it is escaped to empty), and variables with colons in their names are hard to reference
:: As the author said, the above two lines and the :firstArg section can be simplified to a single line: set detachArg1=%1
call call :argTail %%%1%%
set %1=%argTail%
:: Store the remaining return value of the subroutine, %argTail%, in the environment variable argList having the same name as the subroutine command-line parameter
goto :eof
:: Store the first passed-in argument in the environment variable firstArg, named after the current subroutine label
:firstArg
call set %%^^%0%%=%1
goto :eof
:: Return the command-line argument list without the first argument — the tail of the command line
:: return argument list, without first.
:argTail
set argList=%*
set arg1=%1
if not defined arg1 goto :eof
call :varlength arg1
:: Get the string length of the first argument
call call :trimlist %%argList:~%varlength%%%
:: Cut out the following part of the whole command line according to the length, and save it into trimlist
call set %%^^%0%%=%trimlist%
:: Save the value of %trimlist% into argTail
:: The above two lines and the :trimlist section can be simplified to a single line: call set argTail=%%argList:~%varlength%%%
goto :eof
:: String slicing
:trimlist
call set %%^^%0%%=%*
goto :eof
:: Use brute force to get the string length of environment variable arg1 (the first argument)
:: get length of environment variable (via brute force)
:: %1 name of var to check
:varlength
for /f "tokens=1* delims==" %%y in ('set %1^|findstr /b /i "%1=" ') do (
set vl_val=%%z)
set vl_vlen=0
if not defined vl_val goto lenrpdone
:: trap quoted string
:: Environment variables whose string value contains quotation marks can no longer be enclosed in one pair of quotation marks, so they must be handled differently
if "".==%vl_val:~0,1%%vl_val:~-1%. (
set vl_vlen=2
set vl_val=%vl_val:~1,-1%
) else (
set vl_vlen=0
)
:chklenrpdone
if "%vl_val%"=="" goto lenrpdone
set vl_val=%vl_val:~1%
set /a vl_vlen+=1
goto chklenrpdone
:lenrpdone
call set %%^^%0%%=%vl_vlen%
set vl_vlen=
set vl_val=
goto :EOF
:done
endlocal