简体   繁体   English

批处理文件不会回显到文件中

[英]batch file doesn't echo into a file

why do these lines not work? 为什么这些行不起作用?

echo   POSITION-X: %shopxpos%>>shop%shopid%.yml
echo   POSITION-Y: %shopypos%>>shop%shopid%.yml
if "%shopincludedata%" == "y" (echo   DATA-VALUE: %shopitemdata%>>shop%shopid%.yml) else if "%shopincludedata%" == "Y" (echo   DATA-VALUE: %shopitemdata%>>shop%shopid%.yml) else if "%shopincludedata%" == "yes" (echo   DATA-VALUE: %shopitemdata%>>shop%shopid%.yml) else if "%shopincludedata%" == "YES" (echo   DATA-VALUE: %shopitemdata%>>shop%shopid%.yml) else if "%shopincludedata%" == "Yes" (echo   DATA-VALUE: %shopitemdata%>>shop%shopid%.yml)

they just don't spit out into the file here's the file i'm using this in: https://www.dropbox.com/s/xui34kgkdjmee3p/ChestCommands.bat?dl=0 他们只是不吐出文件,这是我在以下位置使用的文件: https : //www.dropbox.com/s/xui34kgkdjmee3p/ChestCommands.bat?dl=0

The problem is the values of %shopxpos% and %shopypos% are single digit numbers, and single digit numbers are used to identify which file handles should be redirected. 问题是%shopxpos%和%shopypos%的值是一位数字,而一位数字用于标识应重定向的文件句柄。 You want the number to be treated as a literal string value to be ECHOed, but the parser is treating the number as a file handle identifier instead. 您希望将数字视为要回显的文字字符串值,但解析器将数字视为文件句柄标识符。

The simplest solution is to move the redirection to the front of the command. 最简单的解决方案是将重定向移动到命令的开头。 It can appear anywhere. 它可以出现在任何地方。

I would not check for all those variations of "yes". 我不会检查所有“是”的变体。 Instead, I would check to see if the first character is Y , case insensitive. 相反,我将检查第一个字符是否为Y ,不区分大小写。

>>shop%shopid%.yml echo   POSITION-X: %shopxpos%
>>shop%shopid%.yml echo   POSITION-Y: %shopypos%
if /i "%shopincludedata:~0,1%" == "y" >>shop%shopid%.yml echo   DATA-VALUE: %shopitemdata%

or better yet, use parentheses and redirect only once. 或更妙的是,使用括号并仅重定向一次。

>>shop%shopid%.yml (
  echo   POSITION-X: %shopxpos%
  echo   POSITION-Y: %shopypos%
  if /i "%shopincludedata:~0,1%" == "y" echo   DATA-VALUE: %shopitemdata%
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM