admin管理员组文章数量:1122846
I need to rename files within folders. The folders follow this naming sequence: vdl-1984-01, vdl-1984-02, vdl-1984-03, etc. I only have access to powershell at work and all the fields are tiffs so I have been using
$i=1
Get-ChildItem *.tiff | %{Rename-Item $_ -NewName ('vdl-1984-01-{0:D3}.tiff' -f $i++)}
on each folder. However, I would like a script that runs this function for every folder. Is this possible? In this example, the two digit number is the folder number and the three digit generated number is the file number.
I have been trying to set of up a Do Loop that includes an increasing interval in the CD function and the -NewName function but I cannot seem to get something that works.
I need to rename files within folders. The folders follow this naming sequence: vdl-1984-01, vdl-1984-02, vdl-1984-03, etc. I only have access to powershell at work and all the fields are tiffs so I have been using
$i=1
Get-ChildItem *.tiff | %{Rename-Item $_ -NewName ('vdl-1984-01-{0:D3}.tiff' -f $i++)}
on each folder. However, I would like a script that runs this function for every folder. Is this possible? In this example, the two digit number is the folder number and the three digit generated number is the file number.
I have been trying to set of up a Do Loop that includes an increasing interval in the CD function and the -NewName function but I cannot seem to get something that works.
Share Improve this question asked Dec 3, 2024 at 16:45 MitcheauxMitcheaux 132 bronze badges 5 |1 Answer
Reset to default 0Perhaps this works for you, essentially instead of searching for all .tiff
files you search for all directories starting with vdl-1984-
and from there, for each directory you target their child files using the parent folder name plus -
and the 3 digit sequence.
$parentPath = 'path\to\vdlFolders'
# get all directories under the parent folder having a name that starts with `vdl-1984-`
Get-ChildItem $parentPath -Directory -Filter vdl-1984-* | ForEach-Object {
$index = @(0); $parentName = $_.Name
# for each directory, get their child files
$_ | Get-ChildItem -File | Rename-Item -NewName {
# and rename using the parent folder name + the 3 digit incrementing number
"$parentName-{0:D3}.tiff" -f $index[0]++
}
}
This code would also work if you wanted to hardcode the path of each vdl-1984-
folder, but here you want to use Get-Item
instead of Get-ChildItem
, for example:
$vdlFolders = @(
'path\to\vdl-1984-01'
'path\to\vdl-1984-02'
'path\to\vdl-1984-03'
)
$vdlFolders | Get-Item | ForEach-Object {
# renaming logic here remains the same
}
本文标签: powershellRenaming files within multiple directoriesStack Overflow
版权声明:本文标题:powershell - Renaming files within multiple directories - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295488a1929581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.tiff
files ? Like, you want to specify each folder in a loop or you want to dynamically discover them ? – Santiago Squarzon Commented Dec 3, 2024 at 16:50vdl-1984-01
+-
and a 3 digit incrementing number) is that correct ? – Santiago Squarzon Commented Dec 3, 2024 at 17:06| % {}
– phuclv Commented Dec 4, 2024 at 2:59