date - Batch Code to Skip Script if File is too Old -
my problem
as there ton of threads address 'using batch file return file modify date' (or delete files older than, etc) - let me first specify issue.
i'm looking create batch (not powershell, etc) return last modify date of specific file given unc path , filename.
code, attempt 1
i've taken peek @ few potential solutions on other threads, i've run number of unique issues. first , obvious solution "forfiles" command in batch. example:
set mypath=\\myuncpath set myfile=myfilename.csv forfiles /p "%mypath%" /m %myfile% /c "goto oldfile" /d -6 thus, if file older want -- jump specific section of batch that. however, yields error:
error: unc paths (\machine\share) not supported.
however, cmd won't work due use of unc (which critical batch called system's task scheduler). seems 'forfiles' cmd out.
code, attempt 2
i go more round way of doing it, retrieving last modified date of file (which in batch return string). can truncate string necessary date values, convert date, , compare current date. that, i've looked using loop such as:
set myfile=\\myuncpath\myfilename.csv echo %myfile% pause %%f in (%myfile%) set myfiledate=%%~tf echo %myfiledate% pause though first debug echo provides proper full file name, second debug returns echo off., tells me it's either not finding file or loop isn't returning file date. not sure why.
i've tried minor changes double check environmental variable syntax:
for %%f in (%myfile%) set myfiledate=%%~ta returns %ta
and finally:
for %%f in (%myfile%) set myfiledate=%~ta which without '%', crashes batch.
i'm @ loss @ point. tips or guidance appreciated!
using forfiles unc path can used using pushd
for echoing file older x in unc path, use
pushd %mypath% &&( forfiles -s -m %myfile% -d -6 -c "cmd /c echo /q @file ) & popd here 1 way how use goto if file older x found in unc path using examples.
if not exist "c:\temp" mkdir c:\temp if not exist "c:\temp\test.txt" echo.>"c:\temp\test.txt" break>"c:\temp\test.txt" set mypath=\\myuncpath set myfile=myfilename.csv pushd %mypath% &&( forfiles -s -m %myfile% -d -6 -c "cmd /c echo /q @file >> c:\temp\test.txt" ) & popd /f %%i in ("c:\temp\test.txt") set size=%%~zi if %size% gtr 0 goto next :next
Comments
Post a Comment