【问题标题】:How do I save a file name to a variable in cmd?如何将文件名保存到cmd中的变量?
【发布时间】:2016-01-29 20:27:11
【问题描述】:
@echo off
title Music
color 0f

:main
cls
echo Welcome to you Music!
echo.
set /p var=What would you like to do: 
if %var%==find goto find
if %var%==play goto play
goto main

:find
cls
set /p song=What song would you like me to check for (Case Sensitive): 
if exist music\"*%song%*.mp3" goto exist
if not exist music\"*%song%*.mp3" goto notExist

:exist
cls
echo %song% is here.
echo.
pause
goto main

:notExist
cls
echo %song% is not here. Check the spelling and letter case for errors.
echo.
pause
goto main

:play
cls
set /p song=Enter the name of a song to play: 
if exist music\"*%song%*.mp3" goto play2
if not exist music\"*%song%*.mp3" goto notExist
goto main

:play2
cls
set songName = [file name]

:playing
:: Will put more code later.

这个程序的目的是在我的音乐中搜索一首特定的歌曲,并能够从命令行播放它。稍后我将添加代码来控制音乐,但现在我只想能够找到歌曲并播放它。我正在使用'',所以有人可以输入“Another Brick in the Wall”并让它找到“Another Brick in the Wall Part 2”问题是保存变量时,它不能使用' ' 否则它们将被保存为字符串的一部分。

【问题讨论】:

  • 文件名不能包含少数字符,引号是其中之一。您是否打算验证他们输入的每个字符都是有效的文件名字符?
  • 我想要它,这样我就可以让他们写下一个名字的一部分,它说有那个名字的结果。然后可能会告诉用户哪些歌曲与搜索匹配。
  • 他说它应该可以正常工作,因为无论如何你都不能在文件名中使用"。您是否有特定的错误或问题?
  • @JosephLyle,我真的不明白你要解决什么问题。
  • 当我运行它时,程序会说它不能播放一首歌,因为它不存在。在帖子中,我以墙上的另一块砖为例。我希望能够输出用户输入的所有歌曲,这样就可以说他们在墙上的另一块砖的搜索有 1 个结果:墙上的另一块砖第 2 部分。如果必须,我可以使用文本包含我的歌曲列表的文件,但我希望它告诉用户哪些歌曲与他们的搜索匹配。

标签: file variables batch-file cmd


【解决方案1】:

你的问题不是很清楚。以下应该回答我从您的问题和 cmets 中猜到的内容:

set /p "song=What song would you like me to check for (Case insensitive): "
   REM if you are afraid of qoutes entered by the user, remove them:
   set song=%song:"=%
<nul set /p "=Songs found: "
dir /b "music\*%song%*.mp3"|find /c /v ""
echo the names are:
dir /b "music\*%song%*.mp3"
echo executing them...
for /f "delims=" %%i in ('dir /b /s "music\*%song%*.mp3"') do (
  echo ... executing %%i
  REM whatever you use to play it
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多