Creating a list of dynamic file names

I started programming in perl a few months ago, and this is my first question on stackoverflow.com. I hope I can find a solution.

So, I want to copy some files from the ftp server. File names are in this format:

abc_201149_cde_07_fgh_include_internal 

In this example, the numerical part changes weekly, for example. 201149 says year = 2011 and week = 49. Similarly, 07 says which version it is.

I copied all the file names into a single file called "sdk_link.txt" and I read each file name from this and then copy it to the local PC:

 use Net::FTP; use File::Copy; $Login = "<redacted>"; $Pwd = "<redacted>"; $ftpHost = "<redacted>"; $ftpFolder = "/daj/dhakj/ahdakl/abc_201206_def_05"; $ftp=Net::FTP->new($ftpHost,Timeout=>100); if ($ftp) { print $ftp->message; } $ftp->login($Login,$Pwd); print $ftp->message; $ftp->cwd($ftpFolder); print $ftp->message; open FILE,"sdk_link.txt" or die $!; while($test=<FILE>) { chomp($test); #Copy the file copy("$test","/testing"); } $ftp->quit; 

I want to run this script every week on Windows. How can I change the numerical part to load the files I need?

+4
source share
1 answer

Well, the obvious answer is to save the template in a file and insert the correct numbers. For instance:

 echo abc_%s_cde_%s_fgh_include_internal | perl -MPOSIX -nE 'say sprintf $_, strftime("%Y%U", localtime()), "07";' 

Output:

 abc_201207_cde_07_fgh_include_internal 

So, if you have a template file, you can use %s to insert lines and provide arguments from your own argument list, or dynamically, as you prefer. For instance:.

 my $year = "2011"; my $week = "49"; my $ver = "07"; # Strings or numbers does not really matter open my $fh, '<', "sdk_link.txt" or die $!; while (<$fh>) { my $file = sprintf $_, $year, $week, $ver; copy($file, "/testing") or die "Copy failed for file $file: $!"; } 

I'm not sure if File :: Copy :: copy works as intended for deleted files, but that is another question. I believe Net :: FTP :: get () may be what you want.

+1
source

Source: https://habr.com/ru/post/1396007/


All Articles