2

I'm trying to mass rename a bunch of 1000 *.json files in a folder but I'm having trouble actually executing the script.

Each file is incrementally named:
"Image 1.json", "Image 2.json, ..... "Image 1000.json"

I want to rename them as integers (-1) without extensions (I know it sounds weird, but gotta do it).
So

  1. "Image 1.json" becomes: 0
  2. "Image 2.json" becomes: 1
  3. "Image 423.json" becomes: 422
  4. "Image 1000.json" becomes: 998

Just so I don't mess this up I create a test folder with only the first 10 files:

enter image description here

And I saved it: C:\test\testfolder

Here's my script

$files = Get-Content
$x = 0
foreach ($file in $files) {
   Rename-Item $file $x
   $x++
}

I save the script in: C:\test\rename.ps1

In PowerShell I navigate to the directory:

cd c:\test\testfolder\

Then in PowerShell I run the script

C:\test\rename.ps1

Power shell then gives me this ("Its asking me for input and I am entering numbers...it goes nowhere):

What am I doing wrong?

enter image description here

bagofmilk
  • 1,492
  • 8
  • 34
  • 69
  • 2
    [1] try using the parameter names instead of just throwing values at the cmdlet and hoping they stick to the right parameter. [*grin*] ///// [2] have you tried just renaming the files with the non-digits removed? – Lee_Dailey Jul 02 '21 at 21:59

2 Answers2

3

This should do the trick:

$folder = 'X:\path\to\jsonfolder'
$i = 0
Get-ChildItem -Path $folder -Filter *.json |
Sort-Object { [int][regex]::Match($_.BaseName,'\d+').Value } |
Rename-Item -NewName { [string]([ref]$i).Value++ }

Sort-Object is there because by the naming of your files, without it, you would jump from Image 1.json to Image 10.json and so on:

-a----          7/2/2021   7:32 PM              2 Image 1.json    
-a----          7/2/2021   7:32 PM              2 Image 10.json   
-a----          7/2/2021   7:32 PM              2 Image 100.json  
-a----          7/2/2021   7:32 PM              2 Image 1000.json 
-a----          7/2/2021   7:32 PM              2 Image 101.json  
-a----          7/2/2021   7:32 PM              2 Image 102.json  

The ([ref]$i).Value++ trick comes from this awesome answer, credits to mklement0.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
3

Get-Content reads content from a file or stream, but you want to read the filenames in a directory. Get-ChildItem is a way to do that; specify your path or use the current directory as shown below.

Don't forget to filter by extension, *.json.

I'm not sure if you really want sequential numbers or you want the number extracted from the filename. If the latter, you can try:

$files = Get-ChildItem *.json

foreach ($file in $files) {
    $newfile = $file -replace '(?<=\\)Image (\d+)\.json$', '$1'
    Rename-Item $file $newfile -WhatIf
}

Remove -WhatIf if you want to actually do the job.

Sample run:

    Directory: C:\Users\foo\Desktop\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          7/2/2021     15:12              6 Image 1.json
-a----          7/2/2021     15:12              6 Image 2.json
-a----          7/2/2021     15:12              6 Image 3333.json
-a----          7/2/2021     15:46            250 rename.ps1


PS C:\Users\foo\Desktop\test> .\rename.ps1
What if: Performing the operation "Rename File" on target "Item: C:\Users\foo\Desktop\test\Image 1.json Destination: C:\Users\foo\Desktop\test\1".
What if: Performing the operation "Rename File" on target "Item: C:\Users\foo\Desktop\test\Image 2.json Destination: C:\Users\foo\Desktop\test\2".
What if: Performing the operation "Rename File" on target "Item: C:\Users\foo\Desktop\test\Image 3333.json Destination: C:\Users\foo\Desktop\test\3333".
ggorlen
  • 44,755
  • 7
  • 76
  • 106