If not exist (*.*) not working on empty folder (Windows batch)

I don't think I have ever had this happen before. Using these commands:

rem Check if any anything was downloaded... if not exist "%download_temp%\*.*" goto :fail_message if exist "%download_temp%\*.*" goto :continue 

I can put a pause before those 2 commands and look in the %download_temp% folder and it is definitely empty, I have hidden files and "super hidden" system files set to show in Windows, I am logged in as an Administrator, the folder is definitely empty. I also know the download location being chosen doesn't have the types of files I am trying to download - so I have multiple reasons to know for sure the folder is empty.

Despite that, the above commands still skip to the :continue label instead of where it should, the :fail_message label.

Here's a quirk I noticed - if I change *.* to the file type *.json then everything works as it should, but sometimes it might not be a .json file, hence the use of *.*

I have also tried swapping *.* to just * but get the same effect.

Here's the entire batch file, there's no other goto commands before the ones shown above and I double checked the labels are correct:

cls @echo off mode con: cols=85 lines=40 powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=160;$B.height=9999;$W.buffersize=$B;}" cd /d %~dp0 rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem This will attempt to download the Live Chat text file from a YouTube video / channel, from a copied clipboard link. rem rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- title Download Live Chats Before Date (From Clipboard Link) color 0f rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Define a variable containing a single space character for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set GAP=%%A rem Set the input date as variables %year%%month%%day% cls echo. echo. echo -------------------------------------------------------------------------- echo Please copy the link to a YouTube channels "Video" page to your clipboard. echo. echo Only Live Chats from BEFORE the date you enter will be downloaded. echo. echo Please use numbers only, for example type 4 for the month of April, etc. echo -------------------------------------------------------------------------- echo. set /p day="%GAP% Please type the DAY: " set /p month="%GAP% Please type the MONTH: " set /p year="%GAP% Please type the YEAR: " echo. echo Please wait... rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Check that yt-dlp is up to date... start /wait /min yt-dlp.exe -U rem Cleanup from last time if it was updated while the process was running... if exist "yt-dlp.exe.new" del "yt-dlp.exe" & ren "yt-dlp.exe.new" "yt-dlp.exe" rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Paste url into clipboard.txt... set clipboard_link=%RANDOM%%RANDOM% winclip.exe -p %clipboard_link%.txt rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Download Live Chat files to randomly named folder using youtube-dl (using clipboard.txt). rem Setting a "%RANDOM%" folder here allows multiple downloads to take place at the same time... set download_temp=%RANDOM%%RANDOM% md %download_temp% rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Download Live Chat to "%download_temp%" folder using yt-dlp (using clipboard.txt). start /wait /min yt-dlp.exe --ignore-errors --skip-download --datebefore %year%%month%%day% --write-sub -o "%download_temp%\%%(upload_date)s - %%(uploader)s - %%(title)s.%%(ext)s" -a %clipboard_link%.txt rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Delete the now useless clipboard.txt file... del %clipboard_link%.txt pause rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Check if any anything was downloaded... if not exist "%download_temp%\*.*" goto :fail_message if exist "%download_temp%\*.*" goto :continue rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- :continue rem Create "NEW\Live Chat" folder. if not exist "..\NEW\Live Chat" md "..\NEW\Live Chat" rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- rem Remove non-standard characters from file names in the "%download_temp%" folder. xcopy "SpecialChar\*.bat" "%download_temp%" /i /r /v /k /f /c /h /y >nul 2>&1 cd /d %download_temp% call rename.bat >nul 2>&1 cd /d .. del "%download_temp%\*.bat" rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- echo. echo Cleaning up Live Chat files to make them readable... echo. echo Please wait... rem Clean up the downloaded Live Chat files with powershell. xcopy "ReplaceText\Cleanup_LiveChat.txt" "%download_temp%" /i /r /v /k /f /c /h /y >nul 2>&1 cd /d %download_temp% ren "Cleanup_LiveChat.txt" "Cleanup_LiveChat.cmd" call "Cleanup_LiveChat.cmd" >nul 2>&1 del "Cleanup_LiveChat.cmd" cd /d .. rem ---------------------------------------------------------------------------------------------------------- rem Move the Live Chat files to "NEW\Live Chat" move /y "%download_temp%\*" "..\NEW\Live Chat" >nul 2>&1 rd /s /q "%download_temp%\" rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- echo. echo Finished downloading Live Chats from before date: %day%\%month%\%year% timeout 15 >nul goto finished rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- :fail_message echo. echo Unfortunately this video/channel did not have echo any Live Chats from before date: %day%\%month%\%year% echo. echo Exiting... timeout 15 >nul goto finished rem ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- :finished exit 

Cheers folks.

2

3 Answers

I found a workaround, but it doesn't answer why the original problem happens.

The workaround is to attempt to delete the %download_temp% with rd (with no switches, so the folder only gets deleted if it's empty).

It does delete it, thus making the goto commands work as they should:

rem Delete %download_temp% if it is empty (workaround for goto)... rd "%download_temp%" rem Check if any anything was downloaded... if not exist "%download_temp%\*.*" goto :fail_message if exist "%download_temp%\*.*" goto :continue 

Microsoft why u do this!

Your test does not work since . and .. exist in all folders and will be found by the mask of *.*.

Here is how to check for an empty folder. (The following code is untested.)

:: Is folder empty set _TMP= for /f "delims=" %%a in ('dir /a /b "%download_temp%"') do set _TMP=%%a IF {%_TMP%}=={} ( set _empty=Empty ) ELSE ( set _empty=Not Empty ) 
2

One simple line:

2>&1 >nul where "%download_temp%:*" && goto :fail_message || goto :continue

Or in two simple lines:

2>&1 >nul where "%download_temp%:*" && ( goto :fail_message) || goto :continue
where path:* find file without name ==> .txt find file without .eXtension ==> file find file with name.eXtension ==> file.txt

And you easily apply operator to check:

where "%download_temp%:*" return 0 to successfully case do () where "%download_temp%:*" return non 0 to unsuccessfully case do ()

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like