如何将参数(而非命令行参数)传递给批处理脚本中的函数 [英] How to pass arguments (not command line arguments) to functions within batch scripts

查看:101
本文介绍了如何将参数(而非命令行参数)传递给批处理脚本中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我的问题是,在:makeFolder函数中,%1引用的是命令行中给出的第一个参数,而不是:readFile函数中提供的参数.我怎样才能做到这一点?警告:我非常批处理脚本,所以您可能不得不跟我说话,就像我有点愚蠢.

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.

推荐答案

我重建了文件,它起作用了

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).

这篇关于如何将参数(而非命令行参数)传递给批处理脚本中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆