I have a script that is designed to do three things:
The script currently looks like this, and works fine for single-titled folders with only .webp or .jpg files in each:
cd D:\TestingGrounds\Test
get-childItem -recurse | Where {$_.extension -eq ".webp"} | rename-item -newname {$_.name -replace ".webp",".jpg"}
$dirs = Get-ChildItem -force D:\TestingGrounds\Test
foreach ($dir in $dirs) {mkdir D:\TestingGrounds\Test\$dir\ch_1; move D:\TestingGrounds\Test\$dir\* D:\TestingGrounds\Test\$dir\ch_1}
I now have multi-chapter folders that already have subfolders with .webp and .jpg files inside pre-made chapter folders, such as "Ch1", "Ch1.5", "Ch2", etc.
I cannot figure out a way to add an exception or exclusion to these multi-chapter folders where they are not touched by the mkdir
and move
portions, only all .webp files to still be renamed
to .jpg
I'm not very familiar with Powershell, much less exception commands. I've tried -notcontains, Where-Object, and making another designator, like $multi = 'Ch*' to be ignored. So far, nothing has worked. It will continue to make a "Ch1" in the multi-chapter subfolders except for the original "Ch1", and move their respective files into them... basically what the original script did. Attached are photos of what I'm trying to do. 1 Before 2 Desired Outcome
Here are some of my attempts:
$dirs = Get-ChildItem -force 'D:\TestingGrounds\Test';
$Multi = 'Ch*';
get-childItem -recurse | Where {$_.extension -eq ".webp"} | rename-item -newname {$_.name -replace ".webp",".jpg"};
if ($folder -notcontains 'Ch*') {mkdir D:\TestingGrounds\Test\$folder\Ch1; move D:\TestingGrounds\Test\$folder\* D:\TestingGrounds\Test\$folder\Ch1}
{mkdir D:\TestingGrounds\Test\$folder\Ch1; move D:\TestingGrounds\Test\$folder\* D:\TestingGrounds\Test\$folder\Ch1} | Where {D:\Testing Grounds\Test\$dir\ -notcontains 'Ch*'}
gci "D:\TestingGrounds\Test" | Where-Object {$_.FullName -notlike "Ch*"} | mkdir D:\TestingGrounds\Test\$dir\Ch1; move D:\TestingGrounds\Test\$dir\Ch1\* D:\TestingGrounds\Test\$dir\Ch1; move D:\TestingGrounds\Test\$dir\Ch1 D:\TestingGrounds\Test\$dir
$dirs = Get-ChildItem -force D:\TestingGrounds\Test; $Multi = 'Ch*'; get-childItem -recurse | Where {$_.extension -eq ".webp"} | rename-item -newname {$_.name -replace ".webp",".jpg"}; if ($dir -notcontains $Multi) {mkdir D:\Testing Grounds\Test\$dir\Ch1; move D:\Testing Grounds\Test\$dir\* D:\Testing Grounds\Test\$dir\Ch1}
Edited to reflect clarification in the question:
This will get all of the .webp files in a given directory, move them to a Chapter folder (and create it if needed), or just rename them to .jpg if the file is in the right place.
#Get all WEBP files in the source folder
$sourcefolder = "C:\Test"
$files = Get-ChildItem $sourcefolder -Recurse -Filter *.webp
$chapter = "Ch1"
#Loop through each of the files
Foreach($f in $files)
{
$newname = $f.Name -replace ".webp",".jpg"
$directory = $f.DirectoryName
#Case insensitive RegEx to see if the file is already in a chapter folder, rename the file.
if($f.DirectoryName -imatch ".*\\ch\d+")
{
Rename-Item $f.FullName -NewName $newname
}
#Chapter folder NOT exists, and, File is NOT in chapter folder. Otherwise we'll create sub chapter folders
If((!(Test-Path "$directory\$chapter")) -and($f.DirectoryName -inotmatch ".*\\ch\d+"))
{
New-Item -Path "$directory\$chapter" -ItemType Directory
}
#Chapter folder EXISTS and File is NOT in chapter folder. We can now move it where it needs to be
If((Test-Path "$directory\$chapter") -and ($f.DirectoryName -inotmatch ".*\\ch\d+"))
{
Move-Item $f.FullName -Destination "$directory\$chapter\$newname"
}
}