用于创建子文件夹的批处理文件 - 参数化函数

Batch File to Create Subfolders - Parameterized Function

我正在尝试借助 file1.bat 中的 make_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(被调用的脚本,也可以命名为被调用者):

    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