From: Daniel Berger Date: 2007-09-29T06:24:32+09:00 Subject: Re: is there to invoke 'previous' in Find? (or refresh the current path?) On Sep 28, 1:12 pm, Daniel Berger wrote: > On Sep 27, 4:10 pm, "dtow...@gmail.com" wrote: > > > > > I am writing a small script which recursively goes down a dir > > hierarchy, and when it reaches a set of files, it applies a 3rd party > > application which modifies those files, and prepends the name with > > "Mod-". > > > So for example, if i had a directory with only 1 file: C:\myDir > > \myFile.txt, and I ran the app, I would end up with the following > > files in c:\myDir > > > myFile.txt > > Mod-myFile.txt > > > Basically, what i want to do is traverse down the hierarchy, when i > > reach a set of files, call the app on that directory, then delete the > > original, and rename the new file so that the "Mod-" is removed. > > Unfortunately, the Find function doesnt see the newly created file, > > and thus i cant delete & rename. My code looks something like this: > > > Find.find(startingDir) do |path| > > if File.directory?(path) > > next > > else > > system("myapp #{path}") > > puts path > > if File.basename(path) =~ /^Mod-/ > > if File.exists?(path.sub('Mod-', '')) > > File.delete(path.sub('Mod-', '')) > > end > > File.rename(path, path.sub('Mod-', '')) > > end > > end > > end > > > I was wondering if there was a way to reset the path one level up > > (i.e. where "puts path" is above), that way the current directory > > would be refreshed, and the new files would be recognized. > > I don't think so. You'll probably have to use Dir.glob, keep a > separate array of paths already visited and manually rewind back to > your queue. > > You post does give me some devilish ideas for my file-find library Ok, here's what I came up with. First, I added a Rule#previous method, which shows you the file you iterated over previously (or nil, if it's the first match). With this in place you can do something like this: require 'file/find' rule = File::Find.new( :name => "z*.h", :path => "/my/include/sys" ) rule.find{ |file| if file == 'zone.h' # Rename zone.h rule.rewind(rule.previous) else puts file end } The rewind code isn't the zippiest thing, but it works. How does that look? Regards, Dan