Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

check me? deleting files based on a pattern

 [1/7] from: notofo:earthlink at: 27-Oct-2003 18:34


hi guys, is this script "safe?" it was taking forever, but then there are alot of files... I killed the process. is there a better way to do this? REBOL [ Title: "removealot" Date: 27-Oct-2003 File: %removealot.r RCS-Id: "$Id$" ] ;trace on home: to-file rejoin ["~" "/"] base: to-file rejoin [home "attempt-it/"] foreach file read base [ if found? (find file "editpost") [ delete base/:file ] ] -- Elevating Cluelessness to an Art Form -tom

 [2/7] from: antonr:iinet:au at: 28-Oct-2003 23:22


Looks pretty safe to me. You are just in one directory. This will delete all files with "editpost" in the name somewhere. I would catch errors while deleting the file so you are not interrupted by a single file access error because one file is in use. foreach file read base [ if find file "editpost" [ if error? set/any 'err try [delete base/:file][ print mold disarm err ] ] ] You can, by the way, say this: home: %~/ base: join home %attempt-it/ How many files? Anton.

 [3/7] from: notofo:earthlink at: 28-Oct-2003 9:02


Anton Rolls wrote:
>Looks pretty safe to me. >You are just in one directory.
<<quoted lines omitted: 15>>
>How many files? >Anton.
Thanks Anton, 7500 files. the task is finished, I "risked it." I'll use your suggestions. I especially like the error checking. I've been a reboler for years and I'm still *very* clueless. you brushed up against a question I've had for a while. How can I elimintate most of the "rejoins" that I use? I rejoin everything. If all you have is a hammer... thanks again, -tom

 [4/7] from: joel:neely:fedex at: 28-Oct-2003 10:01


Hi, Tom, Saving some typing and evaluation... Tom Foster wrote:
> home: to-file rejoin ["~" "/"] > base: to-file rejoin [home "attempt-it/"]
<<quoted lines omitted: 3>>
> ] > ]
You can just use a literal FILE! value for the base directory foreach file read base: %~/attempt-it/ [...[ and avoid all the REJOIN evaluations. -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Enron Accountingg in a Nutshell: 1c=$0.01=($0.10)**2=(10c)**2=100c=$1

 [5/7] from: greggirwin:mindspring at: 28-Oct-2003 11:05


Hi Tom, TF> How can I elimintate most of the "rejoins" that I use? TF> I rejoin everything. If all you have is a hammer... JOIN and REJOIN will return the same datatype as the first item you give them, so you can avoid doing a lot of TO-* stuff if you don't use strings for everything (and look at the source for them so you can see what they do under the hood). INSERT, CHANGE, and APPEND are often all you need, but one of the most important things is to look at how things read (i.e. is the intent clear) based on the words you're using. COMPOSE is another handy tool, as is standard block notation; sometimes you don't really need things joined if the code processing them can operate on a block. i.e. don't REJOIN things in one place only to split them back apart somewhere else if you can avoid it. Path notation is also handy, but it looks like you know about that. -- Gregg

 [6/7] from: rebol-list2:seznam:cz at: 31-Oct-2003 11:09


Hello Tom, Tuesday, October 28, 2003, 3:02:40 PM, you wrote: TF> Anton Rolls wrote:
>>Looks pretty safe to me. >>You are just in one directory.
<<quoted lines omitted: 23>>
>> >>
TF> Thanks Anton, TF> 7500 files. TF> the task is finished, I "risked it." I'll use your suggestions. I TF> especially like the error checking. TF> I've been a reboler for years and I'm still *very* clueless. TF> you brushed up against a question I've had for a while. How can I TF> elimintate most of the "rejoins" that I use? TF> I rejoin everything. If all you have is a hammer... TF> thanks again, TF> -tom If there is too many files you may try to open the directory as a port without buffering: files: open/direct base while [not tail? files][ file: first files if find file "editpost" [ if error? set/any 'err try [delete base/:file][ print mold disarm err ] ] files: next files ] close files -- Best regards, rebOldes -----------------[ http://oldes.multimedia.cz/ ]

 [7/7] from: antonr:iinet:au at: 3-Nov-2003 13:48


RebOldes' code below is not as fast as possible yet, I think, though less likely to write a bug by missing or adding an extra [ next dir-port ], and it is less code to maintain, still... If you look at source delete, it is opening a directory port to do its work. Better to use remove on a direct port, rather than delete, no? I refer you to http://rebol.com/docs/core23/rebolcore.html Chapter 14 - Ports, section 8.3 Direct Port Access See an example using remove at bottom.....
> If there is too many files you may try to open the directory as a > port without buffering:
<<quoted lines omitted: 12>>
> Best regards, > rebOldes -----------------[ http://oldes.multimedia.cz/ ]
; create some files to play with repeat n 10 [save join %editpos [#"p" + to-integer (n / 2) n // 2] none] ; list the files foreach file read %. [print file] prin "Press enter key..." input "" ; selectively delete some of the files base: %. ; current directory dir-port: open/direct base while [not tail? dir-port][ file: first dir-port ?? file either find file "editpost" [ either error? set/any 'err try [remove dir-port][ print mold disarm err ; remove not successful, therefore must move to the next file dir-port: next dir-port ][ ; successfully removed a file from dir-port print "------- removed" ] ][ ; didn't match selection, move to the next file dir-port: next dir-port ] ] close dir-port Anton.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted