Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 从文本文件中读取值并将其传递到if条件中_Windows_Batch File - Fatal编程技术网

Windows 从文本文件中读取值并将其传递到if条件中

Windows 从文本文件中读取值并将其传递到if条件中,windows,batch-file,Windows,Batch File,我试图让这个批处理文件读取一个.txt文件,并使用它找到的值1、2或3来选择一首歌曲播放。我已经写了这篇文章,但它只在.txt读取1时起作用,但是2和3不起作用 @echo off for /f %%i in (startsong.txt) do set ransongforintropicker=%%i IF %ransongforintropicker% == 1 set ransongforintro=BloodMoon_by_VeoRug.mp3 IF %ransongforintro%

我试图让这个批处理文件读取一个.txt文件,并使用它找到的值1、2或3来选择一首歌曲播放。我已经写了这篇文章,但它只在.txt读取1时起作用,但是2和3不起作用

@echo off
for /f %%i in (startsong.txt) do set ransongforintropicker=%%i
IF %ransongforintropicker% == 1 set ransongforintro=BloodMoon_by_VeoRug.mp3
IF %ransongforintro% == BloodMoon_by_VeoRug.mp3 set ransongforintrotext=Song: Blood Moon By VeoRug
IF %ransongforintropicker% == 2 timeout /t -1
IF %ransongforintro% == Invincible_by_VeoRug.mp3 set ransongforintrotext=Song: Invincible By VeoRug
IF %ransongforintropicker% == 3 set ransongforintro=GoldenMove_by_VeoRug.mp3
IF %ransongforintro% == GoldenMove_by_VeoRug set ransongforintrotext=Song: Golden Move By VeoRug

有人能告诉我如何解决这个问题,或者告诉我一种更好的方法吗?

您会遇到执行时变量扩展的问题。您需要在脚本顶部添加以下内容:

setlocal enabledelayedexpansion

下面是对代码进行的修改,以保留现有结构,这可能会澄清一些问题:

@echo off
setlocal enabledelayedexpansion

set trackchoice=0
set trackfile=0
set trackinfo=0

for /f %%i in (trackchoice.txt) do set trackchoice=%%i

if %trackchoice% == 1 set trackfile=Lateralus.mp3
   if %trackfile% == Lateralus.mp3 (
      set trackinfo=Track: Lateralus
      echo %trackfile%
      echo !trackinfo! & echo.Artist: Tool
      pause
      )

if %trackchoice% == 2 set trackfile=Schism.mp3
    if %trackfile% == Schism.mp3 (
      set trackinfo=Track: Schism
      echo %trackfile%
      echo !trackinfo! & echo.Artist: Tool
      pause
      )

如果您想在任何时候播放.mp3,您都可以使用该命令。

现在我终于回到了实际的键盘前,我可以更正确地解决脚本中的问题。如果您必须在原始帖子中保留格式,那么您的代码将无法使用2或3,因为您的If语句不正确

如果%ransongforintropicker%==2 timeout/t-1在文件包含2时运行timeout命令,但是它没有设置ransongforintro变量,因此IF语句将计算为IF==2,这将始终返回false

在最后一个if语句中,您将%ransongforintro%的值与\u VeoRug的GoldenMove\u进行比较,但在前面的语句中,您将该变量设置为\u VeoRug.mp3的GoldenMove\u,因此您的if语句的计算结果为if GoldenMove_by_VeoRug==GoldenMove_by_VeoRug.mp3,它同样会返回false

扩展我的评论,使用括号可以防止将变量设置为不正确的值,这就是为什么3不起作用,或者忘记完全设置它们,这就是为什么2不起作用

此外,我缩短了变量名,因为我是从手机发帖的

@echo off
for /F %%A in (startsong.txt) do set ransong=%%A

if %ransong%==1 (
    set song=BloodMoon_by_VeoRug.mp3
    set "songtitle=Blood Moon by VeoRug"
)
if %ransong%==2 (
    set song=Invincible_by_VeoRug.mp3
    set "songtitle=Invincible by VeoRug"
)
if %ransong%==3 (
    set song=GoldenMove_by_VeoRug.mp3
    set "songtitle=Golden Move by VeoRug"
)
脚本:由@SomethingDark修改括号将放在哪里:

@echo off
for /f %%i in (startsong.txt) do (
    set ransongforintropicker=%%i
    if %%i equ 1 (
        set ransongforintro=BloodMoon_by_VeoRug.mp3
        set ransongforintrotext=Song: Blood Moon By VeoRug
    )
    if %%i equ 2 (
        rem timeout /t -1
        set ransongforintro=Invincible_by_VeoRug.mp3
        set ransongforintrotext=Song: Invincible By VeoRug
    )
    if %%i equ 3 (
        set ransongforintro=GoldenMove_by_VeoRug.mp3
        set ransongforintrotext=Song: Golden Move By VeoRug
    )
)
echo ransongforintropicker %ransongforintropicker%
echo ransongforintro       %ransongforintro%
echo ransongforintrotext   %ransongforintrotext%
输出:

==>echo ^1>startsong.txt

==>..\29291082.bat
ransongforintropicker 1
ransongforintro       BloodMoon_by_VeoRug.mp3
ransongforintrotext   Song: Blood Moon By VeoRug

==>echo ^2>startsong.txt

==>..\29291082.bat
ransongforintropicker 2
ransongforintro       Invincible_by_VeoRug.mp3
ransongforintrotext   Song: Invincible By VeoRug

==>echo ^3>startsong.txt

==>..\29291082.bat
ransongforintropicker 3
ransongforintro       GoldenMove_by_VeoRug.mp3
ransongforintrotext   Song: Golden Move By VeoRug

==>echo ^4>startsong.txt

==>..\29291082.bat
ransongforintropicker 4
ransongforintro
ransongforintrotext

==>

你可以尝试使用合适的工具来完成这项工作。批处理文件对于此类任务来说非常糟糕。那么,您有什么建议吗?PowerShell会做得很好。这是一种脚本语言,它比批处理文件强大得多,并且与Windows捆绑在一起。我将查找它,感谢您的建议:哦,请。PowerShell非常冗长。在批处理上使用PowerShell的唯一必要时间是,如果您正在使用非整数或大于2^31的数字进行数学运算,或者如果您需要与GUI交互。批处理非常适合此任务。它在这里不起作用只是因为你没有使用括号。为什么要把整个东西都放在for循环中呢?文本文件仅包含一个字符。见鬼,如果只说set/p ransongforintropicker=你的括号想法不折不扣:@somethingsdark:但是,set/p ransongforintropicker可能更简单=