如何将参数(不是命令行参数)传递给批处理脚本中的函数

发布于 2024-12-11 08:46:32 字数 1134 浏览 0 评论 0原文

我正在编写一个批处理文件,用于自动创建我们销售的产品的典型文件夹结构。我希望能够使用 2 个可选参数调用我的批处理文件;供应商的名称和用于一次创建大量文件夹的文件。如果没有提供供应商,脚本将通过标准输入询问供应商是谁。如果未提供文件,脚本会询问您要创建的文件夹的名称。如果一个文件作为参数传递,我希望脚本逐行读取文件并为每行创建一个文件夹,以该行的内容命名。这是 :readFile 函数:

:readFile
    echo "Reading from file: %theFile%"
    FOR /F "delims=," %%a IN (%theFile%) do (
        call:makeFolder %%a
    )
    goto:EOF

这是 :makeFolder 函数,它可以选择接受要创建的文件夹名称的参数。如果未提供参数,它将通过标准输入询问名称。

:makeFolder  
    if [%1]==[] (
        set /p product="Enter product name: "
    ) else (
        set product=%1
    )
    if exist "P:\%supplier%\Products\%product%" (
        echo.
        echo The folder '%product%' already exists.
        echo.
        goto:EOF
    )
    mkdir "P:\%supplier%\Products\%product%\Images\Web Ready"
    mkdir "P:\%supplier%\Products\%product%\Images\Supplied"
    mkdir "P:\%supplier%\Products\%product%\Images\Edited"
    goto:EOF

我的问题是,在 :makeFolder 函数中 %1 引用命令行上给出的第一个参数,而不是 :readFile 中提供的参数> 功能。我怎样才能实现这个目标?警告:我对批处理脚本非常很陌生,因此您可能必须像我有点愚蠢一样跟我说话。

I'm writing a batch file for automating the creation of typical folder structures for products that we sell. I would like to be able to call my batch file with 2 optional arguments; the name of the supplier and a file for creating lots of folders at once. If no supplier is supplied the script asks via standard input who the supplier is. If no file is supplied the script asks for the name of the folder you wish to create. If a file is passed as an argument I would like the script to read the file line by line and create a folder for each line, named after the contents of that line. Here is the :readFile function:

:readFile
    echo "Reading from file: %theFile%"
    FOR /F "delims=," %%a IN (%theFile%) do (
        call:makeFolder %%a
    )
    goto:EOF

Here is the :makeFolder function that optionally takes the argument of the name of the folder to create. If no argument is supplied it asks for the name via standard input.

:makeFolder  
    if [%1]==[] (
        set /p product="Enter product name: "
    ) else (
        set product=%1
    )
    if exist "P:\%supplier%\Products\%product%" (
        echo.
        echo The folder '%product%' already exists.
        echo.
        goto:EOF
    )
    mkdir "P:\%supplier%\Products\%product%\Images\Web Ready"
    mkdir "P:\%supplier%\Products\%product%\Images\Supplied"
    mkdir "P:\%supplier%\Products\%product%\Images\Edited"
    goto:EOF

My problem is that in the :makeFolder function %1 refers to the 1st argument given on the command line, not the one provided in the :readFile function. How can I achieve this? Caveat: I'm very new to batch scripting so you may have to speak to me like I'm a bit stupid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2024-12-18 08:46:32

我重建了文件并且它起作用了

@echo off
set "supplier=C:\temp\supp\"
set "product=Car"
echo test1,myComment,myValue > myFile.txt
call :readFile "myFile.txt"
EXIT /B

:readFile
echo "Reading from file: %~1"
FOR /F "usebackq delims=," %%a IN ("%~1") do (
    call :makeFolder %%a
)
goto:EOF

:makeFolder  
if "%1"=="" (
    set /p product="Enter product name: "
) else (
    set "product=%1"
)
if exist "%supplier%\Products\%product%" (
    echo(
    echo The folder '%product%' already exists.
    echo(
    goto:EOF
)
echo "%1"
echo mkdir "%supplier%\Products\%product%\Images\Web Ready"
echo mkdir "%supplier%\Products\%product%\Images\Supplied"
echo mkdir "%supplier%\Products\%product%\Images\Edited"
goto:EOF

但我建议使用延迟扩展,因为您可能会遇到特殊字符的百分比扩展问题(在这种情况下不是很相关,因为特殊字符对于文件/目录名称来说是一个糟糕的选择)。

I rebuild the file and it worked

@echo off
set "supplier=C:\temp\supp\"
set "product=Car"
echo test1,myComment,myValue > myFile.txt
call :readFile "myFile.txt"
EXIT /B

:readFile
echo "Reading from file: %~1"
FOR /F "usebackq delims=," %%a IN ("%~1") do (
    call :makeFolder %%a
)
goto:EOF

:makeFolder  
if "%1"=="" (
    set /p product="Enter product name: "
) else (
    set "product=%1"
)
if exist "%supplier%\Products\%product%" (
    echo(
    echo The folder '%product%' already exists.
    echo(
    goto:EOF
)
echo "%1"
echo mkdir "%supplier%\Products\%product%\Images\Web Ready"
echo mkdir "%supplier%\Products\%product%\Images\Supplied"
echo mkdir "%supplier%\Products\%product%\Images\Edited"
goto:EOF

But I would recommend to use delayed expansion, as you can got problems with the percent expansion of special characters( In this case not very relevant, as special characters are a bad choice for file/directory names).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文