首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环访问Powershell中的文件组,如FileGroup159、FileGroup160等

循环访问Powershell中的文件组,如FileGroup159、FileGroup160等
EN

Stack Overflow用户
提问于 2020-03-08 06:36:10
回答 1查看 101关注 0票数 0

所以我让代码按照我喜欢的方式在单个文件中工作。基于下面的一些建议,我想出了以下几点:

代码语言:javascript
复制
$Path = "C:\Users\User\Documents\PowerShell\"

$Num = 160
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.txt"
$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"
        }

我现在唯一需要做的就是让$Num在每次程序结束后递增。因此,程序将运行,然后将$Num移到160、161等位置,这样我就不必手动重新启动代码了。谢谢你到目前为止的帮助。

EN

回答 1

Stack Overflow用户

发布于 2020-03-08 18:44:58

您的文件名格式应该放在循环中,并且应该使用Format operator -f来获取前面的零,如下所示:

代码语言:javascript
复制
159..1250 | ForEach-Object {
    $UnzippedFile = 'FileGroup{0:0000000000}' -f $_
    $ZipFile = "$UnzippedFile.zip"
    Write-Host "Unzipping: $ZipFile"
    # Do your thing here
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60582851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档