# Help re deleting files please?

**URL:** <https://www.ruby-forum.com/t/help-re-deleting-files-please/77714>\
**Category:** Ruby\
**Created:** [November 29, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/help-re-deleting-files-please/77714 "2006-11-29T22:07:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 29, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/help-re-deleting-files-please/77714/1 "2006-11-29T22:07:05Z")

</div>

Hi,

I have a small task which I was considering using ruby for - if anyone  
has a  
minute and could quickly type in the ruby code (assuming its a few  
lines)  
that would be fantastic. The spec is:

Input

- Small ruby program needs to be triggered by a windows batch file (  
i.e. called out from within a windows batch file)
- The batch file will be provided (from the program that kicks it  
off)  
with a variable containing a filename

Processing

- Take the filename as input
- To search for files under a given directory (so in including  
sub-directories) and delete any files which match the conditions:
  - (size \< 7kb) AND (size \> 5kb)
  - file is closed (i.e. don’t want to pick up a file currently  
being written to by the program which may be just starting to  
grow)
  - Optionally (not sure yet) - May want to limit the files to  
those which match a wildcard version of the filename which was  
provided by the batch file (e.g. if filename was "  
thisfile-v22.mpg", may want to filter on only files matching "  
thisfile\*" if that makes sense.

Output

- No real output required, however if it was simple a log file which  
was appended to with Date/Time, FileDeleted may be nice

Thanks in advance  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 29, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/help-re-deleting-files-please/77714/2 "2006-11-29T22:17:06Z")

</div>

On Nov 29, 2006, at 3:05 PM, Greg H. wrote:

> I have a small task which I was considering using ruby for - if  
> anyone has a  
> minute and could quickly type in the ruby code (assuming its a few  
> lines)  
> that would be fantastic.

I thought _you_ were considering using Ruby for this task. 😉

Seriously, why don’t you give it a shot and give us a shout at any  
point you get stuck on. Send us the broken code and we will gladly  
help you over the hump. You will learn a lot more that way and may  
even surprise yourself.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 30, 2006, 11:55am UTC](https://www.ruby-forum.com/t/help-re-deleting-files-please/77714/3 "2006-11-30T11:55:27Z")

</div>

This smells like a homework , by the way , the best approach to Ruby is  
practice by coding yourself then post the code for others to check or  
optimize  
Hope this piece of code helps you somehow  
Dir.foreach("./") {|x| File.delete(x) if (File.size(x)  
\<7\*1024)&&(File.size(x)

> 5\*1024) }

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 30, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/help-re-deleting-files-please/77714/4 "2006-11-30T23:03:38Z")

</div>

wow - one line - thanks heaps - gives me some pointers re where to dive  
into  
the API doco/classes - take your points  
thanks
