I have a directory with a bunch of subfolders and each subfolder contain a cbz file.
How can I do this on Windows Server 2022? chatgpt gave me a broken powershell script. It just instant closes when I run it
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe" # Change this if 7z.exe is not in your PATH
Get-ChildItem -Recurse -Filter *.cbz | ForEach-Object {
$cbzFile = $_.FullName
$baseFolder = $_.DirectoryName
$tempFolder = Join-Path $baseFolder ([System.IO.Path]::GetFileNameWithoutExtension($_.Name))
# Extract to a subfolder
& $sevenZipPath x $cbzFile "-o$tempFolder" -y
# Move everything up one level
Get-ChildItem -Path $tempFolder -Recurse | ForEach-Object {
$targetPath = Join-Path $baseFolder $_.Name
if (-not (Test-Path $targetPath)) {
Move-Item $_.FullName -Destination $targetPath
} else {
Write-Host "Skipping '$($_.Name)' — already exists in $baseFolder"
}
}
# Clean up
Remove-Item $cbzFile -Force
Remove-Item $tempFolder -Recurse -Force
}
It's most likely the - in line 19. it's showing up when I paste to a file as — and causing an issue.
Change line 19 to:
Write-Host "Skipping '$($_.Name)' already exists in $baseFolder"
Once I did that with your script, it ran fine.