#136564 - 2005-03-28 09:35 PM
not deleting file- why?
|
annie
Fresh Scripter
Registered: 2002-02-12
Posts: 10
|
Would someone be so kind as to look at this code and tell me why the files that are older than 3 days are not getting deleted? basically, I have to read the contents of a directory which will contain about 100 files over a span of several days. if the file is > 3 days old, get rid of it (so I don't hog space). It runs, and displays the file name correctly, but nothing seems to be happening, not sure whether it's the datecalc or my logic that's screwed up.
$date1 = @date
call "\\servername\scripts\DateCalc.udf"
C1 $filename1 = DIR("\\server\adlogs\log_dc1") WHILE $filename1 <> "" and @error = 0 $file = "\\server\adlogs\log_dc1\$filename1" ;comparing the date (if file is 3 days older than todays date, get rid of it) if DateCalc(@date,left(getfiletime($file),10)) > 3 ? $file "will be deleted" del $file endif DIR() ;retrieve next file LOOP return
exit
|
Top
|
|
|
|
#136566 - 2005-03-28 09:48 PM
Re: not deleting file- why?
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Two questions..
You have the DateCalc() UDF in the same script or is it being called?
Also, you would do better to do something like the following: Code:
$filen='log_dc1' $filename1 = DIR("\\server\adlogs\"+$filen) WHILE $filename1 <> "" AND @error = 0 $file = "\\server\adlogs\log_dc1\"+$filen ;comparing the date (if file is 3 days older than todays date, get rid of it) IF DateCalc(@date,left(getfiletime($file),10)) > 3 ? $file "will be deleted" DEL $file ENDIF DIR() ;retrieve next file LOOP RETURN
Here is one that I did to remove old Citrix Profiles that were older than 3 days ago Needs - DATECALC() WSHPIPE() MAILER() LOGGER() : Code:
CLS BREAK ON $log=@scriptdir+'\Profileremove.csv' IF EXIST($log) DEL $log ENDIF $date=split(@date,'/')[1]+'/'+split(@date,'/')[2]+'/'+split(@date,'/')[0] $logdata='Run date/time:'+$date+' '+@time+@crlf $logdata=$logdata+'Days ago,Location,Error Code'+@crlf+'-------------------' LOGGER($log,$logdata+@crlf)
FOR EACH $i IN Split('01 02 03') $loc='\\CITRIX'+$i+'\admin$$\profiles\' $rc=WshPipe('%comspec% /c dir '+$loc+'*.*',1) FOR EACH $line IN $rc IF NOT instr($line, 'File Not Found') ;?$line ; -- Convert American Dates to European format ;?$dtchk $name=split($line)[17] $dtchk=split(split($line)[0],'/')[2]+'/'+split(split($line)[0],'/')[0]+'/'+split(split($line)[0],'/')[1] $dt=DATECALC($dtchk,@date) IF val($dt)>2 AND len($name)>3 AND $name<>'All Users' AND $name<>'DEFAULT' ;? SHELL '%COMSPEC% /C RD /S /Q '+chr(34)+$loc+'\'+$name+chr(34) If @error<>$err=1 ELSE $err=0 ENDIF $logdata=''+$dt+','+$loc+$name+','+$err LOGGER($log,$logdata+@crlf) ENDIF ENDIF NEXT NEXT MAILER('user@@company.com',,,$log)
HTH,
Kent
|
Top
|
|
|
|
#136567 - 2005-03-28 09:56 PM
Re: not deleting file- why?
|
Anonymous
Anonymous
Unregistered
|
yes, I'm calling the datecalc udf up top and the log_dc1 is a FOLDER NAME. The files all reside under \\server\adlogs\logs_dc1\*.*. As I said, it's displaying the file name retrieved corrected. It's probably because of the del statement being incorrect. I'll retest now and update you. THANKS!!!
|
Top
|
|
|
|
#136568 - 2005-03-28 10:04 PM
Re: not deleting file- why?
|
Anonymous
Anonymous
Unregistered
|
nope that didn't work. substituting "Del \\server\adlogs\log_dc1\$file" for "DEL $file" didn't work because $file IS equal to \\server\adlogs\log_dc1\realfilename.log. The substitution reulted in "\\server\adlogs\log_dc1\\server\adlogs\log_dc1\realfilename.log which doesn't exist.
Kent - I'll try your method now, altho I'll have to figure out how to remove the european change, which isn't needed. THANKS ALL
|
Top
|
|
|
|
#136569 - 2005-03-28 10:31 PM
Re: not deleting file- why?
|
Anonymous
Anonymous
Unregistered
|
GRRRRRR
I've changed it to be this: $date1 = @date
call "\\server\scripts\DateCalc.udf"
C1 $c=0
$file=DIR("\\server\adlogs\logs_dc1\*.log") WHILE $file <> "" and @error=0 $c=$c+1 ? $file if DateCalc(@date,left(getfiletime($file),10)) > 2 ? $file del "\\monm607\adlogs\monm_dc1\$file" endif $file=DIR() LOOP
and I KNOW the file logic is working because it displays the file name, and I'm assuming that since I'm in the IF... THEN logic, the solution must be TRUE. so why isn't it executing the delete statement? I even tried shlling out with a comspec /s del "\\server\adlogs\log_dc1\$file" but no luck. why isn't the delete statement running? Sorry to be such a PITA
|
Top
|
|
|
|
#136570 - 2005-03-28 10:41 PM
Re: not deleting file- why?
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
here is another way...
needs the UDF's DirPlus() TimeConvert() FlipCTime()
Code:
Call 'g:\kix\udf\dirplus\dirplus.kix' Call 'g:\kix\udf\TimeConvert\TimeConvert.kix' Call 'g:\kix\udf\FlipcTime\flipcTime.kix'
$files = Dirplus('\\flag-fs01\sys\deltemp','/a-d') $3days = 259200
For Each $file In $files $ts = Split($file.DateLastModified) $ts[0] = Split($ts[0],"/") $ts[0] = $ts[0][2] + '/' + $ts[0][0] + '/' + $ts[0][1] $ts[1] = timeconvert($ts[1]+" "+$ts[2]) If flipctime(@DATE,@TIME) - flipctime($ts[0],$ts[1]) >= $3days ? "Delete! " $file.name " " $ts[0]
; --this needs to be commented out-- ;$file.delete
EndIf Next
Edited by Bryce (2005-03-28 10:49 PM)
|
Top
|
|
|
|
#136573 - 2005-03-29 11:37 AM
Re: not deleting file- why?
|
mima
Hey THIS is FUN
Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
|
I have also done a general program with an ini file thats control this kind of function. The ini file looks like this: Code:
# Day = Number of days from todays date that files should be kept. Day = 0 means that all file will be removed. # Path = The path where the script should look in. [Parameter] Day=3 Path=C:\SomePath
Here is the kix code, I use a function called FlipcTime() I found in someones kix library. Code:
$IniFile = ".\DelFile.ini" $LogFile = ".\DelFile.log"
$rc = RedirectOutput( "$LogFile" ,1 ) ? "Start " + @Date + " " + @Time
$OneDaySec = 86400 $TodayVal = FlipcTime(@DATE,"00:00")
If Exist( $IniFile ) $Day = ReadProfileString("$IniFile","Parameter","Day") $Path = Trim( ReadProfileString("$IniFile","Parameter","Path") ) Else ? "Cant find $IniFile" Exit EndIf
$DelValue = $TodayVal - $OneDaySec * $Day $FileName = Dir( $Path ) While $FileName <> "" And @ERROR = 0 $File = "$Path" + "\" + "$FileName" $FiAt = GetFileAttr( "$File" ) If $FiAt = 1 Or $FiAt = 2 Or $FiAt = 3 Or $FiAt = 32 Or $FiAt = 33 Or $FiAt = 34 Or $FiAt = 35 Or $FiAt = 128 ; Fil och ej Dir $FileDate = SubStr( GetFileTime( "$File" ) ,1,10) $FileVal = FlipcTime( $FileDate,"00:00" ) If $FileVal <= $DelValue Del "$File" /f /h ? "remove file $File " + " / $FileDate with errorcode @Error" EndIf EndIf $FileName = Dir() ; retrieve next file Loop
Exit
Function FlipcTime($date,$time,optional $tz) Dim $y,$m,$d $date = Split($date,"/") If Ubound($date) <> 2 Exit(1) EndIf $y=Val($date[0]) $m=Val($date[1]) $d=Val($date[2]) If $m<3 $m=$m+12 $y=$y-1 EndIf $Date=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306 $time = Split($time,":") Select Case Ubound($time)=1 ReDim preserve $time[2] $time[2]=0 Case Ubound($time)=2 Case 1 Exit(1) EndSelect $time = (Val($time[0])*3600)+(Val($time[1])*60)+Val($time[2]) $flipctime = IIf($tz,(($date-719163)*86400 + $time)-($tz*3600),($date-719163)*86400 + $time) EndFunction
I then schedule this and I have this running on several servers to remove logs from several programs. Works perfect.
Mima
|
Top
|
|
|
|
#136574 - 2005-03-29 03:21 PM
Re: not deleting file- why?
|
maciep
Korg Regular
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
Have you tried using @error/@serror to see why it is not deleting the file?
Code:
$file=DIR("\\server\adlogs\logs_dc1\*.log") WHILE $file <> "" and @error=0 $c=$c+1 ? $file if DateCalc(@date,left(getfiletime($file),10)) > 2 ? $file del "\\monm607\adlogs\monm_dc1\$file" ? @serror endif $file=DIR() LOOP
|
Top
|
|
|
|
#136576 - 2005-03-29 07:54 PM
Re: not deleting file- why?
|
annie
Fresh Scripter
Registered: 2002-02-12
Posts: 10
|
Mima - THANK YOU!!! your code worked perfectly, and it's now cleaning like a champ! I have one small issue with the scheduling of it (runs if I run it real-time, but doesn't run if called in the scheduler, but I'm a HECK of a lot farther along than I was yesterday - THANK YOU!!
|
Top
|
|
|
|
#136577 - 2005-03-29 08:01 PM
Re: not deleting file- why?
|
maciep
Korg Regular
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
So the files were either read-only or hidden/system (or both) i guess
Code:
Del "$File" /f /h
from the manual
Quote:
Del
Action: Deletes one or more files. Syntax: DEL "file name" [/c] [/f] [/h] [/s] /c Continue deleting files, even if error(s) occur. /f Delete read-only files. /h Deletes hidden and system files also. /s Delete specified files from all subdirectories. Remarks: DEL does not prompt the user to confirm the deletion. Wildcards are supported.
|
Top
|
|
|
|
#136578 - 2005-03-30 09:07 AM
Re: not deleting file- why?
|
mima
Hey THIS is FUN
Registered: 2002-01-25
Posts: 217
Loc: Jönköping, Sweden
|
Hi annie You said Quote:
I have one small issue with the scheduling of it (runs if I run it real-time, but doesn't run if called in the scheduler
Have you made a command file ex RemoveFile.cmd with the following Code:
C:\Kix\Kix32.exe C:\Kix\RemoveFile.kix
and then scheduling the command file.
/Mima
|
Top
|
|
|
|
#136579 - 2005-03-30 07:53 PM
Re: not deleting file- why?
|
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
Maybe a stupid one....but if you don’t' shoot you'll never hit anything so here it goes....
What happens if you run the script locally on the server and change the del \\server stuff to the local path? Maybe something gets screwed up somehow in the del \\server\share\...... command.
[edit] Woops should read a little better, would have seen the success post then. [/edit]
Edited by R2D2 (2005-03-30 07:56 PM)
|
Top
|
|
|
|
#136580 - 2005-04-26 04:02 AM
Re: not deleting file- why?
|
Sopmod
Lurker
Registered: 2005-04-22
Posts: 3
|
Quote:
I have also done a general program with an ini file thats control this kind of function. The ini file looks like this: Code:
# Day = Number of days from todays date that files should be kept. Day = 0 means that all file will be removed. # Path = The path where the script should look in. [Parameter] Day=3 Path=C:\SomePath
Here is the kix code, I use a function called FlipcTime() I found in someones kix library. Code:
$IniFile = ".\DelFile.ini" $LogFile = ".\DelFile.log"
$rc = RedirectOutput( "$LogFile" ,1 ) ? "Start " + @Date + " " + @Time
$OneDaySec = 86400 $TodayVal = FlipcTime(@DATE,"00:00")
If Exist( $IniFile ) $Day = ReadProfileString("$IniFile","Parameter","Day") $Path = Trim( ReadProfileString("$IniFile","Parameter","Path") ) Else ? "Cant find $IniFile" Exit EndIf
$DelValue = $TodayVal - $OneDaySec * $Day $FileName = Dir( $Path ) While $FileName <> "" And @ERROR = 0 $File = "$Path" + "\" + "$FileName" $FiAt = GetFileAttr( "$File" ) If $FiAt = 1 Or $FiAt = 2 Or $FiAt = 3 Or $FiAt = 32 Or $FiAt = 33 Or $FiAt = 34 Or $FiAt = 35 Or $FiAt = 128 ; Fil och ej Dir $FileDate = SubStr( GetFileTime( "$File" ) ,1,10) $FileVal = FlipcTime( $FileDate,"00:00" ) If $FileVal <= $DelValue Del "$File" /f /h ? "remove file $File " + " / $FileDate with errorcode @Error" EndIf EndIf $FileName = Dir() ; retrieve next file Loop
Exit
Function FlipcTime($date,$time,optional $tz) Dim $y,$m,$d $date = Split($date,"/") If Ubound($date) <> 2 Exit(1) EndIf $y=Val($date[0]) $m=Val($date[1]) $d=Val($date[2]) If $m<3 $m=$m+12 $y=$y-1 EndIf $Date=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306 $time = Split($time,":") Select Case Ubound($time)=1 ReDim preserve $time[2] $time[2]=0 Case Ubound($time)=2 Case 1 Exit(1) EndSelect $time = (Val($time[0])*3600)+(Val($time[1])*60)+Val($time[2]) $flipctime = IIf($tz,(($date-719163)*86400 + $time)-($tz*3600),($date-719163)*86400 + $time) EndFunction
I then schedule this and I have this running on several servers to remove logs from several programs. Works perfect.
Mima
Greetings, this is my first post and I'm certainly NOT of the programmer mindset so please excuse any terminlogy "dumbness" should there be any.
I have a task to delete audit files older than 90 days in a proxy server. The files to be deleted are log files with the suffix of *-access and *-access.gz.
I've tested Mima's script and this is great for a blanket deletion of everything with a directory however the folder is shared with other files which need to remain.
Would it be possible for Mima or one of you gurus to alter the script to select these file types, or post a suitable script which will achieve this?
Thank you for your assistance.
|
Top
|
|
|
|
#136581 - 2005-04-29 07:13 AM
Re: not deleting file- why?
|
Sopmod
Lurker
Registered: 2005-04-22
Posts: 3
|
/Bump
Can anyone provide feedback?
Also what I need to do is search a directory for all files with the suffixs of *-access and *-access.gz that are 30 days old. I then need to move these to another location on a different drive and directory.
The move is from d:\inetpub\ftproot\proxylogs to e:\arcproxylogs.
Could anyone provide a suitable script or guidence to achieve this?
Thank you.
|
Top
|
|
|
|
#136582 - 2005-04-29 08:04 AM
Re: not deleting file- why?
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Hey guy... I love KiXtart but for your purposes I really think the utility named RoboCopy from Microsoft is really the best tool for this. Yes you could do it via scripting alone, but please take a look at RoboCopy which can automatically do this for you in a much more secure and predictable manner with a ton of options too if wanted.
Download the Windows Server 2003 Resource Kit Tools and install or extract RoboCopy and read the docs on it. I think you'll see how easy and well suited it is for this task.
Windows Server 2003 Resource Kit Tools - ROBOCOPY
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 239 anonymous users online.
|
|
|