Need help with script from this site
Moderator: DosItHelp
Need help with script from this site
I'm having an issue getting 2 scripts featured on this site to work. I know I'm missing something simple. The 2 I'm trying to use are the top 2 @ http://www.dostips.com/DtTipsDateTime.php
I want the batch file to figure out the date, then delete anything in that directory thats 180 days or over.
Any help would be appreciated.
I want the batch file to figure out the date, then delete anything in that directory thats 180 days or over.
Any help would be appreciated.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Need help with script from this site
natively, you can use vbscript
or if you can, download findutils
Code: Select all
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp")
For Each efile in objFolder.Files
'Wscript.Echo "file is " & efile
'WScript.Echo eFile.DateLastModified
If DateDiff("d",eFile.DateLastModified,Now) >= 180 Then
WScript.Echo "file found that is 180 days old: " & efile
WScript.Echo eFile.DateLastModified
'objFSO.DeleteFile(eFile) 'remove the quote to do actual deletion
End If
Next
or if you can, download findutils
Code: Select all
C:\test>find c:\test -mtime +180 -delete
Re: Need help with script from this site
While I'm sure both work well, it's outside the scope of this project. If I can get this part of the batch script to work correctly, it'll be added to a much larger batch file that does cleaning on servers.
Re: Need help with script from this site
Spellman
Try use Forfiles
Try use Forfiles
Code: Select all
/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"dd.MM.yyyy" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.
Re: Need help with script from this site
nevertheless WSH may successfully used from CMD by single or multiple script files, it is more fast & simple than via native CMD calculatingSpellman wrote:While I'm sure both work well, it's outside the scope of this project.
Re: Need help with script from this site
Would like to use forfiles, however it's not on every server. The script will need to run on WinNT, 2000, 2003, 2008. Obviously, have to make it for the weakest link
Re: Need help with script from this site
it is sample of effective combinaition BAT & JS in one CMD-file:
Code: Select all
@set @x=0 /*
@echo off
set DIR=%TEMP%\*
set OLD=3000
for /f "delims=" %%i in (
'dir /b/s/a-d "%DIR%"^|cscript //nologo /e:jscript "%~0" "FileOldDays(%OLD%)"'
) do del /f/q "%%~i"
EXIT
*/
eval(WScript.Arguments.Item(0));
// ===== functions block =======
function FileOldDays(olddays) {
var str, obj, dof, now, dif
var fso = WScript.CreateObject("Scripting.FileSystemObject");
while (!WScript.StdIn.AtEndOfStream) {
str = WScript.StdIn.ReadLine();
if (fso.FileExists(str)) {
obj = fso.GetFile(str);
dof = Date.parse(obj.DateLastModified);
now = new Date()
dif = Math.floor((now-dof)/(1000*60*60*24))
if (dif>=olddays) WScript.StdOut.WriteLine(str);
}
}
}
Re: Need help with script from this site
I do appreciate the help amel27
While I do fully plan on writing this in a few other languages, I'd like to finish the batch version before I move on to a vbscript/jscript.
I've run into a new issue. I figured (I think) what I was doing wrong and got it to work. HOWEVER, when I do controlled test runs on a remote server I get drastic results.
When I ran the batch file, it saw FileA at 70 days old (which is correct). When I ran it on the remote server it found it to be just over 2 millenia old. It saw the file as 730,555 days old. Same script, same file, just ran from different servers. The date is correct on both, though I'm not even sure it's possible to set your calendar off by 2,000 years.
Here's what I'm using for code... Most of it is from this site with one addition by me. (First part "TimeDelete" i added so that I could add it to my script and have it loop threw this with different directories and days.)
Sorry for the wall of text, but unless you have what I'm using it's impossible to be 100% on. And for what it's worth I use Notepad++
:TimeDelete dir days -- Sets the variables below for dymanic uses
:: -- dir [in] - Parent directory to scan
:: -- day [in] - Anything after this in days will be removed
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
cd /d "%1"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
echo.%%~nxF is !diff! days old
call:DeleteIfOld %%~nxF %2
)
ECHO.&PAUSE&GOTO:EOF
eleteIfOld name days tnow -- deletes file or directory if older than given number of days
:: -- name [in] - name of file or directory
:: -- days [in] - number of days to expire
:: -- tnow [in] - today's date in julia days
SETLOCAL
set "days=%~2"
if %diff% LEQ %days% EXIT /b
set "attr=%~a1"
if /i "%attr:~0,1%"=="d" (
rd /Q /S "%~1"
) ELSE (
del /Q /S /F "%~1"
)
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
While I do fully plan on writing this in a few other languages, I'd like to finish the batch version before I move on to a vbscript/jscript.
I've run into a new issue. I figured (I think) what I was doing wrong and got it to work. HOWEVER, when I do controlled test runs on a remote server I get drastic results.
When I ran the batch file, it saw FileA at 70 days old (which is correct). When I ran it on the remote server it found it to be just over 2 millenia old. It saw the file as 730,555 days old. Same script, same file, just ran from different servers. The date is correct on both, though I'm not even sure it's possible to set your calendar off by 2,000 years.
Here's what I'm using for code... Most of it is from this site with one addition by me. (First part "TimeDelete" i added so that I could add it to my script and have it loop threw this with different directories and days.)
Sorry for the wall of text, but unless you have what I'm using it's impossible to be 100% on. And for what it's worth I use Notepad++
:TimeDelete dir days -- Sets the variables below for dymanic uses
:: -- dir [in] - Parent directory to scan
:: -- day [in] - Anything after this in days will be removed
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
cd /d "%1"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
echo.%%~nxF is !diff! days old
call:DeleteIfOld %%~nxF %2
)
ECHO.&PAUSE&GOTO:EOF
eleteIfOld name days tnow -- deletes file or directory if older than given number of days
:: -- name [in] - name of file or directory
:: -- days [in] - number of days to expire
:: -- tnow [in] - today's date in julia days
SETLOCAL
set "days=%~2"
if %diff% LEQ %days% EXIT /b
set "attr=%~a1"
if /i "%attr:~0,1%"=="d" (
rd /Q /S "%~1"
) ELSE (
del /Q /S /F "%~1"
)
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b