79421751

Date: 2025-02-07 17:57:18
Score: 0.5
Natty:
Report link

Here are 3 example filenames. It is the list of the Python Documentation Zip file. I am just learning thanks for the WhatIf suggestion, I will use that from now on. As for the code given to me from SirTao, that script did not uppercase the filenames that had single words such as tutorial.pdf stayed tutorial.pdf

I had an entire conversation with Gemini AI for about 5 hours last night. Gemini gave me strong suggestions. I suggested executing one script and then executing a separate second one, and Gemini agreed. I tried executing the first script that kept the list in order but did not capitalize the single-word filenames, as I executed the second script; PowerShell gave me an error about the file already existing. I then gave Gemini the two variations of the script I originally provided here. Gemini updated its script to handle the error and created a solution for an absolute path.

The error here is something I saw when I was younger but I forgot because I stopped studying computers. In order to manipulate files. You have to construct the full filename from scratch. I learned you cannot let a language like C handle filenames even if it has a library for file I/O. I forget why manipulating filenames causes this behavior but the issue is about the language's privileges and how the directory path is an actual place in memory.

Here is the code Gemini finished with. It is definitely similar to my original script compared to the other recommendations given by Gemini, but more error handling and the solution for the perfect file path. I say this comment because if you use Gemini, you should still provide your solutions to the project.

# Get files, preserve order
$files = Get-ChildItem $dirPath -File | Sort-Object FullName

foreach ($item in $files) {
   $filename = $item.Name # Get the original filename (including extension)
   $extension = $item.Extension # Store the extension separately
   $myString = $filename -replace "-", " "
   $myString = $myString -replace "_", " "
   $myString = $myString -replace "(?i)Whatsnew", "whats new"
   $myString = $myString -replace "(?i)howto", "how to"
   $words = $myString.Split(" ")
   $result = ""
   foreach ($word in $words) {
     if ($word.Length -gt 0) {
       $result += $word.Substring(0, 1).ToUpper() + $word.Substring(1).ToLower() + " "
     }
   }
   $result = $result.Trim() + $extension # Add the extension back
   $new_path = Join-Path -Path $dirPath -ChildPath $result

   # Check if the new file already exists
   if (Test-Path $new_path) {
       # Add a number to the filename to make it unique (only if needed)
       $counter = 1
       do {
           $unique_path = Join-Path -Path $dirPath -ChildPath ("{0} ({1}){2}" -f $result.Replace($extension,""), $counter, $extension) # Add counter and preserve extension
           $counter++
       } until (-not (Test-Path $unique_path))
       $new_path = $unique_path
   }
   Rename-Item $item.FullName $new_path -WhatIf # Use -WhatIf first!
}
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Johnny Van