使用用于Windows的perl脚本使用当前时间戳重命名特定文件

时间:2017-09-12 05:34:37

标签: perl perl-module

我使用以下脚本重命名,但它使用STA重命名所有文件结尾。我需要重命名以KRAT或TRAT开头的文件。

#!/usr/local/bin/perl 
use strict; 
use warnings; 
use File::Copy; 

my $directory = 'C:\Users\Desktop'; 

chdir($directory) or die "Can't chdir to $directory $!"; 
opendir(DIR, $directory) || die "Couldn't opendir: $!\n"; 

my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR; 

foreach(@files) { 
    print $_,"\n"; 
    my $newName = $_; 

    $newName =~ s/STA$/t00/g; 
    print "RENAMING: $_ -> $newName \n"; 

    rename($_, $newName); 

}

2 个答案:

答案 0 :(得分:-1)

更改行

$newName =~ s/STA$/t00/g;

$newName =~ s/^KRAT|^TRAT/t00/;

或更多succint。

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

my $directory = '<DIR>';

chdir($directory) or die "Can't chdir to $directory $!";
opendir(DIR, $directory) || die "Couldn't opendir: $!\n";

rename_file($_) for grep { $_ ne '.' && $_ ne '..' && /^KRAT|^TRAT/} readdir DIR

closedir(DIR);

sub rename_file {
    my $name = shift;
    $name =~ s/^KRAT|^TRAT/t00/;
    rename($_, $name);
}

答案 1 :(得分:-1)

对您的脚本进行一些细微更改。

use strict; 
use warnings; 
use File::Copy; 

my $directory = 'C:\Users\rajkunal_aps\Desktop';

chdir($directory) or die "Can't chdir to $directory $!"; 
opendir(DIR, $directory) || die "Couldn't opendir: $!\n"; 

my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR; 

foreach(@files) { 
    print $_,"\n"; 
    my $newName = $_;
    if ($newName =~ /^KRAT/ || $newName =~ /^TRAT/ ) {
    $newName =~ s/STA/t00/g;
    print "RENAMED: $newName from $_ \n";

    rename($_, $newName); 
    }
}

<强> RESULT

TRAT.AUD.STA -> TRAT.AUD.t00
STAT.AUD.STA -> No changes
KRAT.EUD.STA -> KRAT.EUD.t00

说明:

您的脚本存在的问题是,您要搜索以STA结尾的任何文件,然后将其重命名为t00。相反,您应该专门搜索您想要匹配的文件。

为此,我们会在if语句中使用KRATTRAT来查看行的开头是否与/^KRAT//^TRAT/匹配。如果我们找到匹配项,我们会使用STA 搜索t00并将s/STA/t00//g;替换为Crashed: com.apple.main-thread 0 App 0x1004a7624 specialized InspectionItemVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (InspectionItemVC.swift:610) 1 App 0x10049f82c @objc InspectionItemVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (InspectionItemVC.swift) 2 UIKit 0x198931d90 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 688 3 UIKit 0x198931fa8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 80 4 UIKit 0x19891f6ac -[UITableView _updateVisibleCellsNow:isRecursive:] + 2152 5 UIKit 0x198936f98 -[UITableView _performWithCachedTraitCollection:] + 120 6 UIKit 0x1986cf49c -[UITableView layoutSubviews] + 176 7 UIKit 0x1985e9cc0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1200 8 QuartzCore 0x1957da274 -[CALayer layoutSublayers] + 148 9 QuartzCore 0x1957cede8 CA::Layer::layout_if_needed(CA::Transaction*) + 292 10 UIKit 0x1985fe458 -[UIView(Hierarchy) layoutBelowIfNeeded] + 548 11 UIKit 0x1986a50c8 -[UINavigationController _layoutViewController:] + 1244 12 UIKit 0x1986a2944 -[UINavigationController _layoutTopViewController] + 228 13 UIKit 0x1986bb6f4 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 744 14 UIKit 0x1986bb3d0 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 420 15 UIKit 0x1986bafa0 -[UINavigationTransitionView _cleanupTransition] + 608 16 UIKit 0x198625060 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 312 17 UIKit 0x19862320c +[UIViewAnimationState popAnimationState] + 320 18 UIKit 0x1986ae7dc -[UINavigationTransitionView transition:fromView:toView:] + 1860 19 UIKit 0x1986a4764 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2488 20 UIKit 0x1986a3870 -[UINavigationController _startDeferredTransitionIfNeeded:] + 856 21 UIKit 0x1986a3424 -[UINavigationController __viewWillLayoutSubviews] + 64 22 UIKit 0x1986a3388 -[UILayoutContainerView layoutSubviews] + 188 23 UIKit 0x1985e9cc0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1200 24 QuartzCore 0x1957da274 -[CALayer layoutSublayers] + 148 25 QuartzCore 0x1957cede8 CA::Layer::layout_if_needed(CA::Transaction*) + 292 26 QuartzCore 0x1957ceca8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32 27 QuartzCore 0x19574a34c CA::Context::commit_transaction(CA::Transaction*) + 252 28 QuartzCore 0x1957713ac CA::Transaction::commit() + 504 29 UIKit 0x1985df308 _afterCACommitHandler + 256 30 CoreFoundation 0x1924689a8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32 31 CoreFoundation 0x192466630 __CFRunLoopDoObservers + 372 32 CoreFoundation 0x192466a7c __CFRunLoopRun + 956 33 CoreFoundation 0x192396da4 CFRunLoopRunSpecific + 424 34 GraphicsServices 0x193e01074 GSEventRunModal + 100 35 UIKit 0x198651c9c UIApplicationMain + 208 36 App 0x1000cfd04 main (AppDelegate.swift:22) 37 libdyld.dylib 0x1913a559c start + 4