Search code examples
batch-fileif-statementfile-exists

Select two files in the if exist command


The truth is I'm new to programming. So I'd be happy if you could help me. I need to select two things in the "if exits" command, that is, either the two files or one of them, to which the following command applies. This is the code:

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

I searched and did not find, tried all kinds of options and did not succeed. Thank you!


Solution

  • You can replace your whole code with just the following four lines:

    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\