Perl 从子文件夹中删除文件(如果它们存在于关闭的文件夹中)
首先,我不是一个perl程序员,但我知道这很危险 我买了一个拍卖脚本,有问题,脚本的卖家拒绝处理 有一个auctiondata文件夹,用于保存所有类别文件夹,包括物品出售时的关闭文件夹。当一个项目被发布时,它会进入其中一个类别文件夹,当它出售时,它应该被移动到关闭的文件夹中,但是一个副本会被移动到关闭的文件夹中,原始文件保留在它的类别文件夹中 这会导致软件错误,直到从类别文件夹中删除有问题的文件 因此,我想知道是否有人可以帮助我使用一个脚本来搜索所有类别文件夹,并且任何文件名与关闭文件夹中的文件匹配的文件都将被删除 我自己尝试过,但都失败了 buyit.pl页面上的代码如下所示Perl 从子文件夹中删除文件(如果它们存在于关闭的文件夹中),perl,perl-module,Perl,Perl Module,首先,我不是一个perl程序员,但我知道这很危险 我买了一个拍卖脚本,有问题,脚本的卖家拒绝处理 有一个auctiondata文件夹,用于保存所有类别文件夹,包括物品出售时的关闭文件夹。当一个项目被发布时,它会进入其中一个类别文件夹,当它出售时,它应该被移动到关闭的文件夹中,但是一个副本会被移动到关闭的文件夹中,原始文件保留在它的类别文件夹中 这会导致软件错误,直到从类别文件夹中删除有问题的文件 因此,我想知道是否有人可以帮助我使用一个脚本来搜索所有类别文件夹,并且任何文件名与关闭文件夹中的文件
##############################################
# Sub: Close Auction now
# This sets an item's status to closed.
sub closebuyit {
if ($form{'CATEGORY'},$form{'ITEM'} ne $config{'closedir'}) {
if ($config{'closedir'}) {
umask(000); # UNIX file permission junk
mkdir("$config{'basepath'}$config{'closedir'}", 0777) unless (-d "$config{'basepath'}$config{'closedir'}");
print "Please notify the site admin that this item cannot be copied to the closed directory even though it is closed.\n"
unless &movefile("$config{'basepath'}$form{'CATEGORY'}/$form{'ITEM'}.dat", "$config{'basepath'}$config{'closedir'}/$form{'CATEGORY'}$form{'ITEM'}.dat");
unlink("$config{'basepath'}askseller/$form{'ITEM'}.dat");
unlink("$config{'basepath'}$config{'countdir'}/$form{'ITEM'}.dat");
}
else {
print "Please notify the site admin that this item cannot be removed even though it is closed.\n" unless unlink("$config{'basepath'}$form{'CATEGORY'}/$form{'ITEM'}.dat");
}
}
}
################################################
1;
本应将文件从类别文件夹移动到关闭文件夹,但不会。文件夹的权限是755,但在其中创建的文件是644
提前感谢您的帮助 您应该将错误处理添加到两个取消链接的行中:
unlink("$config{'basepath'}askseller/$form{'ITEM'}.dat")
or die "Error $! deleting $config{'basepath'}askseller/$form{'ITEM'}.dat";
unlink("$config{'basepath'}$config{'countdir'}/$form{'ITEM'}.dat")
or die "Error $! deleting $config{'basepath'}$config{'countdir'}/$form{'ITEM'}.dat";
这将使您的脚本崩溃,但您将得到可能导致最终解决方案的确切错误消息
以下是清理脚本的开始:
#!/usr/bin/perl -l
use strict;
use warnings;
# Read the files from the closed directory
# Quick exit the script if no files could be found
opendir my $closed_dh,'/path/to/closed' or die "$! opening closed dir";
my %closed_files =
# Use each file ($_) as key with a fixed value of 1
(map { $_ => 1 } readdir($closed_dh)) or exit;
closedir $closed_dh;
# Loop through a list of directories
for my $dir ('/path/to/other_dir1','..dir2','..dirN') {
opendir my $search_dh,$dir or die "$! opening $dir";
# Loop through all files in one directory
for my $file (readdir($search_dh)) {
# Remove file if it exists in the list of closed files
unlink $dir.'/'.$file or die "Error $! deleting $dir/$file"
if $closed_files{$file};
}
}
只是从未经测试的源代码开始。所有Perl命令都可以在上找到,更多信息都可以在上找到,谢谢Sebastian,我创建了一个测试文件-move1.pl,这是其中的代码
#!/usr/bin/perl -l
use strict;
use warnings;
# Read the files from the closed directory
# Quick exit the script if no files could be found
opendir my $closed_dh,'/home/users/web/b1262/moo.mybidszcom/cgi-bin/1/closed' or die "$! opening closed dir";
my %closed_files =
# Use each file ($_) as key with a fixed value of 1
(map { $_ => 1 } readdir($closed_dh)) or exit;
closedir $closed_dh;
# Loop through a list of directories
for my $dir ('/home/users/web/b1262/moo.mybidszcom/cgi-bin/1','..dir2','..dirN') {
opendir my $search_dh,$dir or die "$! opening $dir";
# Loop through all files in one directory
for my $file (readdir($search_dh)) {
# Remove file if it exists in the list of closed files
unlink $dir.'/'.$file or die "Error $! deleting $dir/$file"
if $closed_files{$file};
}
}
不幸的是,它没有工作,我从服务器日志中看到了这个错误
0160429T101358:mybidsz.com/cgi-bin/move1.pl
删除/home/users/web/b1262/moo.mybidszcom/cgi-bin/1/时出错。在/home/users/web/b1262/moo.mybidszcom/cgi-bin/move1.pl第19行
在if文件夹1内是Antiques18文件夹,在该文件夹内是名为1463148060.dat的文件
文件夹1内是一个关闭的文件夹,该文件夹内还有一个名为1463148060.dat的文件以及其他文件名
我的希望是找到一个解决方案,然后在buyit.pl代码的末尾合并它…您是否收到这些打印(“请通知…”)了?有输出吗?有错误吗?我遇到问题的categories文件夹在filezilla中有这些权限。。。