Hello,
I’m going a bit nuts with a script of mine that doesn’t seem to behave
with file renaming. I’ve got 172 files in a directory, all with the
extension “.pstxt.”
The above works fine and renames all 172 files in the directory at
present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs
and gives me the following one entry remaining in my directory:
ps2k_.pstxt
All the 172 files are gone! This doesn’t make sense to me. The
“filetime” variables are to be used in something completely different
and I don’t see why they’re affecting this simple renaming of files.
Hello,
I’m going a bit nuts with a script of mine that doesn’t seem to behave
with file renaming. I’ve got 172 files in a directory, all with the
extension “.pstxt.”
The above works fine and renames all 172 files in the directory at
present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs
and gives me the following one entry remaining in my directory:
ps2k_.pstxt
All the 172 files are gone! This doesn’t make sense to me. The
“filetime” variables are to be used in something completely different
and I don’t see why they’re affecting this simple renaming of files.
Thanks,
Peter
Just a thought…
If I recall correctly, gsub! returns nil if no substitution was made.
I think that your problem is here. String#gsub with a string
replacement doesn’t provide the use of $1 in the replacement string.
I’m not sure why it’s working when it does. Something before thoses
lines seems to be setting $1 to what you are expecting, Line 4 is
going to reset $1 to nil since it doesn’t capture anything.
Try replacing line 20 with either
File.rename(pstxtfile, pstxtfile.to_s.gsub(/(^.*).pdf.pstxt/,
“ps2k_\1.pstxt”))
Hello,
I’m going a bit nuts with a script of mine that doesn’t seem to behave
with file renaming. I’ve got 172 files in a directory, all with the
extension “.pstxt.”
The above works fine and renames all 172 files in the directory at
present. But, if I uncomment line number 4 and/or 5 and/or 6, it runs
and gives me the following one entry remaining in my directory:
ps2k_.pstxt
All the 172 files are gone! This doesn’t make sense to me. The
“filetime” variables are to be used in something completely different
and I don’t see why they’re affecting this simple renaming of files.
Thanks,
Peter
Just a thought…
If I recall correctly, gsub! returns nil if no substitution was made.
Thanks, David. Well, yeh, it does appear that the filetime stuff was
messing me up. This is fairly old code, for me, meaning 3 or 4 months
old, so, I modernized that filetime stuff a bit and now it seems to
work! I basically just simplified it by changing:
$filetime = File.stat(pstxtfile).mtime
to
$filetime = File.mtime(pstxtfile)
Thanks again!
-Peter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.