#172845 - 2007-01-16 09:10 PM
How to find and delete a specific file
|
sgtdrpepper
Fresh Scripter
Registered: 2002-10-21
Posts: 9
|
Hello all I am new here and had a question. I would like to run a script that looks for a specific file and then deletes that file. I am sure this is eay to do but have limited experience with kix and did not find anything when searching.
Thanks
Edited by sgtdrpepper (2007-01-16 09:11 PM)
|
Top
|
|
|
|
#172847 - 2007-01-16 09:29 PM
Re: How to find and delete a specific file
[Re: Radimus]
|
sgtdrpepper
Fresh Scripter
Registered: 2002-10-21
Posts: 9
|
Is there a way to do this from within KIX? I can delete the file but i do not know the exact location on every pc. Is there a way search an then if found delete?
Thanks
Edited by sgtdrpepper (2007-01-16 09:35 PM)
|
Top
|
|
|
|
#172853 - 2007-01-17 12:05 AM
Re: How to find and delete a specific file
[Re: Björn]
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
read the switches again...
|
Top
|
|
|
|
#172854 - 2007-01-17 12:09 AM
Re: How to find and delete a specific file
[Re: sgtdrpepper]
|
dgraham
Fresh Scripter
Registered: 2006-04-11
Posts: 39
Loc: Santa Fe, New Mexico
|
I am working on a similar script. I have several thousand audio recordings. They are named with the following format YYYYMMDD-HHMMSS.wma The letters being:YearMonthDay-HourMinuteSecond. It is a long story, but the files are created on little hand held recorders, and the date and time info from these little recorders is extremely unreliable. I wrote a script that pulls the files from the recorders and puts them on a network share while also renaming them to the current date and time as described above. Now I need a script that purges any file more than 90 days old. I can use DateMath UDF to get the date, split and join it into the correct format and then issue a simple del statement such as: "Del /S C:\YYYYMMDD*". The problem is there is no /S switch in the Kix Del command, only in DOS. I can just send the command to a DOS shell, but that seems clumsy. Isn't there a better way?
Here is what I have so far.
Break On
$resultdate = Datemath(@DATE,-91)
$Date = Join(Split($resultdate,'/'),"")
;Del /S C:\YYYYMMDD* What to do here???
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;Functions
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function DateMath($ExpD1,$ExpD2)
; Author: ScriptLogic Corporation
; if both parameters are dates (yyyy/mm/dd), a positive
; integer indicating the days between the two dates will be returned.
; if the first is a date (yyyy/mm/dd) and the second is an integer
; (number of days), a new date will be returned.
; UDF dependencies: SerialDate, abs
Select
Case InStr($ExpD1,'/') And InStr($ExpD2,'/')
; two date passed - return daysbetween integer
$DateMath=serialdate($ExpD1)-serialdate($ExpD2)
If $DateMath<0
$DateMath=$DateMath*-1
EndIf
Case InStr($ExpD1,'/') And 1-InStr($ExpD2,'/')
; date and a number passed - return date
$ExpD2=0+$ExpD2
$Datemath=serialdate(serialdate($ExpD1)+$ExpD2)
Case 1 ; incorrect parameters passed
EndSelect
EndFunction
Function SerialDate($ExpD)
; Author: ScriptLogic Corporation
; parameter ($ExpD) must be a date (in the form of yyyy/mm/dd)
; or an integer previously derived by this function.
; if passed a date, it returns an integer.
; If passed an integer, it returns the date.
; Integers can be used for general-purpose math computations on dates
; and then converted back to dates by calling this function again.
; Algorithms were obtained from: http://www.capecod.net/~pbaum/date/date0.htm
Dim $z,$h,$a,$b,$c,$y,$m,$d
If InStr($ExpD,'/') ; date passed
$y=Val(SubStr($ExpD,1,4))
$m=Val(SubStr($ExpD,6,2))
$d=Val(SubStr($ExpD,9,2))
If $m<3
$m=$m+12
$y=$y-1
EndIf
; return an integer
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
Else ; integer passed
$z=0+$ExpD+306 ; cast numeric
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
If $m>12
$y=$y+1
$m=$m-12
EndIf
$m=Right('0'+$m,2)
$d=Right('0'+$d,2)
; return a string date
$SerialDate=''+$y+'/'+$m+'/'+$d
EndIf
EndFunction
|
Top
|
|
|
|
#172860 - 2007-01-17 01:21 AM
Re: How to find and delete a specific file
[Re: dgraham]
|
dgraham
Fresh Scripter
Registered: 2006-04-11
Posts: 39
Loc: Santa Fe, New Mexico
|
Actually. Never mind. Now that I think about it, what I have been writing here will delete files that are exactly 90 days old. I need to delete all files that are MORE than 90 days old, so this wont work. Oh well. Back to the beginning for a re-tink.
|
Top
|
|
|
|
#172961 - 2007-01-18 10:23 PM
Re: How to find and delete a specific file
[Re: Sealeopard]
|
dgraham
Fresh Scripter
Registered: 2006-04-11
Posts: 39
Loc: Santa Fe, New Mexico
|
Thanks for the help guys. I ended up using DirPlus to search the directory, pull the FSO info for the Creation Date of the files. Then after some splitting and joining to get the date formats right, I used DateMath and SerialDate from ScriptLogic to determine how many days there are between the current date and the creation date of the file. If that date is greater than 90, then the file is deleted. I am posting the code here in case anyone else needs it. I use the creation date of the files in this circumstance because that is the only consistently correct info I can get based on our unique situation. Most everyone else will probably want to modify this code to delete based on some other attribute. Thanks again guys.
Prerequisites DirPlus UDF & FSO
;Description : Purge all files more than 90 days old.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Break On
Call @SCRIPTDIR + "\DirPlus.udf"
$Files = DirPlus("C:\test",'/A-D /S')
For Each $File in $Files
If $File
$CreateDate=fnGetFileProp($File, DateCreated)
$DateOnly=Split($CreateDate, " ")
$Date=$DateOnly[0]
$Array=Split($Date, "/")
$Year=$Array[2]
$Month=$Array[1]
$Day=$Array[0]
$FormattedDate=$Year + "/" + $Day + "/" + $Month
$FileAge=DateMath($FormattedDate,@DATE)
If $FileAge > "90"
Del $File
EndIf
EndIf
Next
Quit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;Functions
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function fnGetFileProp($sFile,$sProp)
Dim $objFSO, $objFile, $nul
$objFSO = CreateObject("Scripting.FileSystemObject")
If Not VarType($objFSO)=9 Exit 1 EndIf
$objFile = $objFSO.GetFile($sFile)
If Not VarType($objFile)=9 Exit 2 EndIf
$nul=Execute("$$fnGetFileProp = $$objFile."+$sProp)
If VarType($fnGetFileProp)=0 Exit 87 EndIf
EndFunction
Function DateMath($ExpD1,$ExpD2)
Select
Case InStr($ExpD1,'/') And InStr($ExpD2,'/')
$DateMath=serialdate($ExpD1)-serialdate($ExpD2)
If $DateMath<0
$DateMath=$DateMath*-1
EndIf
Case InStr($ExpD1,'/') And 1-InStr($ExpD2,'/')
$ExpD2=0+$ExpD2
$Datemath=serialdate(serialdate($ExpD1)+$ExpD2)
Case 1
EndSelect
EndFunction
Function SerialDate($ExpD)
Dim $z,$h,$a,$b,$c,$y,$m,$d
If InStr($ExpD,'/')
$y=Val(SubStr($ExpD,1,4))
$m=Val(SubStr($ExpD,6,2))
$d=Val(SubStr($ExpD,9,2))
If $m<3
$m=$m+12
$y=$y-1
EndIf
$SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
Else
$z=0+$ExpD+306
$h=100*$z-25
$a=$h/3652425
$b=$a-$a/4
$y=(100*$b+$h)/36525
$c=$b+$z-365*$y-$y/4
$m=(5*$c+456)/153
$d=$c-(153*$m-457)/5
If $m>12
$y=$y+1
$m=$m-12
EndIf
$m=Right('0'+$m,2)
$d=Right('0'+$d,2)
$SerialDate=''+$y+'/'+$m+'/'+$d
EndIf
EndFunction
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|