powershell - 将写入进度条添加到脚本 Powershell
问题描述
我实际上正在编写一个按日期对图片和视频进行排序的 powershell 脚本。该脚本工作正常,但我想添加一个进度条,我不知道从哪里开始,这对我来说可能有点棘手,这就是我寻求帮助的原因。
这是对图片进行排序的功能
foreach ($file in $Images)
{
$Directory = $destinationDirectory + "Pictures\" + $file.LastWriteTime.Date.ToString('yyyy') + "\" + $file.LastWriteTime.Date.ToString('MMM')
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
Copy-Item $file.fullname $Directory
}
我阅读了有关写入进度功能的文档,但我真的不知道我应该如何在这个脚本中管理它
解决方案
使用一个变量来保存复制文件的数量,为每个操作增加它,然后显示百分比。Write-Progress的例子有一个很好的例子。
我建议也使用 PowerShell 管道,而不是foreach
.
像这样的东西(-WhatIf
准备好后删除 s):
$images |
ForEach-Object
-Begin {
$filesCopied = 0
} `
-Process {
$Directory = "$destinationDirectory\Pictures\$($_.LastWriteTime.Date.ToString('yyyy'))\$($_.LastWriteTime.Date.ToString('MMM'))"
if (!(Test-Path $Directory)) {
New-Item $directory -type directory -WhatIf
}
Copy-Item $_.fullname $Directory -WhatIf
$filesCopied += 1
Write-Progress -Activity "Copied $_.FullName" `
-CurrentOperation "Copying $_" `
-Status "Progress:" `
-PercentComplete (($filesCopied / $Images.Count) * 100)
}
推荐阅读
- python - 在 Django 中测试从数据库中获取选择的表单 - 不允许数据库访问
- javascript - 使用 Jest、Babel、Webpack 和 React 进行测试。当前未启用对实验语法“classProperties”的支持
- wordpress - 使用反向 NGINX 代理在 Docker 中部署 WordPress 站点
- python - 带有 inotify 的 gunicorn 没有注意到重新加载时更改的文件
- android - Kotlin协程java.lang.IllegalStateException:任务尚未完成,即使任务返回值
- ios - iOS:UIWindow.rootViewController 只能在主线程中使用
- python - 如何使用 for 循环从 Pandas DataFrame 列追加?
- reactjs - 是否可以从 react-stripe-elements 获取值以将其显示在 react-credit-cards 组件中
- android - LottieView 在 android 上关闭应用并在 IOS 上运行
- node.js - 使用 IBM 云功能写入存储在 IBM 云对象存储中的 CSV 文件