如果不存在(*.*),则无法在空文件夹上工作(Windows 批处理)

如果不存在(*.*),则无法在空文件夹上工作(Windows 批处理)

我认为我以前从未遇到过这种情况。使用以下命令:

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

我可以在执行这 2 个命令之前暂停一下,然后查看%download_temp%文件夹,发现文件夹肯定是空的,我已将隐藏文件和“超级隐藏”系统文件设置为在 Windows 中显示,我以管理员身份登录,文件夹肯定是空的。我还知道所选的下载位置没有我要下载的文件类型 - 因此我有多个理由确定文件夹是空的。

尽管如此,上述命令仍然跳转到标签:continue而不是应该跳转到的位置,即:fail_message标签。

我注意到一个怪癖——如果我改变*.*文件类型,*.json那么一切都会正常工作,但有时它可能不是一个 .json 文件,因此使用*.*

我也尝试过交换*.**但得到的效果是一样的。

这是整个批处理文件,上面显示的命令之前没有其他goto命令,并且我仔细检查了标签是否正确:

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

大家干杯!

答案1

简单一行:

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

或者用两行简单的代码:

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

而且您可以轻松地应用运算符来检查:

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

答案2

我找到了一种解决方法,但它并不能解释为什么会发生最初的问题。

解决方法是尝试删除%download_temp%rd没有开关,因此只有文件夹为空时才会被删除)。

它确实删除了它,从而使goto命令正常工作:

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

微软你为什么这样做!

答案3

您的测试不起作用,因为...存在于所有文件夹中,并且可以通过的掩码找到*.*

以下是检查文件夹是否为空的方法。(以下代码未经测试。)

:: 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
)

答案4

抱歉回复迟了。
“Harrymc”说得对。

这是目录列表中“.”和“..”条目的问题。
以前“如果不存在 *.*”不会在列表中“看到”一个点和两个点 - 所以它工作正常。
目前“如果不存在 *.*”总是“看到”一个点和两个点作为文件,目录永远不会为空。
它适用于 FAT16 或 FAT32,但不适用于 NTFS 或 Windows Server 2003 之前的版本(我不记得了)。
很多年前我必须更改我的脚本。
我使用批处理:

dir %1 /b/a-d|find/v "*.*">nul && (set fifexist=1)||(set fifexist=0)

问候

相关内容