在Windows批处理的每个子目录中生成最新文件的列表 [英] Generate a list of the newest file in each subdirectory in windows batch

查看:340
本文介绍了在Windows批处理的每个子目录中生成最新文件的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定根文件夹下每个子目录中的最新文件,并将该列表输出到同时显示文件名和创建日期的文本文件中.例如,如果根目录是D:\ Data,它总共包含4个子目录,而这4个子目录之一包含一个子目录,则我的输出文件应仅包含如下文件的列表(不包括创建的文件夹):

I have a need to determine what the newest file is in each subdirectory under a root folder and output that list to a text file showing both the filename and creation date. As an example, if the root directory is D:\Data which contains a total of 4 subdirectories, and one of those 4 contain a subdirectory, my output file should contain a list of only files (not including created folders) something like this:

D:\ Data \ folder1 \ filename 14/12/14

D:\Data\folder1\filename 02/12/14

D:\ Data \ folder2 \ filename 2014年3月2日

D:\Data\folder2\filename 03/02/14

D:\ Data \ folder3 \ filename 03/15/14

D:\Data\folder3\filename 03/15/14

D:\ Data \ folder4 \ folder01 \ filename 01/22/14

D:\Data\folder4\folder01\filename 01/22/14

我检查了其他帖子,但似乎没有人专门这样做...

I have checked other posts, but none seem do this specifically...

推荐答案

两个选项

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "rootFolder=c:\somewhere"

:: option 1
    echo --------------------------------------------------------------------------------
    for /d /r "%rootFolder%" %%a in (.) do (
        set "first="
        for /f "delims=" %%b in ('dir /o-d /a-d /b "%%~fa\*" 2^>nul') do if not defined first (
            set "first=1"
            for %%c in ("%%~fa\%%b") do echo %%~tc %%~fc
        )
    )

:: option 2
    echo --------------------------------------------------------------------------------
    set "tempFile=%temp%\%~nx0.%random%.tmp"
    dir /a-d /b /s /o-d "%rootFolder%" 2>nul >"%tempFile%"

    set "oldFolder="
    for /f "usebackq delims=" %%a in ("%tempFile%") do if not "%%~dpa"=="!oldFolder!" (
        echo %%~ta %%~fa
        set "oldFolder=%%~dpa"
    )

    del /q "%tempFile%" >nul 2>nul

选项1将遍历文件夹结构,并且对于每个文件夹结构,均执行dir命令以按降序检索文件夹中的文件列表.列表中的第一个是最近度最高的.

Option 1 will recurse over the folders structure and for each one, a dir command is executed to retrieve the list of files in the folder in descending date order. The first in the list is the most rececent one.

选项2将仅使用一个dir命令以降序检索文件的完整列表,并将检索到的信息保存在一个临时文件中.然后处理文件.每次文件夹名称更改时,输出第一行,它将是该文件夹中的最新文件.

Option 2 will use only one dir command to retrieve the full list of files in descending date order and will save the retrieved information in a temporary file. Then the file is processed. Each time the name of the folder changes, output the first line that will be the most recent file in the folder.

选项1会更早开始回显信息,但是由于使用了多个命令,因此将需要更多的时间来执行,但是需要的内存更少,因为它每次只会检索一个文件夹的文件列表.

Option 1 will start earlier to echo information, but as multiple commands are used, it will require more time to execute, but needs less memory as it will only retrieve the file list of one folder each time.

选项2使用更少的命令并且速度更快,但是使用临时文件并且需要更多的内存,因为选项1的完整文件列表将被加载到内存中.

Option 2 uses less commands and will be faster, but uses a temporary file and requires more memory that Option 1 as the full file list will be loaded in memory.

根据您的文件夹结构的深浅,文件的数量,内存的大小...进行测试.

Select depending of how deep is your folder structure, how much files you have, how much memory, .... just test.

这篇关于在Windows批处理的每个子目录中生成最新文件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆