我在一個特定檔案夾中有 900k 個檔案,我需要根據 100 mbs 的總大小每天將它們移動到另一個檔案夾。我嘗試了以下代碼,但它移動了 100 個檔案,而不是 100 mbs 的總量
@echo on
set Source=C:\Users\%Username%\Desktop\Dropbox\Pre
set Target=C:\Users\%Username%\Desktop\Dropbox\Pos
set MaxLimit=10
for /f "tokens=1* delims=[]" %%G in ('dir /A-D /B "%Source%\*.*" ^| find /v /n ""') do (
move /y "%Source%\%%~nxH" "%Target%"
if %%G==%MaxLimit% exit /b 0
)
pause
我需要批處理來移動檔案大小總和小于或等于 10 kbs 的選擇。如果我手動選擇構成 100 mbs 的 N 個檔案,基本上也是如此。我相信它不起作用,因為它只檢查單個檔案的大小。
uj5u.com熱心網友回復:
基本問題是您必須跟蹤復制了多少資料并檢查它是否超過 MaxLimit。這在 PowerShell 中并不困難。此腳本將最多將 $MaxLimit 位元組復制到 $TargetDir。此腳本需要 PowerShell Core 6 。https://github.com/PowerShell/PowerShell 上的當前穩定版本是 7.1.5。這可以通過更改 Join-Path 陳述句來使用舊的 Windows PowerShell 5.1。
將這兩 (2) 個檔案放在同一目錄中。當您確信正確的檔案將被復制到正確的目錄時,請-WhatIf從 Move-Item 命令中洗掉。
=== Move-PictureBatch.bat
@pwsh -NoLogo -NoProfile -File "%~dp0Move-PictureBatch.ps1"
=== Move-PictureBatch.ps1
#Requires -Version 6
$MaxLimit = 100MB
$SourceDir = Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop' -AdditionalChildPath 'Pre'
$TargetDir = Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop' -AdditionalChildPath 'Pos'
$CurrentSize = 0
Get-ChildItem -File -Path $SourceDir |
ForEach-Object {
if (($CurrentSize $_.Length) -lt $MaxLimit) {
Move-Item -Path $_.FullName -Destination $TargetDir -WhatIf
$CurrentSize = $_.Length
} else {
break
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337930.html
