# File rename code assistance

**URL:** <https://forums.powershell.org/t/file-rename-code-assistance/24370>\
**Category:** PowerShell Help\
**Created:** [July 1, 2024, 6:26am UTC](https://forums.powershell.org/t/file-rename-code-assistance/24370 "2024-07-01T06:26:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott\_86](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@scott\_86](https://forums.powershell.org/u/scott_86)\
**Post date:** [July 1, 2024, 6:26am UTC](https://forums.powershell.org/t/file-rename-code-assistance/24370/1 "2024-07-01T06:26:42Z")

</div>

Hi, I have a number of files that follow the same format I would like to bulk rename using PS.

The file format currently is:

```auto
YYYYMMDD - Title - Firstname Lastname.pdf

```

and I am hoping to bulk change them to the file naming convention my team is now using:

```auto
LASTNAME-FIRSTNAME-TITLE-YYYYMMDD.pdf

```

Note: The `Title/TITLE` part of the filename is generally a single acronym with no extra delimiters.

I am attempting to amend a code (full credit to a user from Reddit who helped me with that initial code) to use it for my application above.

After amending, the current code I have is:

```auto
dir | rename-item -newname { $date,$title,$name = $_.basename -split " - ",3; "$name-$title-$date.pdf".toupper() }

```

which returns:

```auto
FIRSTNAME LASTNAME-TITLE-YYYYMMDD.PDF

```

This is the first time I am trying to amend some code for renaming files that I for myself would consider “complex.”

Two points I cannot figure out how to do:

- Convert in my result `FIRSTNAME LASTNAME` to `LASTNAME-FIRSTNAME` (My thoughts is this needs an extra “nested” `-split` with a `space` delimiter.
- Leave the file extension, in this case `.pdf`, as lowercase.

Would anyone have any thoughts on how to amend the code to also achieve the above 2 points?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 1, 2024, 7:12am UTC](https://forums.powershell.org/t/file-rename-code-assistance/24370/2 "2024-07-01T07:12:06Z")

</div>

> [@scott\_86](#):
>
> I am attempting to amend a code (full credit to a user from Reddit who helped me with that initial code) to use it for my application above.

Hmmm … why don’t you ask follow up questions about your requirement on reddit? I’d expect them to help you further.

> [@scott\_86](#):
>
> After amending, the current code I have is:
> 
> ```auto
> dir | rename-item -newname { $date,$title,$name = $_.basename -split " - ",3; "$name-$title
> 
> ```

Please don’t use aliasses in scripts or here in the forum as it makes your code harder to read.

> [@scott\_86](#):
>
> Two points I cannot figure out how to do:

What have you tried? Use some test files and start trying … 🤷🏼‍♂️

> [@scott\_86](#):
>
> Convert in my result `FIRSTNAME LASTNAME` to `LASTNAME-FIRSTNAME` (My thoughts is this needs an extra “nested” `-split` with a `space` delimiter.

You already know how to split on a combination of characters. It’s not that hard to change that to a space, or is it? 🤷🏼‍♂️

> [@scott\_86](#):
>
> Leave the file extension, in this case `.pdf`, as lowercase.

If you don’t want to change it. Don’t change it. 🤷🏼‍♂️ 😉

```auto
Get-ChildItem | 
Rename-Item -newname { $Date, $Title, $Name = $_.BaseName -split " - ",3; $First, $Last = $Name -split ' ' ; "$Last-$First-$title-$Date".ToUpper() + $_.Extension }

```

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 1, 2024, 5:27pm UTC](https://forums.powershell.org/t/file-rename-code-assistance/24370/3 "2024-07-01T17:27:34Z")

</div>

> [@scott\_86](#):
>
> After amending, the current code I have is:
> 
> ```auto
> dir | rename-item -newname { $date,$title,$name = $_.basename -split " - ",3; "$name-$title-$date.pdf".toupper() }
> 
> ```
> 
> which returns:
> 
> `FIRSTNAME LASTNAME-TITLE-YYYYMMDD.PDF`

It’s going to be a bit more code, but i’m a big fan of the “format operator.”

```auto
Get-ChildItem | Foreach-Object {
    $date,$title,$name = $_.basename -split " - ",3
    Rename-Item -NewName $('{0}-{1}-{2}.pdf' -f $name.ToUpper(),$title.ToUpper(),$date.ToUpper())
}

```

The [Format Operator](https://ss64.com/ps/syntax-f-operator.html) lets you define your string with positional placeholders for an array of input. It’s 0 indexed like all arrays in powershell. This _could_ be reduced to less code, but I’m hoping this makes it easier to understand.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 31, 2024, 5:28pm UTC](https://forums.powershell.org/t/file-rename-code-assistance/24370/4 "2024-07-31T17:28:11Z")

</div>

Closed on July 31, 2024, 5:28pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
