【发布时间】:2017-09-20 03:39:46
【问题描述】:
首先,很抱歉这篇文章太长了——我正在尝试详细说明!
我希望自动解决我发现的问题。一旦“工作”目录中有超过 100,000 个文件,我有一个工人会定期轰炸。预防性地,我可以停止该过程并将工作目录重命名为“HOLD”并创建新的工作目录以使其继续运行。然后我一次一点地将文件从 HOLD 文件夹移回工作目录,直到它赶上。
我想做的是通过带有 2 个 PowerShell 脚本的任务计划程序自动化整个过程。
----脚本 1----
条件如下:
- 如果工作目录中的文件数大于 60,000
我发现( [System.IO.Directory]::EnumerateFiles($Working)比Get-ChildItem快。
动作:
-
Stop-Service用于 Service1、Service2、Service3 -
Rename-Item -Path "C:\Prod\Working\" -NewName "Hold"或“Hold1”、“2”、“3”等。如果文件夹已经存在——我并不特别关注数字,只要它是一致的,所以如果让系统命名它更容易HOLD、HOLD(1)、HOLD(2) 等。或者在 HOLD 之后附加日期就可以了。 New-Item C:\Prod\Working -type directory-
Start-Service服务1,服务2,服务3
---脚本 2----
条件:
- 如果工作目录中的文件数少于 50,000
动作:
- 从 HOLD* 文件夹中移动 5,000 个文件 -- 从 HOLD 文件夹中移动 5k 个文件直到为空,然后跳过空文件夹并开始从 HOLD1 中移动文件。这个过程应该是动态的并重复到下一个文件夹。
在它出现之前,我很清楚将文件从工作文件夹移动到保留文件夹会更容易,但文件的大小可能非常大,移动它们似乎总是需要更长的时间.
我非常感谢任何意见,我渴望看到一些可靠的答案!
编辑
这是我为脚本 2 运行的内容 - 由 Bacon 提供
#Setup
$restoreThreshold = 30000; # Ensure there's enough room so that restoring $restoreBatchSize
$restoreBatchSize = 500; # files won't push $Working's file count above $restoreThreshold
$Working = "E:\UnprocessedTEST\"
$HoldBaseDirectory = "E:\"
while (@(Get-ChildItem -File -Path $Working).Length -lt $restoreThreshold - $restoreBatchSize)
{
$holdDirectory = Get-ChildItem -Path $HoldBaseDirectory -Directory -Filter '*Hold*' |
Select-Object -Last 1;
if ($holdDirectory -eq $null)
{
# There are no Hold directories to process; don't keep looping
break;
}
# Restore the first $restoreBatchSize files from $holdDirectory and store the count of files restored
$restoredCount = Get-ChildItem $holdDirectory -File `
| Select-Object -First $restoreBatchSize | Move-Item -Destination $Working -PassThru |
Measure-Object | Select-Object -ExpandProperty 'Count';
# If less than $restoreBatchSize files were restored then $holdDirectory is now empty; delete it
if ($restoredCount -lt $restoreBatchSize)
{
Remove-Item -Path $holdDirectory;
}
}
【问题讨论】:
-
看起来您已经具备了完成这项工作的基础。你有什么问题?你有什么问题?
-
将您的代码粘贴到问题中。
标签: powershell if-statement move