查找已删除的文件数

查找已删除的文件数

我想知道通过批处理脚本删除了多少文件。我找到了一个脚本并对其进行了修改,但它不起作用。有没有什么办法可以修复它。

set-location d:\testing -ErrorAction stop               # Change directory
$files = get-childitem -File                                   # Get all 
the files in this directory
$folders = get-childitem -directory                            # Get all 
the folders in this directory

$count = ($files|measure).count                                # 
($files|measure).count gives how 
                                                           # many files 
are in this collection.
$countfo = ($folders|measure).count                            # 
($files|measure).count gives how 
                                                           # many files 
are in this collection.

Start-Sleep -s 5

write-host "There are $($count) files to be deleted."          # Write 
that to the screen.
$files | remove-item                                           # remove 
the files

write-host "There are $($countfo) folders to be deleted."      # Write 
that to the screen.
$folders | remove-item                                         # remove 
the files

$files = get-childitem -File                                   # Get all 
the files in this directory
                                                           # after the 
delete to find out how
                                                           # how many 
remained.

$folders = get-childitem -directory                              # Get all 
the folders in this directory
                                                           # after the 
delete to find out how
                                                           # how many 
remained.


$count2 = ($files|measure).count                               # 
$countfinal = $count-$count2

$countfo2 = ($folders|measure).count
$countfofinal = $countfo-$countfo2

write-host "$($count2) files remained. " -NoNewline            # 
write-host "$($countfinal) files were deleted. "               # 

write-host "$($countfo2) folders remained. " -NoNewline        # 
write-host "$($countfofinal) folders were deleted. "           # 

Start-Sleep -s 5

迅速的

答案1

您可以使用以下脚本删除脚本顶部指定的文件夹中的所有文件。如果文件夹不存在,则使用 -erroraction stop,脚本将中止,而不是删除当前文件夹中的所有文件。

set-location d:\testing -ErrorAction stop               # Change directory
$files = get-childitem -File                            # Get all the files in this directory

$count = ($files|measure).count                         # ($files|measure).count gives how 
                                                        # many files are in this collection.
write-host "There are $($count) files to be deleted."   # Write that to the screen.
$files | remove-item                                    #remove the files

$files = get-childitem -File                            # Get all the files in this directory
                                                        # after the delete to find out how
                                                        # how many remained.
$count2 = ($files|measure).count                        # 
$countfinal = $count-$count2
write-host "$($count2) files remained. " -NoNewline     # 
write-host "$($countfinal) files were delted. "         # 

相关内容