在 Powershell 5.0 中每次函数为 运行 时增加一个数字一次
Increment a number once each time a function is ran in Powershell 5.0
我只是在寻找一种方法,每当它下面的脚本是 运行 时,它就会递增变量 $Num。所以脚本从使用 168 开始,运行,然后增加到 169,再次运行,直到 999。谢谢!
$Path = "H:\ClientFiles\CHS\Processed\"
$Num = 168
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if($n.count -gt 0){
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'
Remove-Item $Path$File
}
else {
Write-Output "No Files to Move for FileGroup$File"
}
这就是 for
循环的本质!
$Path = "H:\ClientFiles\CHS\Processed\"
for($Num = 168; $Num -le 999; $Num++){
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if ($n.count -gt 0) {
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item "${Path}.zip" "${Path}FileGroup0000000${Num}.zip"
Remove-Item $Path$File
}
else {
Write-Output "No Files to Move for FileGroup$File"
}
}
我们的 for()
循环声明的分解:
for($Num = 168; $Num -le 999; $Num++){
# ^ ^ ^
# | | | Increase by one every time
# | | Keep running as long as $num is less than or equal to 999
# | Start with an initial value of 168
}
不确定你想走这条路。您可以拥有一个维护脚本变量的模块:
# file.psm1
$script:num = 1
function myfunc {
$script:num
$script:num++
}
import-module .\file.psm1
myfunc
1
myfunc
2
myfunc
3
remove-module file
我正在回答post的标题。您的模块看起来像这样,但 powershell 提示符必须始终打开。
$num = $script:Num = 168
function myfunc {
$Path = ".\"
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if($n.count -gt 0){
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'
Remove-Item $Path$File
} else {
Write-Output "No Files to Move for FileGroup$File"
}
$script:num++
}
我只是在寻找一种方法,每当它下面的脚本是 运行 时,它就会递增变量 $Num。所以脚本从使用 168 开始,运行,然后增加到 169,再次运行,直到 999。谢谢!
$Path = "H:\ClientFiles\CHS\Processed\"
$Num = 168
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if($n.count -gt 0){
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'
Remove-Item $Path$File
}
else {
Write-Output "No Files to Move for FileGroup$File"
}
这就是 for
循环的本质!
$Path = "H:\ClientFiles\CHS\Processed\"
for($Num = 168; $Num -le 999; $Num++){
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if ($n.count -gt 0) {
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item "${Path}.zip" "${Path}FileGroup0000000${Num}.zip"
Remove-Item $Path$File
}
else {
Write-Output "No Files to Move for FileGroup$File"
}
}
我们的 for()
循环声明的分解:
for($Num = 168; $Num -le 999; $Num++){
# ^ ^ ^
# | | | Increase by one every time
# | | Keep running as long as $num is less than or equal to 999
# | Start with an initial value of 168
}
不确定你想走这条路。您可以拥有一个维护脚本变量的模块:
# file.psm1
$script:num = 1
function myfunc {
$script:num
$script:num++
}
import-module .\file.psm1
myfunc
1
myfunc
2
myfunc
3
remove-module file
我正在回答post的标题。您的模块看起来像这样,但 powershell 提示符必须始终打开。
$num = $script:Num = 168
function myfunc {
$Path = ".\"
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.83*"
$n = dir -Path $Path$File | Measure
if($n.count -gt 0){
Remove-Item $Path$ZipFile
Compress-Archive -Path $Path$File -DestinationPath $Path
Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'
Remove-Item $Path$File
} else {
Write-Output "No Files to Move for FileGroup$File"
}
$script:num++
}