Help with rename function

M

mparker

Hi all,

I have a perl script that ftp's to a server and downloads a file
everyday. The file name is currently called ag051509.txt(051509 is
the date and changes daily). I need to rename that file to agents.txt
using a script. Since the name changes everyday I need to do
something that is equivalent to ag*.txt in unix. Is there something I
can write that is close to -

rename(ag*.txt, agents.txt)

Any help would be greatly appreciated. Thanks in advance!
 
J

Jürgen Exner

Hi all,

I have a perl script that ftp's to a server and downloads a file
everyday. The file name is currently called ag051509.txt(051509 is
the date and changes daily). I need to rename that file to agents.txt
using a script. Since the name changes everyday I need to do
something that is equivalent to ag*.txt in unix. Is there something I
can write that is close to -

rename(ag*.txt, agents.txt)

Assuming there is exactly one matching file name

($name) = glob('ag*.txt');
rename($name, 'agents.txt');

jue
 
T

Tad J McClellan

I have a perl script that ftp's to a server and downloads a file
everyday. The file name is currently called ag051509.txt(051509 is
the date and changes daily).


How do you know what filename to ask for when you do the FTP request?

Save that name in a variable and then use that variable later
to rename the file.

I need to rename that file to agents.txt
using a script.


Why "using a script"?

Why not "using the ftp program itself"?

ftp -o agents.txt (e-mail address removed)/path/to/ag051509.txt

I need to do


You think you need to, but you do not need to.

something that is equivalent to ag*.txt in unix.


The technical name for those ag*.txt thingies in unix is a "glob",
or more accurately, a "filename glob".

Is there something I
can write that is close to -

rename(ag*.txt, agents.txt)


No, because rename() takes only 2 arguments, and ag*.txt may expand
to more than 1 filename.

Also no because strings need quotes around them.

perldoc -f glob

You could use a List Slice to ensure that the glob returns only
one filename:

rename( (glob 'ag*.txt')[0], 'agents.txt');
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top