当文件名前两个字符为ZZ时删除文件

9

我想要删除所有以zzZZ开头的文件。请问应该如何操作?


NTFS 不区分大小写,因此 del zz* 即使文件名以 zz、zZ、Zz 或 ZZ 开头也会删除这些文件。 - vonPryz
这不起作用,因为zz文件在不同的子文件夹中。 - Diondk
5个回答

13

获取路径中以“zz”开头的所有对象(包括隐藏的和递归的),过滤掉目录对象并删除这些项目。

Get-ChildItem <path> -Recurse -Force -Filter zz* | Where-Object {!$_.PSIsContainer} | Remove-Item

为避免 Where-Object 检查非容器,请使用 -File 参数来获取 Get-ChildItem。Get-ChildItem <path> -Recurse -File -Force -Filter zz* | Remove-Item - rob

4
这对我有效:
Remove-Item zz*

这应该是被接受的答案,简单快捷,你可以使用任何东西甚至使用通配符 Remove-Item *.js 来删除扩展名为 .js 的文件,太棒了! - crisleo94

0
get-childitem zz* |
  where-object {$_ -cmatch "^(zz|ZZ)"} |
   foreach-item {remove-item $_.fullname}

0

使用-filter会更快。当你确定它是你想要的时候,去掉-whatif。

get-childitem -recurse -file -filter zz* | remove-item -whatIf

-1

NSFileManager *FileManager = [NSFileManager defaultManager]; NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

// 创建用于准备的 txt 文件夹 NSDirectoryEnumerator *DirFile = [FileManager enumeratorAtPath:[Paths objectAtIndex:0]];

IFileTxt = [[NSMutableArray alloc] init]; NSString *file; while ((file = [DirFile nextObject])) {

//In your case you need to test substring = "ZZ"
if ([[file pathExtension] isEqualToString: @"txt"]){ 
    [FileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",[Paths objectAtIndex:0],file] error:nil];
}

}

[IFileTxt release];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接