批处理功能无法正常工作 [英] Batch function not working correctly

查看:35
本文介绍了批处理功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一阵子,我做了一个可以从命令提示符或任何批处理文件调用的函数(这只是为了好玩,我看不出它怎么有用).基本上,它只是使您的(Microsoft)计算机说出您作为参数写入的内容.最近,我得到了一些灵​​感,可以在其中添加一个开关来读取文件的内容.我的独立脚本可以运行,但是当我将其添加到函数中时,它却无法正常运行.

A while ago I made a function that you can call from the command prompt or any batch file (it was just for fun, I don't see how it could be useful). It basically just makes your (Microsoft) computer speak whatever you wrote in as the parameter. I recently got some inspiration to add a switch to it where it would read the contents of a file. My standalone script worked, but when I added it to my function, it didn't work as I would have liked.

代码如下:

@echo off & setlocal enabledelayedexpansion

if "%~1"=="/?" (
    echo.
    echo TALK "Text" [Parameters]
    echo.
    echo Text - The phrase you want to be spoken.
    echo.
    echo [Parameters]:
    echo              /f - Read the contents of a file. "Text" changes to the file path.
    echo.
    endlocal
    exit /b
)

if "%~2 X" equ "/f X" (

    if not exist %~1 (
        echo File does not exist or cannot be found.
        endlocal
        exit /b
    )

    set cont= 
    for /f "delims=" %%i in (%~1) do set cont=!cont! %%i
    :b
    echo Set a = Wscript.CreateObject("SAPI.SpVoice") > "Talk.vbs"
    echo a.speak "%cont%" >> "Talk.vbs"
    start /WAIT Talk.vbs
    del Talk.vbs
    endlocal
    exit /b
)

set text=%~1

echo set speech = Wscript.CreateObject("SAPI.spVoice") > "talk.vbs"
echo speech.speak "%text%" >> "talk.vbs"
start /WAIT talk.vbs
del Talk.vbs
endlocal
exit /b

不幸的是,我没有可用的功能代码(在添加/f 开关之前).

Unfortunately I don't have working function code (before I added the /f switch).

这对我来说是不得已的方法,因为我已经对其进行了大量编辑,并仔细检查了代码以免出现任何问题.

This is a last resort for me as I've edited it heavily and scoured the code for any give away as to what the problem might be.

另一个不好的事情是我没有记下更改的内容,因此我无法准确告诉您我尝试过的内容.我可以告诉你输出的是什么.

Another bad thing is that I didn't take note of what I changed, so I can't exactly tell you what I've tried. I can tell you what the outputs are though.

我第一次尝试,它给出了输出该命令的语法不正确.

The first time I tried, it gave the output The syntax of the command is incorrect.

现在是原始功能(仅将文本转换为语音)不再起作用的时候.文件 Talk.vbs (在此过程中创建)的内容为 a.speak" .

It's now at the point where the original function (just converting text to speech) doesn't work anymore. The contents of the file Talk.vbs (which was made during the process) is a.speak "".

我将不断更新自己的尝试,但了解我,这确实是我忽略的事情.

I'll keep updating my attempts, but knowing me it's something really simple that I've overlooked.

-编辑-根据某人的建议,我在语法部分的方括号前放置了克拉.什么都没改变.

--EDIT-- At the suggestion of someone, I put carats before the square brackets in the syntax section. Nothing changed.

推荐答案

除了转义括号,如果参数为,则还必须将如果存在%〜1 括在引号中我想说的一些话" .还清理了一下.代码位于底部,但首先进行说明.

Along with escaping the parenthesis you also had to surround if exist %~1 in quotes in case of a argument of "some words I want it to say". Also cleaned it up a bit. Code at the bottom, but first an explanation.

如果您在talk.vbs删除之前查看它,则会看到以下内容:

If you looked at talk.vbs before it was deleted you would see this:

a.speak "!cont! contents of the file here" 

这是因为此代码:

for /f "delims=" %%i in (%~1) do set cont=!cont! %%i
:b
echo Set a = Wscript.CreateObject("SAPI.SpVoice") > "Talk.vbs"

如果打开echo并查看代码,您将看到最后一个未转义的)正在获取for循环的内容并将其包含在重定向中.

If you turned echo on and watched the code you would see the last unescaped ) was taking the contents of the for loop and including it in the redirect.

已更正和清除的代码:

@echo off & setlocal enabledelayedexpansion
if "%~1"=="/?" (
    echo.
    echo TALK "Text" [Parameters]
    echo.
    echo Text - The phrase you want to be spoken.
    echo.
    echo [Parameters]:
    echo              /f - Read the contents of a file. "Text" changes to the file path.
    echo.
    endlocal
    exit /b
)
set text=
if [%2]==[/f] (
    if exist "%~1" (
        for /f "usebackq delims=" %%i in (%1) do set text=!text! %%i
    ) else (
        endlocal
        exit /B
    )
)
if [%2]==[] set text=%~1

echo set speech = Wscript.CreateObject^("SAPI.spVoice"^) > "talk.vbs"
echo speech.speak "%text%" >> "talk.vbs"
cscript //NoLogo //B talk.vbs
del Talk.vbs
endlocal
exit /b

编辑:修复了Andriy M指出的 for 语句.

fixed the for statement pointed out by Andriy M

这篇关于批处理功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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