批处理脚本子例程:传递参数 [英] batch script subroutine: Passing arguments
问题描述
我的理解是,为了从传递给子例程的文件中获取日期作为参数,您必须将该参数重新设置为子例程中的变量.这样对吗?这对我而言没有任何意义,所以我想知道我是否不完全了解正在发生的事情.除了日期提取之外,我几乎可以在任何其他子例程代码中使用传递的参数.
My understanding is that in order to get the date from a file passed into a subroutine as an argument, you must re-set that argument as a variable within the subroutine. Is this correct? This doesn't make since to me, so I am wondering if I do not fully understand what is going on. I can use the passed in argument in practically any other subroutine code except for date extraction.
set setupEXE=setup.exe
CALL :SUB_CheckCorrectDate %setupEXE%
GOTO EOF
::----------------------------------
:SUB_CheckCorrectDate
set filename=%1%
:: SUCCESSFUL
for %%x in (%filename%) do set FileDate=%%~tx
@For /F "tokens=1-3 delims=-/ " %%A in ('@echo %FileDate%') do @(
Set file_Month=%%A
Set file_Day=%%B
Set file_Year=%%C
)
:: GET ERROR
for %%x in (%1%) do set FileDate=%%~tx
@For /F "tokens=1-3 delims=-/ " %%A in ('@echo %FileDate%') do @(
Set file_Month=%%A
Set file_Day=%%B
Set file_Year=%%C
)
GOTO:EOF
:: ------------------
:EOF
推荐答案
使用%1
访问参数,而不是%i%
.
Use %1
to access the parameter, not %i%
.
参数变量与FOR变量具有相同的修饰符,因此可以使用%~t1
.
The argument variables have the same modifiers as FOR variables, so you can use %~t1
.
无需在FOR/F中执行命令.使用in ("string")
处理字符串文字更为简单.
No need to execute a command in your FOR /F. It is simpler to process a string literal using in ("string")
.
不需要:EOF
标签.每个脚本都有一个隐式的:eof
.我想改用exit /b
.
No need for :EOF
label. Every script has an implicit :eof
. I like to use exit /b
instead.
@echo off
setlocal
set "setupEXE=setup.exe"
call :SUB_CheckCorrectDate "%setupEXE%"
exit /b
::----------------------------------
:SUB_CheckCorrectDate
set "filename=%~1"
for /F "tokens=1-3 delims=-/ " %%A in ("%~t1") do (
set "file_Month=%%A"
set "file_Day=%%B"
set "file_Year=%%C"
)
exit /b
这篇关于批处理脚本子例程:传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!