Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

changing a file name, extension not showing up

by mark4444az (Sexton)
on Nov 27, 2012 at 20:59 UTC ( [id://1005916]=perlquestion: print w/replies, xml ) Need Help??

mark4444az has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having a hard time renaming a file and getting the file extension to show up. Here's what I have: <code> system("mv fpga_log.txt $serial.txt"): /<code> It gives me the $serial just fine, but no ".txt" extension. Anyone have any ideas?
  • Comment on changing a file name, extension not showing up

Replies are listed 'Best First'.
Re: changing a file name, extension not showing up
by kennethk (Abbot) on Nov 27, 2012 at 21:32 UTC
    Another possibility is you've got trailing whitespace on your name. What happens when you $serial =~ s/\s//g? You can also see what your variable literally contains by printing w/ delimiters, a la print "x${serial}x"

    And ++ for rename and File::Copy.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I did a length check and there's an extra character, presumably a newline, I didn't do a chomp on that <STDIN>. I added chomp and used "rename" and it works like a charm. Thanks very much!
Re: changing a file name, extension not showing up
by roboticus (Chancellor) on Nov 27, 2012 at 21:23 UTC

    mark4444az:

    If you're using the Windows Explorer, then the file may have a .txt extension but it's being hidden. Check your settings. (I hate that "feature".)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      No, it's not a hidden filename this time, thanks though. I'm going to try and use rename, that does seem the most logical way to do it.
Re: changing a file name, extension not showing up
by TomDLux (Vicar) on Nov 27, 2012 at 21:14 UTC

    Whenever I use a variable within contiguous text, I always delimit it. The '.' should terminate the variable name, because dots are not allowed in variable names. But the delimiter does help the human reader. And of course in situations where underscores are used in variables, you need to delimit to prevent accidents ( we frequently use date/time in file names, at work.)

    Using the Perl rename command instead of shelling out is a superior solution. 99% of the time you think you need to shell out to use some standard Unix component, you need to look at the manual.

    rename 'fpga_log.txt', $serial . '.txt'; # or maybe rename 'fpga_log.txt', "${serial}.txt";

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: changing a file name, extension not showing up
by Anonymous Monk on Nov 27, 2012 at 21:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1005916]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found