0

I have a script that is designed to do three things:

  1. Convert all .webp files to .jpg from the main "Test" and all its differently-titled subfolders.
  2. For each directory, make a sub directory called "Ch1" within.
  3. Move all the files into the "Ch1" folder

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}
nooblett
  • 3
  • 2
  • Please show us an example of the structure you now have and also how the desired outcome of the script should be. Also, your script does not convert the files at all.. It merely renames them by replacing `webp` into `.jpg`. You may want to try [ConvertTo-Jpeg](https://github.com/DavidAnson/ConvertTo-Jpeg) for that – Theo Feb 10 '23 at 12:24
  • I'm sorry, looking back I realized over-explained a lot on this. What I mean to say is that the initial script works just fine on folders or $dir in $dirs that do not have any subfolders. I'm trying to exclude the subfolders and their associated parent folder($dir) from the portion of the script that makes directories and moves files into them. – nooblett Feb 10 '23 at 12:40
  • In the first example, If you want to use wildcards in your comparison then try -notlike instead of -ne. In your second example you probably want $_.name instead of $_.fullname. Fullname includes the entire path to the item, so your -notlike will always be true. – emanresu Feb 10 '23 at 16:48

1 Answers1

0

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"
    }
}
Ollie99th
  • 20
  • 6
  • Thanks for the script, however it did not work in the way I was needing. It created "Ch1" folders in all subfolders, and only placed the converted .webp files into them. I'll do some more research and better formulate my question later, but thank you for your help! – nooblett Feb 11 '23 at 07:46
  • No worries, if you can give an example of your folder structure too then I can rework a solution that might fit better – Ollie99th Feb 12 '23 at 09:44
  • Sure thing! I just updated the entire post and added two screencaps showing the initial folder/file structure and the desired outcome. Hopefully this makes a bit more sense. Thanks! – nooblett Feb 12 '23 at 22:07
  • Answer updated in respect to the clarification you added to the OP. I've added case insensitive matches to filter out files that are already in a chapter folder. You can use RegEx with these switches. In this case we're looking to either include or exclude filepaths that have \ch1 (any numbers really) This might cause issues with any paths that have different formats than Ch[x] – Ollie99th Feb 15 '23 at 12:54
  • Thank you! It skips the folders with subfolders now! Though it only seems to place the converted .webp files into the created "Ch1" folder instead of any pre-existing .jpgs too, but I think I might be able to figure out how to change that. It's practically non-existent for an individual folder to contain both .webp AND .jpg files simultaneously anyway. Thank you so much for your help! – nooblett Feb 16 '23 at 23:47