批处理文件> NUL>> %dir%错误

时间:2012-04-13 17:19:46

标签: file batch-file redirect

我制作了这个批处理文件,除了子菜单4重定向ping 192.168.1.1 -n 1 -w!thyme之外,它工作正常!到NUL而不是echo ping 192.168.1.1 -n 1 -w!thyme!> NUL to%dir101%

@echo off & SETLOCAL EnableDelayedExpansion
title TXTanimation
set dir101=C:\USers\Jake\Desktop\file.bat
goto jakeinc
echo Lets make a text animation
pause
set /p dir101=where will the .bat be saved:
:jakeinc
echo @ECHO OFF > %dir101%
echo: >> %dir101%
:menu
echo 0=Edit important information
echo 1=Display .txt
echo 2=Play sound
echo 3=Close sound player
echo 4=Add nessicary wait between .txt
echo 5=Label point
echo 6=Edit variable
echo 7=Goto point
echo 8=Create IF sentence
echo 9=End it
Set /p choose=which one:

If '%choose%'=='0' (
SEt /p title=What is the title
:LOL101
set /p color=what color do you want type test to experiment with the colors
If '!color!'=='test' goto tester
goto model
:tester
SEt /p test=Please give hexadecimal number type exit to leave:
if '!test!'=='exit' goto LOL101
color !test!
goto tester
:model
echo title %title% >> %dir101%
echo color %color% >> %dir101%
goto menu
)

If '%choose%'=='1' (
set /p dir=what is the dir of your .txt:
)
If '%choose%'=='1' (
echo cls >> %dir101%
echo type %dir% >> %dir101%
goto menu
)
If '%choose%'=='4' (
SEt /p thyme=How much milliseconds 250 is usual:
echo ping 192.168.1.1 -n 1 -w !thyme!>NUL >> %dir101%
goto menu
)
If '%choose%'=='9' (
echo Thanks for making
pause
exit /b
)

是的,这是一个很大的计划,但我的错误真的很小。我可以输入模板并插入变量吗? 但是echo ping 192.168.1.1 -n 1 -w !thyme!>NUL >> %dir101%我需要它来声明要重定向的> NUL

2 个答案:

答案 0 :(得分:1)

当您尝试将这些命令回显到另一个批处理时,您需要正确地转义这些命令。试试这个:

echo ping 192.168.1.1 -n 1 -w ^!thyme^!^>NUL >> %dir101%

将此内容写入%dir101%批次:

ping 192.168.1.1 -n 1 -w *value_of_thyme_variable* > NUL

^批量转义大部分功能。 仅供参考:%变量值起作用。如果您需要转发%变量值,例如%test%,您可以这样做:%%test%%,但正如您所看到的,它适用于上面的!变量值

答案 1 :(得分:1)

您只需要转义重定向符号,以便它被回显而不是被解释为重定向。

echo ping 192.168.1.1 -n 1 -w !thyme! ^>NUL >>%dir101%

警告:您的子菜单0按照书面形式工作,但您可能会养成一种不良习惯,将来会给您带来麻烦。通常不应在带括号的代码块中放置标签。请阅读(Windows batch) Goto within if block behaves very strangely以获取可能导致问题的示例,并且所选答案解释了导致问题的原因。