-2

I'm having trouble removing characters on files and folders. I need to remove all characters after 640

This is what i have`

WW-459-1-6401HR5N6UJ

WW-478-2-6409Z7E6H98

XAC-3001-640GOGQ8ZHN

This is what i need to get

WW-459-1

WW-478-2

XAC-3001

Thanks

Mike
  • 1,048
  • 2
  • 11
  • 23
  • [Matching it is easy, `-640.*$`.](https://regex101.com/r/uPXuDG/1/) If you wish to replace it then you just need to figure out the powershell code for doing a replacement. – MonkeyZeus Nov 01 '19 at 12:54

1 Answers1

0

This is a pretty specific request. If anything changes the script gets more complex. But string manipulation is pretty simple, and there's a lot of ways to do the same thing. Here's one example:

$str = Get-Item -Path 'C:\Path\To\WW-459-1-6401HR5.txt' | Select-Object -Property Name -ExpandProperty Name
$newStr = $str.Substring(0,$str.IndexOf("640")-1)
$newstr
$ext = $str.Substring($str.IndexOf("."),$str.Length - $str.IndexOf("."))
$ext
$finalStr = $newStr + $ext
$finalStr

Obviously you'll still need to add some logic to grab all the files and loop through them, but this takes care of the string manipulation that you requested.