批处理脚本:复制非空目录下的最新文件夹

Batch script: copy the newest folder in the directory that is nonempty

我觉得问题不难解决。

例如,我有一个版本号为 1.1.0.1、1.1.0.2 的文件夹。我想获取最新的 modified 文件夹,即 nonempty 意味着它在某处有一个文件(它总是具有相同的名称和位置该文件夹),而不仅仅是更多文件夹,并将该文件复制到其他地方。

现在我有

FOR /F "delims=" %%i IN ('dir /b /ad-h /t:c /od') do (set latest=%%i)

找到最新的文件夹,但这不起作用,而是返回一个随机文件。任何帮助将不胜感激。

我建议

set "latest="
FOR /F "delims=" %%i IN ('dir /b /ad-h /o-d') do if not defined latest if exist ".\%%i\x64\abc.exe" set "latest=%%i\x64\abc.exe"

应该可以,在每个目录中以相反的日期顺序查找文件 .\x64\abc.exe,并在设置变量 latest 后停止该过程。

Sorry about this folks, I misread the title and thought this was a bash script rather than a batch script. I'm leaving it up on the off chance it may be useful getting to the correct answer.

假设您的目录如下所示:

1.1/
1.2/some_file
2.4/some_other_file

然后这个命令:

 find . -maxdepth 1 -not -name '.' -print0 |\
   xargs -0 -r -n1 -P0 du -s |\
   egrep -v '^0\s+' |\
   cut -f2 |\
   cut -d'/' -f2- |\
   sort -n |\
   head -n1

应该给你找到编号最小的非空文件或文件夹。

这是正在发生的事情:

  1. 查找当前目录中所有未命名为“.”的文件,作为空分隔列表发出。
  2. 将每个文件放在一个单独的进程中,运行du确定每个文件的大小file/folder。
  3. 过滤掉大小为零的所有内容。
  4. 删除尺寸(不再需要)。
  5. 删除 du 作为其输出的一部分包含的文件名前面的“./”。
  6. 按文件名排序(-n是数字排序,这里假设数字前面没有前缀。sort如果有可以处理,查看联机帮助页)。
  7. 丢弃除第一行以外的所有内容。