Help directory name issue

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!

2009/8/25 Ahmet K. [email protected]:

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

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

Kind regards

robert

2009/8/25 Ahmet K. [email protected]:

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

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

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.

On Tue, 25 Aug 2009 19:59:36 +0900
7stud – [email protected] 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?