# Help directory name issue

**URL:** <https://www.ruby-forum.com/t/help-directory-name-issue/174339>\
**Category:** Ruby\
**Created:** [August 25, 2009, 10:45am UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339 "2009-08-25T10:45:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 25, 2009, 10:45am UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/1 "2009-08-25T10:45:09Z")

</div>

I am searching inside of the files ,like this

unless /.\*test/.match( file\_name ) || /.\*Test/.match( file\_name )  
text = File.read(file\_name)  
text.scan(/find/) do |it|  
$files \<\< ("#{it} —\> #{File.basename(file\_name) } =====\>  
#{File.dirname(file\_name)}")  
end

I can list the keyword, file name and directory name with path name,  
but i want  
1- file name without any extension. for example : _.rb ,_.java, \*.txt ,  
…  
2- I want to output only last directory name. Not path name. for  
example: C:\test\myfiles, C:\test\outputs\ … so only I want to  
output myfiles and outputs directory names  
3- i want to output the search keyword`s left and right side sentences  
also.

please help me. please!

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 25, 2009, 11:04am UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/2 "2009-08-25T11:04:04Z")

</div>

2009/8/25 Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> but i want  
> 1- file name without any extension. for example : _.rb ,_.java, \*.txt ,  
> …  
> 2- I want to output only last directory name. Not path name. for  
> example: C:\test\myfiles, C:\test\outputs\ … so only I want to  
> output myfiles and outputs directory names  
> 3- i want to output the search keyword`s left and right side sentences  
> also.
> 
> please help me. please!

Pathname to the rescue:

[http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html)

Also, please look into $` (prematch) and $’ (postmatch) and / or  
MatchData and Regexp#match.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 25, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/3 "2009-08-25T12:23:42Z")

</div>

2009/8/25 Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> but i want  
> 1- file name without any extension. Â for example : _.rb ,_.java, \*.txt ,  
> …  
> 2- I want to output only last directory name. Not path name. for  
> example: Â C:\test\myfiles, C:\test\outputs\ Â … so only I want to  
> output myfiles and outputs directory names

I suggest you look at File::split, File::basename or String.split.

> 3- i want to output the search keyword`s left and right side sentences  
> also.

I am not sure what that means. You can print the line containing a  
keyword easily with

File.open(filename){|f| f.each\_line{|l| STDOUT \<\< l if l =~  
keyword\_regexp }}

and if you want sentences you could possibly split on punctuation  
instead of newlines.

HTH

Michal

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 25, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/4 "2009-08-25T12:59:27Z")

</div>

In addition, you can always use the universal hammer: split().

fname = “C:/path/to/dir/pic10.jpg”  
pieces = fname.split("/")  
puts pieces[-2] #dir

parts = pieces[-1].split(".")  
puts parts[0] #pic10

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 25, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/5 "2009-08-25T16:01:51Z")

</div>

7stud – wrote:

> In addition, you can always use the universal hammer: split().
> 
> fname = “C:/path/to/dir/pic10.jpg”  
> pieces = fname.split("/")  
> puts pieces[-2] #dir
> 
> parts = pieces[-1].split(".")  
> puts parts[0] #pic10

thank you very very much. That was what I want.

> and if you want sentences you could possibly split on punctuation  
> instead of newlines.

Yeah, I want that. I want to output some sentences, they are sql  
sentences insert, update. delete and table column names.

Thanks I will try it.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 26, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/help-directory-name-issue/174339/6 "2009-08-26T17:50:55Z")

</div>

On Tue, 25 Aug 2009 19:59:36 +0900  
7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> fname = “C:/path/to/dir/pic10.jpg”  
> pieces = fname.split(“/”)  
> puts pieces[-2] #dir

I find that I often use

fname.split(File::Separator)

to make my code more OS-agnostic…

While I’ve heard that “/” will work in Ruby code run on Windows… I  
don’t have a system to test.

Should I continue using File::Separator?
