Home > Enterprise >  Create a batch file to create a CSV only containing the names of the newest file and date within mul
Create a batch file to create a CSV only containing the names of the newest file and date within mul

Time:07-31

I have managed to find a handful of scripts to allow me to create a csv that contains all files within a directory and it's sub directories, only pulling specific file types (.pdf for my tests) and sort them by the most recent modified date. However I can not find a way to cut the list down to only the most recently modified file from each folder.

For instance lets say I have folder "A" and in folder "A" I have folders "1-5" I would like to be able to look into each of the folders 1-5, note which files is the most recently modified and report to a csv with the information as such

File path Filename Date Modified
C:\A\1 Name.pdf 7/29/2022
C:\A\2 Other.pdf 5/10/2021

Some of the batch scripts I have been able to find and work through are below but I haven't been able to combine them in any meaningful way.

for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i echo The most recently created file is %LAST% pause

and

(FOR /f "delims=" %%a IN ('dir *.pdf /s /o-d /b /a-d') DO (
    FOR /f "tokens=1-3*" %%x IN ('dir /a-d /tc "%%~a"^|findstr "^[0-9]"') DO (
        ECHO "%%a",%%~ta,%%x %%y %%z
    )
))>DIR.csv
TYPE DIR.csv```

CodePudding user response:

@ECHO OFF
SETLOCAL

rem switch to specified directory

PUSHD "%~1"

(
ECHO File path,Filename,Date Modified

FOR /f "delims=" %%e IN ('dir /ad /b') DO (
 SET "first=Y"
 FOR /f %%o IN ('dir /b /o-d /a-d "%%~fe\*.bat" 2^>nul') DO (
  FOR /f %%b IN ('dir /o-d /a-d "%%~fe\*.bat" 2^>nul^|find "/" ') DO IF DEFINED first (
   SET "first="
   ECHO            
  • Related