带文件检查和重命名的单行管道循环(Single line piped loop with File Check and Rename)

编程入门 行业动态 更新时间:2024-10-24 12:33:32
带文件检查和重命名的单行管道循环(Single line piped loop with File Check and Rename)

所以我试图重命名一堆MSSQL备份,如下所示:

DBName_backup_2017_12_20_564451321_567987465.bak

类似的东西

DBName.bak

但也有一个安全检查,以确保如果DBName.bak已经采取它会做DBName_1.bak 。 其中1将是一个增量变量,直到有一个有效的未使用的文件名。

我可以用下面的代码来完成它:

Get-ChildItem *_*.bak | % { # Set the new name, replace everything after the first underscore '_' with # '.bak' $newName = &{$_.Name -replace $_.Name.Substring($_.Name.IndexOf("_")), '.bak'} # Check if new name exists for ($cnt = 1; (Test-Path $newName) -eq $true; $cnt++) { # If it already exists add '_' and a number check again until unused # filename is found $newName = &{$newName -replace '.bak', "_$cnt.bak"} } # Rename file to new filename. Uncomment WhatIf for testing. Rename-Item -Path $_ -NewName $newName #-WhatIf }

我现在想要做的,就是把它与管道系在一条线上,但我没有运气。 特别是让循环与Test-Path检查一起运行。 有谁知道我怎么能这样?

So I'm trying to rename a bunch of MSSQL backups that come out like:

DBName_backup_2017_12_20_564451321_567987465.bak

To Something like

DBName.bak

But also have a safety check to ensure that if DBName.bak is already taken it'll do DBName_1.bak. Where 1 will be an incremental variable until there is a valid unused filename.

I was able to do it with the following code:

Get-ChildItem *_*.bak | % { # Set the new name, replace everything after the first underscore '_' with # '.bak' $newName = &{$_.Name -replace $_.Name.Substring($_.Name.IndexOf("_")), '.bak'} # Check if new name exists for ($cnt = 1; (Test-Path $newName) -eq $true; $cnt++) { # If it already exists add '_' and a number check again until unused # filename is found $newName = &{$newName -replace '.bak', "_$cnt.bak"} } # Rename file to new filename. Uncomment WhatIf for testing. Rename-Item -Path $_ -NewName $newName #-WhatIf }

What I'm trying to do now, is to one line it with piping, but am having no luck. Particularly getting the loop to run with the Test-Path check. Does anyone know how I could one like this?

最满意答案

简单:

$cnt = 0; Get-ChildItem *_*.bak | Rename-Item -NewName { ($_.BaseName -replace '_.*', '_') + $script:cnt++ + $_.Extension } -WhatIf

如果您只想(重新)为具有重复数据库名称的文件编号,则不能真正实现单行。 你需要这样的东西:

Get-ChildItem *_*.bak | ForEach-Object { $basename = $_.BaseName -replace '_.*' $newname = $basename + $_.Extension $script:cnt = 1 while (Test-Path $newname) { $newname = $basename + '_' + $script:cnt++ + $_.Extension } Rename-Item -NewName $newname -WhatIf }

但是,在PowerShell中,您可以使用换行符和分号分隔语句,因此您仍然可以将上述所有内容合并到一行中。 喜欢这个:

Get-ChildItem *_*.bak | ForEach-Object {$basename = $_.BaseName -replace '_.*'; $newname = $basename + $_.Extension; $script:cnt = 1; while (Test-Path $newname) {$newname = $basename + '_' + $script:cnt++ + $_.Extension}; Rename-Item -NewName $newname -WhatIf}

不过,我通常不建议这样做,因为它会使代码难以阅读和调试。

无论采用哪种方式,在验证重命名后都可以按照需要进行操作,然后卸下鸡开关。

Simple:

$cnt = 0; Get-ChildItem *_*.bak | Rename-Item -NewName { ($_.BaseName -replace '_.*', '_') + $script:cnt++ + $_.Extension } -WhatIf

If you just want to (re)number files with duplicate database names you can't truly one-line the thing. You need something like this:

Get-ChildItem *_*.bak | ForEach-Object { $basename = $_.BaseName -replace '_.*' $newname = $basename + $_.Extension $script:cnt = 1 while (Test-Path $newname) { $newname = $basename + '_' + $script:cnt++ + $_.Extension } Rename-Item -NewName $newname -WhatIf }

However, in PowerShell you can separate statements with both newlines and semicolons, so you can still merge all of the above into one line. Like this:

Get-ChildItem *_*.bak | ForEach-Object {$basename = $_.BaseName -replace '_.*'; $newname = $basename + $_.Extension; $script:cnt = 1; while (Test-Path $newname) {$newname = $basename + '_' + $script:cnt++ + $_.Extension}; Rename-Item -NewName $newname -WhatIf}

I don't normally recommend doing this, though, because it makes code unnecessarily hard to read and debug.

Either way, remove the the chicken switch after you verified renaming would work as desired.

更多推荐

bak,$newName,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-30 22:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   管道   文件   Single   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!