问题
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
回答1:
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
You should be able to run it from a batch-file or cmd like this:
PowerShell -ExecutionPolicy RemoteSigned -File "MyScript.ps1"
If you find that some information gets truncated due to very long file paths, you can increase the Width
value as necessary.
来源:https://stackoverflow.com/questions/56614812/list-all-file-parameters-in-directory-recursively-using-forfiles-in-for-loop