简体   繁体   English

如何使用perl将文件从一个文件夹复制到另一个具有不同扩展名的文件夹

[英]how to copy the files form one folder to another with different extension with perl

#!/usr/bin/perl 
use File::Copy; 
print "content-type: text/html \n\n"; #The header 
$filetobecopied = "C:\Users\avinash\Desktop\mktg/"; 
$newfile = "C:\Users\avinash\Desktop\elp/"; 
copy("$.pdf","$.elp") or die "File cannot be copied.";

the above program i have used to get the out put but getting error can any one can help me to out in the code 上面的程序我曾经使用过,但是出错了,任何人都可以帮助我退出代码

If you use backslashes, use single quotes for strings, or double the backslashes. 如果使用反斜杠,请对字符串使用单引号,或将反斜杠加倍。 In double quotes, many backslashed characters have special meanings: 用双引号将许多反斜杠字符具有特殊含义:

my $newfile = "C:\Users\avinash\Desktop\elp/";
print $newfile;

Output: 输出:

C:SERSVINASHDESKTOPP/

There are some hidden characters, too: 也有一些隐藏的字符:

0000000: 433a 5345 5253 0756 494e 4153 4844 4553  C:SERS.VINASHDES
0000010: 4b54 4f50 1b4c 502f                      KTOP.LP/

There are three big issues with your script. 您的脚本存在三个大问题。

  1. Always include use strict; 始终包括use strict; and use warnings; use warnings; in EVERY perl script. 在每个perl脚本中。

    Using these two Pragmas is the number one thing that you can do to become a better programmer. 使用这两个Pragmas是成为更好的程序员所能做的第一件事。 Additionally, you'll always get more help from experts here on SO if they see you're doing this basic due diligence to track down errors yourself. 此外,如果专家看到您正在做此基本的尽职调查以自行查找错误,您将始终从此处的专家那里获得更多帮助。

    In this case, you'll actually get two warnings in your code: 在这种情况下,您实际上会在代码中收到两个警告:

     Use of uninitialized value $. in concatenation (.) or string at script.pl line 6. Use of uninitialized value $. in concatenation (.) or string at script.pl line 6. 

    So your line copy("$.pdf","$.elp") is interpolating the variable $. 因此,您的行copy("$.pdf","$.elp")内插变量$. which is undefined because you haven't read from a file. 未定义,因为您尚未从文件中读取。

  2. Escape your backslashes in double quoted strings 用双引号引起来的转义符

    Backslashes have special meaning in literal string definitions. 反斜杠在文字字符串定义中具有特殊含义。 If you want a literal backslash in a double quoted string, then you need to escape it. 如果要在双引号字符串中使用文字反斜杠,则需要对其进行转义。

    In this instance the following are being translated: 在这种情况下,正在翻译以下内容:

    • \\U is the uc function \\Uuc函数
    • \\a is an alarm code \\a是警报代码
    • \\D is just a literal D \\D只是文字D
    • etc. 等等

    To fix this you either need to use a single quoted string or escape the backslashes 要解决此问题,您需要使用单引号引起来的字符串或转义反斜杠

     my $filetobecopied = "C:\\\\Users\\\\avinash\\\\Desktop\\\\mktg"; # Backslashes escaped my $filetobecopied = 'C:\\Users\\avinash\\Desktop\\mktg'; # Single quotes safer 

    Also, I can't figure out why you have a trailing forward slash in both of your strings. 另外,我无法弄清楚为什么两个字符串中都有一个斜杠。

  3. Output the error message: $! 输出错误信息: $!

    Always include as much information in your error messages as possible. 始终在错误消息中包含尽可能多的信息。 In this case the File::Copy does the following: 在这种情况下, File::Copy执行以下操作:

    All functions return 1 on success, 0 on failure. 所有函数成功返回1,失败返回0。 $! will be set if an error was encountered. 如果遇到错误将设置。

    Therefore your or die statement should contain the following: 因此,您的or die声明应包含以下内容:

     copy("fromfile","tofile") or die "Can't copy: $!"; 

    For even better debugging information, you could include the parameters that you sent to copy: 为了获得更好的调试信息,您可以包括发送来复制的参数:

     copy("fromfile","tofile") or die "Can't copy fromfile -> tofile: $!"; 

Anyway, these three things will help you debug your script. 无论如何,这三件事将帮助您调试脚本。 It's still not possible to completely interpret your intent based off the information you've provided, but the following is a stub of better formatting code: 仍然无法根据您提供的信息完全解释您的意图,但是以下是更好的格式化代码的清单:

#!/usr/bin/perl 
use strict;
use warnings;

use File::Copy; 

print "content-type: text/html \n\n"; #The header 

# The following is likely wrong, but the best interpretation of your intent for now:
my $filetobecopied = 'C:\Users\avinash\Desktop\mktg.pdf'; 
my $newfile        = 'C:\Users\avinash\Desktop\elp.elp'; 

copy($filetobecopied, $newfile)
    or die "Can't copy $filetobecopied -> $newfile: $!";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Perl脚本将具有特定扩展名的文件从一个文件夹移动到另一个文件夹,然后重命名已移动的文件 - How to Perl script to move files with specific extension from one folder to another and rename the moved files 如何在perl中将文件夹从一个目录复制到另一个目录? - How to copy the folder from one directory to another in perl? 如何将Perl中的文件列表从一个目录复制到另一个目录 - How to copy a list of files in perl from one directory to another 给定用户名和密码,如何在Windows上使用Perl将文件从一台计算机复制到另一台计算机 - Given username and password, how do I copy files from one machine to another using perl on windows 如何从Perl中的文件夹获取具有特定扩展名的文件名 - How to get files names with specific extension from a folder in perl Perl:如何定期将某些文件从一个文件夹移动到另一个文件夹? - Perl: How can I periodically move certain files from one folder to another? 根据相似的文件名将文件从一个文件夹复制到另一个文件夹 - Copy files from one folder to another based on similar file name Perl:将文件从一个位置复制到另一个位置 - Perl: Copy file from one location to another 如何基于Perl中的扩展名删除文件? - How to delete files based on the extension in perl? Perl将文件从一个目录复制到另一个目录 - Perl copying files from one directory to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM