Batch script to pick filename from a text file and find the latest file -
scenario: have multiple releases of product, , each release, folder created in main folder. file modified in various releases. have file names listed in text file. need script to:
- take each file name filenames.txt file
- search file name in entire directory (in releases)
- find latest file
- copy specified folder
i took various pieces of code found on stack overflow, , combined them code:
@echo off setlocal enabledelayedexpansion echo. /f "usebackq delims=" %%a in ("filenames.txt") ( set "x=%%a" echo '!x!' set ffpath=c:\svn\nlbavwdocsvn\rep_doc_erpln\trunk\erpln set newpath=c:\lavanya\extracted set newestdate=20160824 echo recursively searching %ffpath% /f %%i in ('dir %ffpath%\ !x! /a:-d /s /b') ( set fulldate=%%~ti echo %ffpath% rem set currdate yyyymmdd format. note: fail if regional settings changed. set currdate=!fulldate:~6,4!!fulldate:~0,2!!fulldate:~3,2! if !currdate! gtr !newestdate! ( set newestdate=!currdate! set newestfile=%%~fi ) echo copying %newestfile% %newpath% echo. copy /y "%newestfile%" "%newpath%" echo. ) ) pause
this code not working. , unable figure out error.
here script search modified file, using wmic
command retrieve last modification date/time in locale-independent manner (e. g., 20160824115500.000000+060
).
so every file name read list file .\filenames.txt
, directory tree routed @ directory c:\svn\nlbavwdocsvn\rep_doc_erpln\trunk\erpln
searched matching files recursively, , respective modify date/time stamp gathered. due format, simple greater-than (gtr
) comparison can done determine whether or not later point of time cached one; if criterion fulfilled, cache updated accordingly.
the upper-case rem
, echo
commands constitute placeholders real action performed on files. extend script there like. variable !lastfile!
holds full path each encountered file.
so here code:
@echo off setlocal enableextensions disabledelayedexpansion rem // define constants here: set "location=c:\svn\nlbavwdocsvn\rep_doc_erpln\trunk\erpln" set "filelist=.\filenames.txt" set "wmicprop=lastmodified" & rem // {creationdate | lastaccessed | lastmodified} pushd "%location%" || exit /b 1 /f "usebackq eol=| delims=" %%l in ("%filelist%") ( set "lastfile=" set "lastfage=00000000000000.000000+000" /f "eol=| delims=" %%f in ('dir /b /s /a:-d "%%~l"') ( set "file=%%f" setlocal enabledelayedexpansion set "file=!file:\=\\!" /f "tokens=2 delims==" %%j in (' 2^> nul wmic datafile ^(name^="!file!"^) %wmicprop% /value ^|^| ^ 2^> nul wmic datafile name^="!file!" %wmicprop% /value ') /f %%i in ("%%j") ( endlocal set "fage=%%i" setlocal enabledelayedexpansion if !fage! gtr !lastfage! ( endlocal set "lastfile=%%f" set "lastfage=%%i" setlocal enabledelayedexpansion ) ) endlocal ) if defined lastfile ( setlocal enabledelayedexpansion rem whatever want file here... echo newest file: "!lastfile!" endlocal ) ) popd endlocal exit /b
Comments
Post a Comment