Link to home
Start Free TrialLog in
Avatar of Flora Edwards
Flora EdwardsFlag for Sweden

asked on

powershell script to rename files in specified folder

in my folder C:\Flora\Downloads  i have pdf files with website link with it.

i need help with powershell so that when i run it, it removes are URLs and replaces the addtional dots / with spaces.

C:\Flora\Downloads

PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com
PRODUCTAB.EAST.BRE.www.producteast.com


so the end result should look like this
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
PRODUCTAB EAST BRE
Avatar of Bill Prew
Bill Prew

These are file names, right?

And is the URL always "www.producteast.com", or does it vary from file to file?


»bp
Or are you trying to change content within a PDF file?


»bp
Avatar of Flora Edwards

ASKER

the URL varies file to file, also the file names are not always EAST, it can be WEST, SOUTH etc.
what i am looking for is that to remove anything that starts with www and ends with .com and also all the dots to be replaced with spaces.
$files = Get-ChildItem;
foreach($file in $files){
    $newName = "$($file.Name.replace(".www","|").split('|')[0])".trim();
    $extension = [IO.Path]::GetExtension($file.Name);
    $newFileName = "$($newName)$($extension)";
    Rename-Item $file.FullName $newName
}

Open in new window

clever removing the last portion after the www, then just renaming the first portion
Using a regular expression; can be started repeatedly without throwing errors if there are files without the URL.
It's in test mode and will only display what it would do; remove the -WhatIf to run it for real.
$RegEx = '(?<Name>.+?)\.www\..+\.com\Z'
Get-ChildItem -Path 'C:\Flora\Downloads' -Filter *.pdf | Where-Object {$_.BaseName -match $RegEx} | ForEach-Object {
	Rename-Item $_.FullName -NewName "$($Matches['Name'].Replace('.', ' '))$($_.Extension)" -WhatIf
}

Open in new window

thanks a alot everyone
@oBdA

is this regex going to work in powershell?
Yes; it's a PowerShell script, after all.
@Shaun
how do i run your code?

i went to powershell then  logged in the directory with cd

then pasted your code and it did not change anything
oBdA

when i run your code, i get the following result at the end of code and inside folder nothing happens.

You cannot call a method on a null-valued expression.
At line:4 char:38
+ ... em $_.FullName -NewName "$($Matches['Name'].Replace('.', ' '))$($_.Ex ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oBdA

you are truly a genius!

 removed  -WhatIf and it worked like a charm.
is there any poweshell command book i can buy and learn it?
Check out these two earlier questions on Experts Exchange, lots of useful info and links.



»bp
many thanks Bill
Very welcome Flora.


»bp