在for循环中使用forfiles递归列出目录中的所有文件参数 [英] List all file parameters in directory recursively using forfiles in for loop

查看:427
本文介绍了在for循环中使用forfiles递归列出目录中的所有文件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归列出文件夹中的所有文件及其参数,以列出目录/子目录中大小大于10000字节且修改时间不超过30天的所有文件.问题在于下面的代码只是卡住了,没有任何输出.

I am trying to list down all the Files and its parameters within folder recursively to list all the files in the directories/sub directories that has size greater than 10000 bytes and not modified more than 30 days. The problem is that the below code just gets stuck giving no output.

   @echo on


SET Path1=Z:\Server_P8BE890914E369FBB6FBD0C91748F8B19
 break > Z:\FilestobeDeleted.txt
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\FilestobeDeleted.txt
echo FileName                       Size        Path           Date            Time >>Z:\FilestobeDeleted.txt
for /f "skip=1 tokens=1,2,3,4,5* delims=,] " %%i in ('forfiles /p %Path1% /s /m *.* /c "cmd /c if @fsize gtr 10000 echo @fsize @file @path @fdate @ftime" /d -30') do ( 
set FILE_SIZE_WINDOWS=%%i
set FILE_NAME=%%j
set FILE_PATH=%%k
set FILE_DATE=%%l
set FILE_TIME=%%m
set "unit=B"
for %%b in (KB MB GB TB PB EB ZB YB) do if 1024 lss !FILE_SIZE_WINDOWS! (
set "unit=%%b"
if  !FILE_SIZE_WINDOWS! lss 2147483647 (set /a "FILE_SIZE_WINDOWS=FILE_SIZE_WINDOWS/1024") else (set "FILE_SIZE_WINDOWS=!FILE_SIZE_WINDOWS:~0,-3!")
)

 echo !FILE_NAME! !FILE_SIZE_WINDOWS!!unit! !FILE_PATH! !FILE_DATE! !FILE_TIME! >>Z:\FilestobeDeleted.txt

 )
echo Done

推荐答案

下面是一个示例解决方案,该解决方案尝试在Z:\FilestobeDeleted.txt中列出源目录及其子目录中大于的所有文件的文件名,大小,完全限定的路径以及修改的日期和时间.或等于1MB,并且最近一次在30天前进行了修改.它使用switch语句为这些大小分配适当的单位,然后将其四舍五入到小数点后两位.

Here's an example powershell solution which tries to list, in Z:\FilestobeDeleted.txt, the file name, size, fully qualified path, and modified date and time of all files in your source directory and its subdirectories which are greater than or equal to 1MB and which were last modified over 30 days ago. It uses a switch statement to assign the appropriate unit to those sizes and rounds each to a maximum of two decimal places.

Function Convert-BytesToHighest {
    [CmdletBinding()]
    Param([Parameter(Mandatory=$False,Position=0)][Int64]$Size)

    Switch ($Size) {
        {$Size -GT 1PB} {$NewSize="$([Math]::Round(($Size/1PB),2))PB";Break}
        {$Size -GT 1TB} {$NewSize="$([Math]::Round(($Size/1TB),2))TB";Break}
        {$Size -GT 1GB} {$NewSize="$([Math]::Round(($Size/1GB),2))GB";Break}
        {$Size -GT 1MB} {$NewSize="$([Math]::Round(($Size/1MB),2))MB";Break}
        {$Size -GT 1KB} {$NewSize="$([Math]::Round(($Size/1KB),2))KB";Break}
        Default {$NewSize = "$([Math]::Round($Size,2))Bytes";Break}
    }

    Return $NewSize
}

Get-ChildItem -Path "Z:\Server_P8BE890914E369FBB6FBD0C91748F8B19" -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object {
    (! $_.PSIsContainer) -And ($_.Length -GE 1048576) -And ($_.LastwriteTime -LT (Get-Date).AddDays(-30))
} |
Format-Table Name,@{Expression={Convert-BytesToHighest $_.Length};Label="Size"},FullName,LastWriteTime -AutoSize |
Out-File -FilePath "Z:\FilestobeDeleted.txt" -Width 512

您应该能够从这个:

PowerShell -ExecutionPolicy RemoteSigned -File "MyScript.ps1"

如果发现某些信息由于文件路径过长而被截断,则可以根据需要增加Width值.

If you find that some information gets truncated due to very long file paths, you can increase the Width value as necessary.

这篇关于在for循环中使用forfiles递归列出目录中的所有文件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆