使用批处理命令找到最新的文件路径 [英] find path of latest file using batch command
问题描述
我在我的文件夹中的多个子文件夹名为file.txt的文件。我想获得最新file.txt的路径
FOR / R %%我('file.txt的DIR / B / O:-D')方法设置= %%我呼应最近的子文件夹:%A%
产生了具有FILE.TXT最新创建的文件夹,而我想其中有最新file.txt的文件夹
关闭@echo
SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion 对于一个%%在/ F,*记号= 1,2('
使用Robocopy。 。 file.txt的/ L / NOCOPY /是/ S / NC / NS / TS / NDL / NJH / NJS
^ |排序/ R 2 ^> NUL
^ | CMD / V / Q / C设置/ P =&安培;!!回声(^^
')做(
发现回声文件:%%Ç
回声文件的时间戳(UTC ^):%%一个%% b
呼应文件文件夹:%%〜DPC
)
这将使用的Robocopy
命令枚举当前的活动目录下的所有 file.txt的
文件,而不复制任何东西,但生成所有符合条件的文件列表,用 YYYY / MM / DD HH:NN:SS
UTC时间戳。则列表按降序和时间戳进行排序仅在readed和呼应由 FOR / F
标记生成器和检索日期,时间进行处理,输出的第一行并用全路径文件。
如果仅需要的文件夹和文件的列表不是非常大,一个简化的版本可能是
关闭@echo
SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion 对于一个%%在/ F,*记号= 1,2('
使用Robocopy。 。 file.txt的/ L / NOCOPY /是/ S / NC / NS / TS / NDL / NJH / NJS
^ |排序/ R
')也将lastFolder = %%〜DPC&放大器;转到:完成
:完成
呼应最后一个文件夹:%lastFolder%
几乎相同,但,而不是包括在列表生成一个过滤器以仅检索第一行(如果文件列表是非常大的需要),在这里 FOR / F
将获取完整列表但第一个元素被处理后,我们跳出循环到指定的标签。
I have files named file.txt in multiple subfolders in my folder. I want to get the path of the latest file.txt
FOR /r %%i IN ('DIR file.txt /B /O:-D') DO SET a=%%i
echo Most recent subfolder: %a%
gives latest created folder having file.txt whereas I want the folder which has latest file.txt
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1,2,*" %%a in ('
robocopy . . file.txt /l /nocopy /is /s /nc /ns /ts /ndl /njh /njs
^| sort /r 2^> nul
^| cmd /v /q /c "set /p .=&echo(^!.^!"
') do (
echo File found : %%c
echo File timestamp (UTC^) : %%a %%b
echo Folder of file : %%~dpc
)
This will use the robocopy
command to enumerate all the file.txt
files under the current active directory, without copying anything but generating a list of all matching files with a yyyy/mm/dd hh:nn:ss
utc timestamp. Then the list is sorted on the timestamp in descending order and only the first line in the output readed and echoed to be processed by the for /f
tokenizer and retrieve the date, time and file with full path.
If only the folder is required and the list of files is not very large, a simplified version could be
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1,2,*" %%a in ('
robocopy . . file.txt /l /nocopy /is /s /nc /ns /ts /ndl /njh /njs
^| sort /r
') do set "lastFolder=%%~dpc" & goto :done
:done
echo Last folder : %lastFolder%
Almost the same, but instead of including a filter in the list generation to only retrieve the first line (required if the list of files is very large), here the for /f
will retrieve the full list but after the first element is processed we jump out of the loop to the indicated label.
这篇关于使用批处理命令找到最新的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!