# Trouble with FileUtils.rm() -- Invalid arguement error

**URL:** <https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492>\
**Category:** Ruby\
**Created:** [February 8, 2008, 3:38am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492 "2008-02-08T03:38:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 3:38am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/1 "2008-02-08T03:38:16Z")

</div>

I have been screwing with this for the last hour, and I still cant get  
it (so frustrating when you get stuck on matters that seem trivial).

I am trying to delete all the contents from the temporary internet files  
in vista, and I keep getting an invalid argeument error.

cache\_dir = ENV[‘USERPROFILE’] +  
“\AppData\Local\Microsoft\Windows\Temporary Internet Files\_._”

FileUtils.rm(cache\_dir)

I have tried several different variations, of backtick, single quotes,  
double quotes, etc, etc…even tried deleting a single file, and it  
always gives me the same error. Also tried rm\_r, rm\_rf…still no luck.

could someone explain to me what I am doing wrong? thanks!

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 3:43am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/2 "2008-02-08T03:43:04Z")

</div>

What’s the error?!?!?1111oneoneeleveneleven

On Feb 7, 2008, at 9:38 PM, an an wrote:

> 

Ari  
--------------------------------------------|  
If you’re not living on the edge,  
then you’re just wasting space.

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 3:57am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/3 "2008-02-08T03:57:10Z")

</div>

an an wrote:

> I have tried several different variations, of backtick, single quotes,  
> double quotes, etc, etc…even tried deleting a single file, and it  
> always gives me the same error. Also tried rm\_r, rm\_rf…still no luck.
> 
> could someone explain to me what I am doing wrong? thanks!

It looks like you’re trying to remove all the files in the directory at  
once. Bear in mind that FileUtils.rm wants a _list_ of filenames, not a  
pattern to match. You can specify a list of filenames by accumulating  
them in an array and then using the splat operator:

filenames = whatever it takes to get all the filename in an array  
FileUtils.rm(\*filenames)

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/4 "2008-02-08T23:46:03Z")

</div>

Tim H. wrote:

> an an wrote:
> 
> > I have tried several different variations, of backtick, single quotes,  
> > double quotes, etc, etc…even tried deleting a single file, and it  
> > always gives me the same error. Also tried rm\_r, rm\_rf…still no luck.
> > 
> > could someone explain to me what I am doing wrong? thanks!
> 
> It looks like you’re trying to remove all the files in the directory at  
> once. Bear in mind that FileUtils.rm wants a _list_ of filenames, not a  
> pattern to match. You can specify a list of filenames by accumulating  
> them in an array and then using the splat operator:
> 
> filenames = whatever it takes to get all the filename in an array  
> FileUtils.rm(\*filenames)

So I am stuck again on this…I have the following code:

dir = Dir.open(“c:\myDir\delete\”)  
FileUtils.rm(dir.entries.reject{|e| /.{1,2}$/ =~ e})

the problem is, that the dir.entries only returns the filename of each  
entry…is there anyway to have them expanded?

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 4:07am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/5 "2008-02-08T04:07:45Z")

</div>

Tim H. wrote:

> an an wrote:
> 
> > I have tried several different variations, of backtick, single quotes,  
> > double quotes, etc, etc…even tried deleting a single file, and it  
> > always gives me the same error. Also tried rm\_r, rm\_rf…still no luck.
> > 
> > could someone explain to me what I am doing wrong? thanks!
> 
> It looks like you’re trying to remove all the files in the directory at  
> once. Bear in mind that FileUtils.rm wants a _list_ of filenames, not a  
> pattern to match. You can specify a list of filenames by accumulating  
> them in an array and then using the splat operator:
> 
> filenames = whatever it takes to get all the filename in an array  
> FileUtils.rm(\*filenames)

that makes much more sense. what threw me off was the \* in the following  
line in the rdoc

FileUtils.rm Dir.glob(‘\*.so’)

Did some reading up on Dir.glob and it makes much more sense now.  
Thanks!

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 12:38am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/6 "2008-02-09T00:38:00Z")

</div>

an an wrote:

> > It looks like you’re trying to remove all the files in the directory at  
> > dir = Dir.open(“c:\myDir\delete\”)  
> > FileUtils.rm(dir.entries.reject{|e| /.{1,2}$/ =~ e})
> 
> the problem is, that the dir.entries only returns the filename of each  
> entry…is there anyway to have them expanded

Use Dir.glob instead:

FileUtils.rm Dir.glob(“c:\myDir\delete\”)

-Justin

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 1:47am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/7 "2008-02-09T01:47:12Z")

</div>

an an wrote:

> > > entry…is there anyway to have them expanded  
> > > Dir.glob("C:")  
> > > puts Dir.glob("C:")
> 
> neither of which print anything

I forgot the asterisk:

FileUtils.rm Dir.glob(“c:\myDir\delete\*”)

-Justin

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 12:59am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/8 "2008-02-09T00:59:56Z")

</div>

Justin C. wrote:

> an an wrote:
> 
> > > It looks like you’re trying to remove all the files in the directory at  
> > > dir = Dir.open(“c:\myDir\delete\”)  
> > > FileUtils.rm(dir.entries.reject{|e| /.{1,2}$/ =~ e})
> > 
> > the problem is, that the dir.entries only returns the filename of each  
> > entry…is there anyway to have them expanded
> 
> Use Dir.glob instead:
> 
> FileUtils.rm Dir.glob(“c:\myDir\delete\”)
> 
> -Justin

still doesn’t work for some reason. in irb if I do

Dir.glob("C:")  
puts Dir.glob("C:")

neither of which print anything

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 3:08am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/9 "2008-02-09T03:08:52Z")

</div>

Justin C. wrote:

> an an wrote:
> 
> > > > entry…is there anyway to have them expanded  
> > > > Dir.glob("C:")  
> > > > puts Dir.glob("C:")
> > 
> > neither of which print anything
> 
> I forgot the asterisk:
> 
> FileUtils.rm Dir.glob(“c:\myDir\delete\*”)
> 
> -Justin

still no luck…here is exactly what I have typed into irb:

irb(main):003:1’ Dir.glob(‘c:')  
irb(main):004:1’ Dir.glob(‘c:\drivers')  
irb(main):005:1’ Dir.glob(“c:\drivers")  
irb(main):006:1’ Dir.glob(“c:\drivers\*”)  
irb(main):007:1’ puts Dir.glob(“c:\drivers\*”)  
irb(main):008:1’ puts Dir.glob(“c:\drivers\_")  
irb(main):009:1’ Dir.glob("c:\drivers\_”)  
irb(main):010:1’ Dir.glob('c:\drivers\_‘)  
irb(main):011:1’ Dir.glob("c:\_”)  
irb(main):012:1’ print Dir.glob(“C:\*”)

none of these commands returned anything…any idea why?

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 3:47am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/10 "2008-02-09T03:47:23Z")

</div>

an an wrote:

> > >
> 
> irb(main):011:1’ Dir.glob(“c:\_")  
> irb(main):012:1’ print Dir.glob("C:\_”)
> 
> none of these commands returned anything…any idea why?

Well…it _might_ be a Windows thing, but what did you type in before  
those lines? You seem to be within a quote.

Notice your prompt:

irb(main):003:1’  
^

```
         Should be a > (greater than sign)

```

For instance:

irb(main):001:0\> ’ ← I just started a string  
irb(main):002:0’ ← Now the prompt is different  
irb(main):003:0’ Until I close the string, it will remain that way  
irb(main):004:0’ here I close it → ’  
=\> " ← I just started a string\n ← Now the prompt is different\nUntil I  
close the string, it will remain that way\nhere I close it → "  
irb(main):005:0\> puts “Now we’re back to normal”  
Now we’re back to normal  
=\> nil

-Justin

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 3:33am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/11 "2008-02-09T03:33:51Z")

</div>

On Feb 9, 2008 10:08 AM, an an [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> still no luck…here is exactly what I have typed into irb:  
> irb(main):011:1’ Dir.glob(“c:\_")  
> irb(main):012:1’ print Dir.glob("C:\_”)  
> none of these commands returned anything…any idea why?

i think nobu advised us on using (forward)slash instead of backslash  
since the latter was a special/metacharacter, so,

irb(main):008:0\> Dir.glob(‘c:/\*’)  
=\> [“c:/#!mail!#2060tDO#”, “c:/2.pst”, “c:/abap”, “c:/abap.pst”,  
“c:/adaptec”, "  
c:/admin", “c:/adsm.pst”, “c:/ae”, “c:/aeditor-2.0.log”, “c:/and”,  
“c:/AntigenIn  
stall.log”, “c:/archive.pst”, “c:/archive2.pst”, “c:/asoutput.log”,  
"c:/Autostar  
", “c:/avgun.log”, “c:/backup”, “c:/backup2.pst”, “c:/bind.pst”,  
“c:/bind2.pst”,  
\<–snipped–\>

btw, before running the above in irb, pls press ctrl-c+enter or  
restart your irb first. your irb prompt is hinting a continuation on a  
quote 😉

kind regards -botp

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 9, 2008, 3:53am UTC](https://www.ruby-forum.com/t/trouble-with-fileutils-rm-invalid-arguement-error/128492/12 "2008-02-09T03:53:05Z")

</div>

Justin & botp,

thanks for your help. I didn’t notice that problem with irb, but using  
the ‘/’ slash ended up working…thanks for all your help!
