Flora Edwards
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
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
Or are you trying to change content within a PDF file?
»bp
»bp
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.
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
}
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.
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
}
ASKER
thanks a alot everyone
ASKER
@oBdA
is this regex going to work in powershell?
is this regex going to work in powershell?
Yes; it's a PowerShell script, after all.
ASKER
@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
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
ASKER
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'].Replac e('.', ' '))$($_.Ex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
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'].Replac
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
- What is the best available resource for an absolute beginner wanting to master Powershell?
- How can I learn PowerShell scripting by my self
»bp
ASKER
many thanks Bill
Very welcome Flora.
»bp
»bp
And is the URL always "www.producteast.com", or does it vary from file to file?
»bp