如何在不使用 FOR 循环的情况下获取目录中的最新文件?
How to get most recent file in a directory without using a FOR loop?
我试图在不使用 for
循环的情况下编写一个 returns 目录中最新文件的批处理文件。我试了很多,还是不行。
那么有没有一种方法可以不使用 for
循环来获取最新的文件?
@echo off
cd D:\Applications
for /f "delims=" %%i in ('dir /b/a-d/od/t:c') do set RECENT="%%~nxi"
echo ..... starting jar........
start java -jar %RECENT%
echo ....jar started.....
exit 0
执行在开始时卡住 java,它不会转到下一行或退出 0。
我不确定你为什么不想使用 cmd
中最强大的命令,但为了回答你的问题:
dir /o-d /b "C:\my folder\*" >tmp
<tmp set /p "latest="
del tmp
echo the latest file is %latest%
可以使用以下代码使用命令 FOR:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
cd /D "D:\Applications" 2>nul || (echo ERROR: Directory D:\Applications does not exist.& goto EndBatch)
for /F "eol=| delims=" %%i in ('dir /A-D /B /O-D /TC 2^>nul') do set "RECENT=%%i" & goto StartJava
echo WARNING: Could not find any file in directory: "%CD%"& goto EndBatch
:StartJava
if exist %SystemRoot%\System32\where.exe %SystemRoot%\System32\where.exe java.exe >nul 2>nul
if errorlevel 1 echo ERROR: Could not find java.exe. Check environment variable PATH.& goto EndBatch
echo ..... Starting jar .....
start "Running %RECENT%" java.exe -jar "%RECENT%"
if not errorlevel 1 echo ..... jar started .....
:EndBatch
if errorlevel 1 echo/& pause
endlocal
代码中还添加了一些错误处理。
请注意,创建日期是在当前目录中创建文件的日期。因此,如果一个文件从目录 X
复制到目录 Y
,它的最后修改日期没有被修改,但是文件在目录 Y
中有一个新的创建日期,比它的最后一个更新修改日期。
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
cd /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?
start /?
where /?
另请参阅:
- Single line with multiple commands using Windows batch file
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/
- Why is no string output with 'echo %var%' after using 'set var = text' on command line?
我试图在不使用 for
循环的情况下编写一个 returns 目录中最新文件的批处理文件。我试了很多,还是不行。
那么有没有一种方法可以不使用 for
循环来获取最新的文件?
@echo off
cd D:\Applications
for /f "delims=" %%i in ('dir /b/a-d/od/t:c') do set RECENT="%%~nxi"
echo ..... starting jar........
start java -jar %RECENT%
echo ....jar started.....
exit 0
执行在开始时卡住 java,它不会转到下一行或退出 0。
我不确定你为什么不想使用 cmd
中最强大的命令,但为了回答你的问题:
dir /o-d /b "C:\my folder\*" >tmp
<tmp set /p "latest="
del tmp
echo the latest file is %latest%
可以使用以下代码使用命令 FOR:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
cd /D "D:\Applications" 2>nul || (echo ERROR: Directory D:\Applications does not exist.& goto EndBatch)
for /F "eol=| delims=" %%i in ('dir /A-D /B /O-D /TC 2^>nul') do set "RECENT=%%i" & goto StartJava
echo WARNING: Could not find any file in directory: "%CD%"& goto EndBatch
:StartJava
if exist %SystemRoot%\System32\where.exe %SystemRoot%\System32\where.exe java.exe >nul 2>nul
if errorlevel 1 echo ERROR: Could not find java.exe. Check environment variable PATH.& goto EndBatch
echo ..... Starting jar .....
start "Running %RECENT%" java.exe -jar "%RECENT%"
if not errorlevel 1 echo ..... jar started .....
:EndBatch
if errorlevel 1 echo/& pause
endlocal
代码中还添加了一些错误处理。
请注意,创建日期是在当前目录中创建文件的日期。因此,如果一个文件从目录 X
复制到目录 Y
,它的最后修改日期没有被修改,但是文件在目录 Y
中有一个新的创建日期,比它的最后一个更新修改日期。
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
cd /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?
start /?
where /?
另请参阅:
- Single line with multiple commands using Windows batch file
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/
- Why is no string output with 'echo %var%' after using 'set var = text' on command line?