用分割的输出替换文件名

huangapple go评论55阅读模式
英文:

replacing files names with split output

问题

$files = Get-ChildItem -Path C:\Test
write-output $files

Directory: C:\Test

1_N04532L_LEFT.JPG
2_N04532R_RIGHT.JPG

code continues

foreach ($file in $files)
{
   $nameArray = $file -split "_"
   $newName = $nameArray[1]
   write-output $newName
}
N04532L
N04532R

有没有办法实现这个我不是程序员网上有很多信息但对我来说没用
英文:

I am trying to use PowerShell to read filenames from a dir;
then within a for loop:
split names using a delimiter; store desired output in a new variable. Now I want to replace the original filenames in the directory with this new variable. So far I have gathered the following with the expected outputs shown:

$files = Get-ChildItem -Path C:\Test
write-output $files

Directory: C:\Test

1_N04532L_LEFT.JPG
2_N04532R_RIGHT.JPG

code continues

foreach ($file in $files)
{
   $nameArray = $file -split "_"
   $newName = $nameArray[1]
   write-output $newName
}

N04532L
N04532R

Any Ideas on how to accomplish this. I am not a programmer and there is lots of data on this, but it's not working for me.

答案1

得分: 0

如两位评论者已经解释过的那样,可以使用Rename-Item cmdlet来重命名文件。由于此cmdlet可以在其NewName参数中接受一个脚本块,因此您可以使用它来创建一个新的文件名。

# 添加开关 -File 可以确保您不会尝试重命名子文件夹
$files = Get-ChildItem -Path 'C:\Test' -File  
foreach ($file in $files) { 
    $file | Rename-Item -NewName { '{0}{1}' -f ($file.BaseName -split '_')[1], $file.Extension }
}

您可以通过将从Get-ChildItem获取的结果逐个传递给Rename-Item cmdlet来缩短此过程。因为在这里我们正在传递FileInfo对象,所以可以利用$_自动变量

# 用括号括起Get-ChildItem cmd以便在将其传递给Rename-Item cmdlet之前完全枚举文件。
# 如果不这样做,您可能会再次捡起并处理已重命名的文件...
(Get-ChildItem -Path 'C:\Test' -File) | 
Rename-Item -NewName { '{0}{1}' -f ($_.BaseName -split '_')[1], $_.Extension }

注意:在重命名文件时,您可能会遇到命名冲突,此时您将收到一个异常。

英文:

As both commenters already explained, there is the Rename-Item cmdlet for renaming files.
Since this cmdlet can take a scriptblock in its NewName parameter, you can use that to create a new filename.

# adding switch -File makes sure you do not also try to rename subfolders
$files = Get-ChildItem -Path 'C:\Test' -File  
foreach ($file in $files) { 
    $file | Rename-Item -NewName { '{0}{1}' -f ($file.BaseName -split '_')[1], $file.Extension }
}

You can shorten this by piping the results from Get-ChildItem trhough one-by-one to the Rename-Item cmdlet.
Because we're piping the FileInfo objects here, we can make use of the $_ automatic variable

# enclose the Get-ChildItem cmd in brackets so this will enumerate the files to completion
# before passing them on to te Rename-Item cmdlet.
# if you don't, files you already have renamed could be picked up and processed again..
(Get-ChildItem -Path 'C:\Test' -File) | 
Rename-Item -NewName { '{0}{1}' -f ($_.BaseName -split '_')[1], $_.Extension }

Note: when renaming files, you can always run into naming collisions, upon which you will receive an exception

huangapple
  • 本文由 发表于 2023年2月19日 08:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定