Wednesday 15 July 2015

PowerShell script to remove characters from files and folders -


I am trying to extract some characters from files and folders repeatedly using PowerShell script. Below I've found that the script is, but it will only remove underscores from files, not folders. There are some characters that need to be removed, but if I need to have a script for each character, then it is okay. It frequently changes files in folders, but no folders are 'fixed'.

Current PowerShell Script:

  'dir -Recurse | Where {-not $ _ PsIscontainer- and $ _ Name-match "_"} | Foreign currency {$ new = $ _ name Change ("_", "") Rename-Item-Path $ _. Full Name - New Name $ New-PassStra} ''  

As the root of your problem The comments tell you that you do not have not- $ _ Skipping the folder with the PsIscontainer Your where is the component of the block

  DIR-Resource | Where {$ _. Name-match "_"} | ...  

The second issue that is happening to you, it is most likely that since you are changing the folder names, you must first specify the dir The wrong path would be after the get-childItem was invented with / . One way to resolve this is to first process the files, then folders

  $ filesandfolders = Go- ChildItem -recurse | Where the object {$ _ Name-match "_"} $ filesand folders | Where-matter {! $ _ PsIscontainer} | Foreign currency {$ new = $ _ name Change ("_", "") Rename-Item-Path $ _. Full name - New name $ new -Pastro-Voyife} $ filesandfolders | Where-object {$ _. PsIscontainer} | Foreach {$ New = $ _ Name. Replace ("_", "") Rename-Item-Path $ _. Fullname -newname $ new -passthru -WhatIf}  

Not the most beautiful solution means but it will work. It first processes all files, then folders. Remove -Whatif s when you will make sure that what you will do

Other characters

You mentioned other characters Those who were looking for you to remove would not be a long order. You can use regex for this, so try to do this. "$ Files =". $ "$ Regex =" [$ ($ regex): escape ($ letters) "$ filesandfolders = Go- ChildItem- refinance | where objects {$ _ .nam-match $ regex} $ filesand folders | where-goods { ! $ _. PsIscontainer} | Foreach {$ $ new = $ _- Rename $ regex Rename-item-path $ _. Fullname -newname $ new -passthru -WhatIf} $ filesandfolders where-object {$ _ . PsIscontainer} | Foreach {$ new = $ _. Name -Replace $ regex Rename-item-path $ _. Fullname -newname $ new -passthru -WhatIf}

Will remove and underlie those files and folders.

Not tested but you In some of these lines you can also get it

  $ characters = "._" $ Regex = "[$ ([regex] :: escape ($ Letters)) "$ filesandfolders = Get-ChildItem -recurse | Where the object {$ _ .nam-match $ regex} $ filesand folders | where-object {! $ _. PsIscontainer} | Rename - New ($ _ Name-install $ regex) -PassThru- what $ filesandfolders | Where-object {$ _. PsIscontainer} | Rename - New name ($ _- name-install $ regex) -PassThru -WhatIf  

No comments:

Post a Comment