Page 1 of 1 1
Topic Options
#172845 - 2007-01-16 09:10 PM How to find and delete a specific file
sgtdrpepper Offline
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
#172846 - 2007-01-16 09:24 PM Re: How to find and delete a specific file [Re: sgtdrpepper]
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
open cmd prompt
type: del /?
Quote:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\wheelerc>del /?
Deletes one or more files.

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

names Specifies a list of one or more files or directories.
Wildcards may be used to delete multiple files. If a
directory is specified, all files within the directory
will be deleted.

/P Prompts for confirmation before deleting each file.
/F Force deleting of read-only files.
/S Delete specified files from all subdirectories.
/Q Quiet mode, do not ask if ok to delete on global wildcard
/A Selects files to delete based on attributes
attributes R Read-only files S System files
H Hidden files A Files ready for archiving
- Prefix meaning not

If Command Extensions are enabled DEL and ERASE change as follows:

The display semantics of the /S switch are reversed in that it shows
you only the files that are deleted, not the ones it could not find.


you want to run a command something like:
del c:\filename.ext /f /s /q
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#172847 - 2007-01-16 09:29 PM Re: How to find and delete a specific file [Re: Radimus]
sgtdrpepper Offline
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
#172848 - 2007-01-16 09:47 PM Re: How to find and delete a specific file [Re: sgtdrpepper]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
You may wanna look at the dirplus()-udf.
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#172853 - 2007-01-17 12:05 AM Re: How to find and delete a specific file [Re: Björn]
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
read the switches again...
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#172854 - 2007-01-17 12:09 AM Re: How to find and delete a specific file [Re: sgtdrpepper]
dgraham Offline
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.

Code:
 
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 Offline
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
#172861 - 2007-01-17 01:43 AM Re: How to find and delete a specific file [Re: dgraham]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You can use the DirPlus() UDF to list through all the files and pull the date with FSO and even delete with FSO. There are several examples posted.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172862 - 2007-01-17 01:49 AM Re: How to find and delete a specific file [Re: dgraham]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Try this UDF instead...
http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=82473
_________________________
Today is the tomorrow you worried about yesterday.

Top
#172872 - 2007-01-17 04:37 AM Re: How to find and delete a specific file [Re: Gargoyle]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Or try CleanDirectory() - Cleans up the temporary directories (or any arbitrary directory)
_________________________
There are two types of vessels, submarines and targets.

Top
#172961 - 2007-01-18 10:23 PM Re: How to find and delete a specific file [Re: Sealeopard]
dgraham Offline
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

Code:
;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
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.063 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org