help directory name issue...

A

Ahmet Kilic

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!
 
R

Robert Klemme

2009/8/25 Ahmet Kilic said:
=A0I am searching inside of the files ,like this

=A0unless /.*test/.match( file_name ) || /.*Test/.match( file_name )
=A0 =A0 =A0 =A0text =3D File.read(file_name)
=A0 =A0 =A0 =A0text.scan(/find/) do |it|
=A0 =A0 =A0 =A0$files << ("#{it} ---> #{File.basename(file_name) } =3D=3D= =3D=3D=3D>
#{File.dirname(file_name)}")
=A0end

I can list the keyword, file name and directory name with path name,
but i want
1- file name without any extension. =A0for example : *.rb ,*.java, *.txt = ,
...
2- I want to output only last directory name. Not path name. for
example: =A0C:\\test\myfiles\, C:\\test\outputs\ =A0.... so only I want t= o
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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
M

Michal Suchanek

2009/8/25 Ahmet Kilic said:
=C2=A0I am searching inside of the files ,like this

=C2=A0unless /.*test/.match( file_name ) || /.*Test/.match( file_name )
=C2=A0 =C2=A0 =C2=A0 =C2=A0text =3D File.read(file_name)
=C2=A0 =C2=A0 =C2=A0 =C2=A0text.scan(/find/) do |it|
=C2=A0 =C2=A0 =C2=A0 =C2=A0$files << ("#{it} ---> #{File.basename(file_na= me) } =3D=3D=3D=3D=3D>
#{File.dirname(file_name)}")
=C2=A0end

I can list the keyword, file name and directory name with path name,
but i want
1- file name without any extension. =C2=A0for example : *.rb ,*.java, *.t= xt ,
...
2- I want to output only last directory name. Not path name. for
example: =C2=A0C:\\test\myfiles\, C:\\test\outputs\ =C2=A0.... 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 =3D~ keyword_rege=
xp }}

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

HTH

Michal
 
7

7stud --

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
 
A

Ahmet Kilic

7stud said:
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.
 
S

spiralofhope

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top