Generate a list of the newest file in each subdirectory in windows batch -
i have need determine newest file in each subdirectory under root folder , output list text file showing both filename , creation date. example, if root directory d:\data contains total of 4 subdirectories, , 1 of 4 contain subdirectory, output file should contain list of files (not including created folders) this:
d:\data\folder1\filename 02/12/14
d:\data\folder2\filename 03/02/14
d:\data\folder3\filename 03/15/14
d:\data\folder4\folder01\filename 01/22/14
i have checked other posts, none seem specifically...
two options
@echo off setlocal enableextensions enabledelayedexpansion set "rootfolder=c:\somewhere" :: option 1 echo -------------------------------------------------------------------------------- /d /r "%rootfolder%" %%a in (.) ( set "first=" /f "delims=" %%b in ('dir /o-d /a-d /b "%%~fa\*" 2^>nul') if not defined first ( set "first=1" %%c in ("%%~fa\%%b") 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=" /f "usebackq delims=" %%a in ("%tempfile%") if not "%%~dpa"=="!oldfolder!" ( echo %%~ta %%~fa set "oldfolder=%%~dpa" ) del /q "%tempfile%" >nul 2>nul
option 1 recurse on folders structure , each one, dir
command executed retrieve list of files in folder in descending date order. first in list rececent one.
option 2 use 1 dir
command retrieve full list of files in descending date order , save retrieved information in temporary file. file processed. each time name of folder changes, output first line recent file in folder.
option 1 start earlier echo information, multiple commands used, require more time execute, needs less memory retrieve file list of 1 folder each time.
option 2 uses less commands , faster, uses temporary file , requires more memory option 1 full file list loaded in memory.
select depending of how deep folder structure, how files have, how memory, .... test.
Comments
Post a Comment