【问题标题】:Batch File to Create Subfolders - Parameterized Function用于创建子文件夹的批处理文件 - 参数化函数
【发布时间】:2020-05-07 12:32:57
【问题描述】:

我正在尝试借助来自file1.batmake_dir 函数和来自同一文件的主文件夹内的子文件夹来创建主目录。

file1.bat内容:

:make_dir
@IF NOT EXIST "Web_Files" (mkdir Web_Files)
ECHO Web_Files Directory Created

@IF NOT EXIST "C++ Files"   (mkdir C++ Files)
ECHO C++ Files Directory Created

EXIT /B 0

:make_sub_dir
@IF NOT EXIST "Web_Files"   (mkdir Web_Files)
ECHO Web_Files Directory Created

ECHO Creating %~2 Directory
@IF NOT EXIST "Web_Files\%~2" (mkdir Web_Files\%~2)
ECHO %~2 Directory Created

EXIT /B 0

来自file2.bat 我将此函数称为:

SET "HTMLSubFolder=HTML"
SET "CSSSubFolder=CSS"
SET "JSSubFolder=JS"

call file1.bat make_dir
call file1.bat make_sub_dir %HTMLSubFolder%
call file1.bat make_sub_dir %CSSSubFolder%
call file1.bat make_sub_dir %JSSubFolder%

来自file2.bat 函数调用,仅创建主文件夹,但未创建子文件夹。

【问题讨论】:

  • call 命令可以调用另一个批处理文件或当前批处理文件中的标签。要在某个标签处输入另一个批处理文件,您必须输入一些额外的代码。例如,您可以在file1.bat 的开头放置goto :%~1 行,因此第一个参数被视为目标标签...
  • T检查目录是否存在,语法为… EXIST "Dir Name\" …(反斜杠很重要,确保同名文件不会被混淆为目录)。此外,您告诉最终用户创建了一个目录,但您尚未确定是否是这种情况。你告诉了它,不代表它成功了,所以我建议你也看看。
  • @aschipfl 您能否将您的建议添加到答案中?没有得到你想要说的话。
  • @Compo 我只在它们之前不存在的情况下创建那些目录,是的,我正在使用反斜杠。
  • @Compo,我曾经使用if exist "dir\" 方法,直到我最近发现这仅适用于本地驱动器,但当目标项目位于共享/映射网络上时它不起作用驱动器(net use ...),因为当项目实际上是一个文件时,带有尾随反斜杠的语法甚至报告了True;不确定(还)是否也是 substpushd 映射的驱动器的情况(但我会测试),或者我系统中的特定星座是否是根本原因,但这让我改用for %I in ("dir") do set "ATTR=%~aI" & if "!ATTR:~,1!"=="d" echo Dir!...

标签: windows batch-file cmd subroutine


【解决方案1】:

call 命令可以调用另一个批处理文件或当前批处理文件中的标签,但您试图同时指定两者,其中第二个只是作为参数传递给第一项,即批处理文件。

但是,您可以让被调用的脚本完全使用该参数作为跳转标签:

  • file1.bat(被调用脚本;我们也可以将其命名为被调用者):

    rem /* This takes the first argument as a jump label and continues execution there;
    rem    if it fails due to an non-existent or invalid label the script terminates: */
    goto :%~1 || exit /B 1
    
    :make_dir
    rem /* Here I removed `if exist` since `mkdir` cannot create an already existing
    rem    directory anyway; to suppress the error message I appended `2> nul`: */
    mkdir "Web_Files" 2> nul
    echo  "Web_Files" directory created
    
    mkdir "C++ Files" 2> nul
    echo "C++ Files" directory created
    
    exit /B 0
    
    :make_sub_dir
    rem /* Here I remove creation of "Web_Files" as the next command would create it
    rem    anyway when it does not yet exist (if command extensions are enabled): */
    echo Creating "%~2" directory
    mkdir "Web_Files\%~2" 2> nul
    echo "%~2" directory created
    
    exit /B 0
    
  • file2.bat(调用者;除了引用之外几乎没有变化):

    SET "HTMLSubFolder=HTML"
    SET "CSSSubFolder=CSS"
    SET "JSSubFolder=JS"
    
    rem // Here I quoted the second argument to protect spaces and poisonous characters:
    call file1.bat make_dir
    call file1.bat make_sub_dir "%HTMLSubFolder%"
    call file1.bat make_sub_dir "%CSSSubFolder%"
    call file1.bat make_sub_dir "%JSSubFolder%"
    

如果我必须实施这样的方法,我会改变一些事情:

  • 仅当第一个参数以 : 开头时,才让被调用方将其解释为标签,因此它还可能会接收其他参数以获取更多功能;
  • 在跳转到该标签之前,移动剩余的参数,以使被调用例程中的参数引用与内部调用时相比没有更多差异;

这就是我的意思(应用于被调用者,file1.bat):

rem /* This takes the first argument as a jump label and continues execution there;
rem    if it fails due to an non-existent or invalid label the script terminates: */

rem // Store the first argument to a variable:
set "ARG1=%~1"
rem /* Check the first character of the first argument:
rem     * if it is a colon, the argument is interpreted as a jump label;
rem       so shift all further argument references back by one, so called routines
rem       do not have to take care of the first argument (the label) anymore, which
rem       is irrelevant there anyway; then try to jump and terminate upon an error;
rem    * if it is any other character or even blank (when no argument was given),
rem      any other activity can be triggered; for example, printing a message: */
if defined ARG1 if "%ARG1:~,1%"==":" shift /1 & goto %~1 || exit /B 1
rem /* This point is reached when the first argument does not begin with `:`;
rem    you can do some default actions here, or whatever else you like. */
exit /B 0


:make_dir
::(skipping the functional code here...)
exit /B 0


:make_sub_dir
rem // Here the first argument is referred to now:
echo Creating "%~1" directory
mkdir "Web_Files\%~1" 2> nul
echo "%~1" directory created

exit /B 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2016-10-23
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多