# splitting filename

**URL:** <https://forums.powershell.org/t/splitting-filename/6444>\
**Category:** PowerShell Help\
**Created:** [May 13, 2016, 12:22am UTC](https://forums.powershell.org/t/splitting-filename/6444 "2016-05-13T00:22:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 12:22am UTC](https://forums.powershell.org/t/splitting-filename/6444/1 "2016-05-13T00:22:03Z")

</div>

Hello, I’m writing a larger script, but one of the pieces I need is to split the file name into 2 parts:

This File would need split into 2 parts, the first part being the saved to like:

$date = “06-12-07”  
$vendor = “Vendor Name”

```
06-12-07 Vendor Name.pdf
```

And to complicate things, sometimes the file name would be just like

```
Vendor Name.pdf
```

Without the year information, which is ok. I’m not sure exactly how to split it up, I cant use split as the filename can have multiple spaces in it.

Any help would be greatly appreciated.

---

<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:** [May 13, 2016, 12:35am UTC](https://forums.powershell.org/t/splitting-filename/6444/2 "2016-05-13T00:35:01Z")

</div>

I’d use regular expressions for this, personally:

> <https://gist.github.com/dlwyatt/a48e0afde767ca78880dd97ded947828>

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 12:43am UTC](https://forums.powershell.org/t/splitting-filename/6444/3 "2016-05-13T00:43:11Z")

</div>

Thank you Dave, that works perfectly! I wish I knew regex a lot better (than 0). Do you have any helpful links or books you use?

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 12:45am UTC](https://forums.powershell.org/t/splitting-filename/6444/4 "2016-05-13T00:45:10Z")

</div>

Ah…sorry one more thing…I noticed a date using underscores…

12\_14\_12\_ - Vendor name.pdf

How could I edit your regex to include these and maybe any other chars users can find and plug in?

---

<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:** [May 13, 2016, 12:50am UTC](https://forums.powershell.org/t/splitting-filename/6444/5 "2016-05-13T00:50:38Z")

</div>

That’s going to get ugly fast 🙂 I’ve updated the gist’s pattern, give it a try.

I learned Regex years ago from this site: [http://www.regular-expressions.info/](http://www.regular-expressions.info/) . What I like about it is that it’s got the general “applies to everything” information, but it’s also got sections specific to many different implementations. So you can get data about perl, java, .NET, PowerShell, VBScript, whatever. 🙂

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 12:59am UTC](https://forums.powershell.org/t/splitting-filename/6444/6 "2016-05-13T00:59:55Z")

</div>

Thank you again, works great! My code on the other hand leaves a lot to be desired I imagine 😉 But your regex is working flawlessly. Thanks again!

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 1:21am UTC](https://forums.powershell.org/t/splitting-filename/6444/7 "2016-05-13T01:21:37Z")

</div>

Crap…last Time I will bug you about this…

The name can also be like

```
10_10_10 F. last.pdf
```

All files will end in pdf, any way to allow for .'s in the vendor name and just exclude the extension?

---

<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:** [May 13, 2016, 1:24am UTC](https://forums.powershell.org/t/splitting-filename/6444/8 "2016-05-13T01:24:09Z")

</div>

Oops, there’s supposed to be a $ sign at the very end of the pattern.

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [May 13, 2016, 1:24am UTC](https://forums.powershell.org/t/splitting-filename/6444/9 "2016-05-13T01:24:58Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/splitting-filename/6444/10 "2024-05-16T20:41:23Z")

</div>

Closed on May 16, 2024, 8:41pm UTC


