1

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
UserKD
  • 31
  • 1
  • 5
  • 1
    I'm assuming that you've`SetLocal EnableDelayedExpansion` earlier in the script? – Compo Jun 16 '19 at 00:11
  • Yes you are right. I removed it but still not luck. Updated the entire code above. – UserKD Jun 16 '19 at 00:42
  • So i waited for few more mins, the output in the file is FILE RAN 2019/06/15 FileName Size Path Date Time !FILE_NAME! !FILE_SIZE_WINDOWS!!unit! !FILE_PATH! !FILE_DATE! !FILE_TIME! with same lines multiple times – UserKD Jun 16 '19 at 00:52
  • So, I'll ask the same question again! – Compo Jun 16 '19 at 01:38
  • Thank you !! Added it back and it worked. It just takes too long to work as i have many files more than 1 MB. – UserKD Jun 16 '19 at 02:18
  • 1
    FYI, your question states `10000 bytes`, your comment above states, `more than 1 MB` yet `1`MB is `1048576`bytes (possibly `1000000`). Fixing that should reduce the number of files for you but, `Set /A` works only with 32-bit integers, meaning that it will be unable to calculate sizes for possibly the vast majority of your file sizes. My advice is therefore to look for an alternative solution for your task, _possibly a powershell based one_. – Compo Jun 17 '19 at 19:33
  • `if !FILE_SIZE_WINDOWS! lss 2147483647`: this condition will always be true (except a file is exactly `2147483647` bytes big), because `if` coerces numeric values to fit into the range of signed 32-bit integers. Perhaps you want to take a look at this pure [tag:batch-file] solution for integer division of non-negative numbers even greater than 2^31: [Get size of a directory in 'MB' using batch file](https://stackoverflow.com/a/36318398)... – aschipfl Jun 19 '19 at 10:21

1 Answers1

0

Here's an example 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 or 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.

Compo
  • 36,585
  • 5
  • 27
  • 39