【问题标题】:Select two files in the if exist command在 if exists 命令中选择两个文件
【发布时间】:2020-07-11 07:16:18
【问题描述】:

事实上,我是编程新手。所以如果你能帮助我,我会很高兴。 我需要在“if exits”命令中选择两件事,即两个文件或其中之一,以下命令适用。 这是代码:

if exist *.mp3 *.wma *.wav goto music
if exist *.mp4 *.mov goto video
if exist *.docx *.txt *.pdf goto document
if exist *.jpg *.png goto image
goto end
:music
md Music
move *.mp3 Music
move *.wma Music
move *.wav Music
if exist *.mp4 *.mov goto video
if exist *.docx *.txt *.pdf goto document
if exist *.jpg *.png goto image
goto end
:video
md Video
move *.mp4 Video
move *.mov Video
if exist *.docx *.txt *.pdf goto document
if exist *.jpg *.png goto image
goto end
:document
md Documents
move *.docx Documents
move *.txt Documents
move *.pdf Documents
if exist *.jpg *.png goto image
goto end
:image
md Pictures
move *.jpg Pictures
move *.png Pictures
goto end
:end
exit

我找了也没找到,试了各种办法都没有成功。 谢谢!

【问题讨论】:

  • 如果存在不是你想要的命令。打开CMD,输入For /?

标签: batch-file if-statement file-exists


【解决方案1】:

您可以使用循环来迭代值:

for %%a in (mp3 wma wav) do if exist *.%%a goto music

为了使内容更具可读性,您甚至可以定义宏并重用它们:

::::: marco ::::::::::
set "if_one_of=for %%a in ("
set "exist=) do if exist *.%%a"
:::::::::::::::::::::::

%if_one_of% mp3 wma wav %exist% goto music

更多关于FOR

【讨论】:

  • 非常感谢!你帮了我很多!
【解决方案2】:

您可以只用以下四行替换整个代码:

for %%a in (mp3 wma wav) do if exist *.%%a md music 2>nul & move *.%%a music\
for %%a in (mp4 mov) do if exist *.%%a md video 2>nul & move *.%%a video\
for %%a in (docx txt pdf) do if exist *.%%a md documents 2>nul & move *.%%a documents\
for %%a in (jpg png) do if exist *.%%a md pictures 2>nul & move *.%%a pictures\

【讨论】:

  • 太棒了!你真棒!!!我只是不明白什么是命令 2>nul 谢谢!
  • 2>nulmd 尝试创建已经存在的文件夹时抑制错误消息(这种方式比先检查文件夹是否已经存在更容易)。 2 是“错误流”。请参阅here 了解更多信息。
猜你喜欢
  • 2011-12-24
  • 1970-01-01
  • 2013-09-17
  • 2016-12-10
  • 2018-02-25
  • 1970-01-01
  • 2017-09-09
  • 2017-06-13
  • 1970-01-01
相关资源
最近更新 更多