gpt4 book ai didi

Windows 'dir' 命令 : sort files by date when '/s' is specified

转载 作者:可可西里 更新时间:2023-11-01 11:15:29 26 4
gpt4 key购买 nike

目标: 我想使用批处理文件将具有特定扩展名的最新文件从“源目录”复制到“目标目录”。最新的文件可能在源目录下的几个子目录下。

This question/answer正是我想要的,但是当指定 /s 选项时它似乎没有排序(如 this comment would suggest ):

FOR /F "delims=|" %%I IN ('DIR "K:\path\tp\source\dir\*.ext" /B /S /O:D') DO SET NewestFile=%%I
copy "%NewestFile%" "C:\path\to\destination\dir"

你可以自己测试DIR "K:\path\tp\source\dir\*.ext"/B/S/O:D 看看它没有排序。

我试过的:这个命令本身确实有效:DIR "K:\path\tp\source\dir\*.ext"/S/B | sort 但我不知道如何在 for 循环中使用它(批处理文件在我确定错误之前退出 - 即使最后有一个 pause)。

有什么想法吗?

参见:dir docs

最佳答案

如果需要,您可以从批处理文件调用 Powershell。此代码可以在较新版本的 Powershell 上缩短。

for /F "delims=" %%G IN ('powershell "gci -rec | where { ! $_.PSIsContainer } | sort LastWriteTime | select -last 1 | Select-Object fullname"') do set NewestFile=%%G

完整答案:

cd /d "K:\path\to\source\dir"
for /F "delims=" %%G IN ('powershell "gci -rec *.ext | where { ! $_.PSIsContainer } | sort LastWriteTime | select -last 1 | Select-Object fullname"') do set NewestFile=%%G
copy "%NewestFile%" "C:\path\to\dest\dir"

关于Windows 'dir' 命令 : sort files by date when '/s' is specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com