775 snippets from 1602 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(138)
JavaScript
(125)
Java
(66)
VBSCRIPT
(58)
String
(44)
CSS
(31)
CSharp
(28)
File
(28)
HTML
(27)
mysql
(25)
C
(24)
VB.NET
(24)
python
(24)
CPlusPlus
(23)
groovy
(23)
New Snippets
Shell keepalive t...
Clean PHP Templat...
PHP/MySQL impleme...
Analyte - Easy to...
Easy SQLite inter...
Very lightweight ...
AutoComplete plug...
AutoComplete plug...
Connection Java -...
View PostgreSql
Venture Capital Jobs
New Members
KennethCC
me
jamesmcm
Can
Kelmi
ysg
dannymo2
chorny
wallie
Hackdemian
Top Members
dannyboy
sundaramkumar
mattrmiller
Pio
i_kenneth
ASmith
ctiggerf
sehrgut
bertheymans
SCoon
Home
/
Snippets
/
Directory Utility
/
Comments
Directory Utility
Snippet Menu
Revisions
Comments
Related Snippets
Add to Favorites
Email Snippet
Download Snippet
Print Snippet
Blog Snippet
snippet
|
revisions
|
comments
|
related
|
Add to Favorites
|
email
download
|
print
|
blog it
New Comment
Bug in subExt
Wed. Jul. 26th, 2006 6:56 PM
MyrddinE
If a filename looks like this: Foo.Bar.txt
It will get improperly modified by subExt(), to Foo.new
The gsub line should be /\.[^.]+/ rather than /\..+/. The extension itself should not contain any periods.
Also, there is nothing wrong with adding a newext onto a file with no extension... for example, subExt("My File", "txt") should return "My File.txt".
# change ext def Dir.subExt( filename , newext ) newext = ('.'+newext) if newext[0] != '.' if filename.rindex('.') filename.sub!( /\.[^.]+/,newext ) else filename << newext end end
Reply
Code blocks are good.
Wed. Jul. 26th, 2006 7:00 PM
MyrddinE
Didn't realize I needed to click the little code button. Lemme try again:
# change ext
def
Dir.
subExt
(
filename , newext
)
newext =
(
'.'+newext
)
if
newext
[
0
]
!= '.'
if
filename.
rindex
(
'.'
)
filename.
sub
!
(
/\.
[
^.
]
+/,newext
)
else
filename << newext
end
end
Reply
New Comment
It will get improperly modified by subExt(), to Foo.new
The gsub line should be /\.[^.]+/ rather than /\..+/. The extension itself should not contain any periods.
Also, there is nothing wrong with adding a newext onto a file with no extension... for example, subExt("My File", "txt") should return "My File.txt".
# change ext
def Dir.subExt( filename , newext )
newext = ('.'+newext) if newext[0] != '.'
if filename.rindex('.')
filename.sub!( /\.[^.]+/,newext )
else
filename << newext
end
end