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
  • Do you know beforehand the folders containing these .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:50
  • 1 @SantiagoSquarzon I know the names of each folder containing these .tiff files. Additionally, the folder names are all identical except for the final two characters which are a two digit number that increases with each folder. – Mitcheaux Commented Dec 3, 2024 at 17:05
  • Ok, and just to confirm, basically all files inside each folder get their parent folder name (i.e.: vdl-1984-01 + - and a 3 digit incrementing number) is that correct ? – Santiago Squarzon Commented Dec 3, 2024 at 17:06
  • That is correct @SantiagoSquarzon – Mitcheaux Commented Dec 3, 2024 at 17:12
  • Rename-Item can receive from pipeline, so no need to use | % {} – phuclv Commented Dec 4, 2024 at 2:59
Add a comment  | 

1 Answer 1

Reset to default 0

Perhaps 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