From: Daniel Berger Date: 2007-09-29T04:12:22+09:00 Subject: Re: is there to invoke 'previous' in Find? (or refresh the current path?) 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, and made me reconsider the implementation in general. I think I should use Dir.new (instead of Dir.foreach) and iterate over the file list using the returned handle. That way I can use Dir#rewind, and "fast forward" to a given point in an array of already visited paths. You may want to consider that approach as well. I'll also post what I come up with for file-find. :) Regards, Dan