Margaret Mungai
Margaret Mungai

Reputation: 11

Removing all non-alphanumeric characters from folder

I am trying to remove all non-alphanumeric characters from a bunch of Tif files using Windows Command.

I used cmd to access the folder and typed powershell to enter it.

I tried the following line but it did not work: get-childitem *.tif | foreach {rename-item $_ $_.name.replace("^A-Za-z0-9","")}

Upvotes: 1

Views: 554

Answers (1)

SBdev
SBdev

Reputation: 101

String replace does not execute regex patterns, you need to directly use Regex. Also using .Name instead of .BaseName will cause your . to be replaced from the tif extension.

get-childitem *.tif | ForEach { rename-item $_ ([regex]::Replace($_.BaseName, "[^a-zA-Z0-9]", "") + $_.Extension)}

Upvotes: 1

Related Questions