@jonesbones<\/a> <\/p>\nGood point, was just seeing if there was a quicker way, and it’s not that this request has come up but I’m just trying to cover all bases in case someone does make the request. Thank you.<\/p>","upvoteCount":0,"datePublished":"2020-06-28T18:42:05.000Z","url":"https://community.spiceworks.com/t/remove-commas-from-folder-names/767517/15","author":{"@type":"Person","name":"spiceuser-8vxr4","url":"https://community.spiceworks.com/u/spiceuser-8vxr4"}},{"@type":"Answer","text":"
Just a quickie<\/p>\n
The error message says there is an error passing a script block = the code did this:<\/p>\n
ForEach-Object {Rename-Item $_ -NewName {$_.Replace(\",\",\"\")}} \n<\/code><\/pre>\nThe clue is that the parameter value is enclosed in {} which makes it a script block, Instead what about this:<\/p>\n
ForEach-Object {Rename-Item $_ -NewName ($_.Replace(\",\",\"\")) } \n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2020-06-29T13:04:31.000Z","url":"https://community.spiceworks.com/t/remove-commas-from-folder-names/767517/16","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}}]}}
I have a CSV file listing about 100 entries of folders that need to be created. The folder names include commas which I would like to remove. I was able to create the script to create the folders and tried within the same script to replace the commas with an empty value but is not working.
I also tried to remove the commas outside of the script in case it can’t be combined in the original script but that too did not work. Here is my current syntax:
$Folders = import-csv "Path to CSV file"
ForEach ($Folder in $Folders) {
New-Item $Folder.Company -type directory
}
That first part of the script works fine.
I then tried to use this to remove the commas but did not work:
Get-ChildItem -Filter "*,*" |
ForEach-Object {Rename-Item $_ -NewName {$_.Replace(",","")}}
TIA
7 Spice ups
Neally
(Neally)
June 27, 2020, 4:22pm
2
can you post a sample of what the CSV looks like and name you want to fix/remove the comma from??
in theory something like this?
$csv = import-csv ".\file.csv"
foreach($line in $CSV){
new-item $($line.company -replace ",") -ItemType Directory
}
Try
$Folders = import-csv “Path to CSV file”
ForEach ($Folder in $Folders) {
New-Item $Folder.Company.replace(“,”,””) -type directory
}
@alexw
That worked great, thanks so much. That worked perfect when the folder is first getting created. The only issue is if the folder already exists in a path, that’s where I feel I need to use something like this:
Get-ChildItem PathToNetworkLocationOfFolders -Filter "*,*" -Recurse |
ForEach-Object {Rename-Item $_ -NewName {$_.Replace(",","")}}
But it fails with the following message:
Rename-Item : Cannot evaluate parameter ‘NewName’ because its argument is specified as a script block and there is no input. A script
block cannot be evaluated without input.
At line:2 char:41
ForEach-Object {Rename-Item $_ -NewName {$_.Replace(“,”,“”)}}
CategoryInfo : MetadataError: ( [Rename-Item], ParameterBindingException
FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand
Neally
(Neally)
June 27, 2020, 6:30pm
5
as said…please give a sample so we can help with the code…
Neally
(Neally)
June 27, 2020, 6:47pm
6
something like this in the theory, again without sample data, tough to test…
$csv = import-csv ".\file.csv"
foreach ($line in $CSV) {
if(test-path $line.company){
Rename-Item $line.company -NewName $($line.company -replace ",")
}
else{
new-item $($line.company -replace ",") -ItemType Directory
}
}
@alexw
No problem. As an example, if you browse to the network path where these folders are located the directory will currently display the folders as such:
Balloons, Inc
Crayons, LTD
Flat Cap, Inc
Vandelay Industries, Inc
So I’d want to rename each folder so they display as follows:
Balloons Inc
Crayons LTD
Flat Cap Inc
Vandelay Industries Inc
jitensh
(JitenSh)
June 27, 2020, 7:11pm
8
commas will be treated as row or column if import as CSV remove the header company , try get-content
$csv = (get-content C:\file.csv)-replace(',')
foreach($line in $CSV){
new-item $($line) -ItemType Directory
}
Neally
(Neally)
June 27, 2020, 7:24pm
9
commas will be treated as row or column if import as CSV
totally depends on the CSV, that’s why i asked 3 times already for a sample…
1 Spice up
jitensh
(JitenSh)
June 28, 2020, 4:47am
10
he has already posted a sample and based on that.
^ → text after comma is removed.
but if you do get-content, you get results
@alexw
Neally
(Neally)
June 28, 2020, 6:45am
11
You don’t know if it’s In quotes in the CSV
“Folder, Inc” will be treated differently than “Folder”,”Inc”, as you said, so it helps to know the raw data
This should rename all folders under $Path even if they are nested. Rename-Item is run with -WhatIf so no changes will be done, unless you remove it
$Path = 'C:\Test'
# Sorting by FullName in descending order puts nested folders on top of the list.
# Otherwise renaming nested folders will fail since Rename-Item can't rename whole path at once
$Folders = Get-ChildItem -Path $Path -Directory -Recurse | Sort-Object FullName -Descending
foreach ($Folder in $Folders) {
# Renames PSParentPath prefix otherwise Rename-Item will not work
$Parent = $Folder.PSParentPath.Replace('Microsoft.PowerShell.Core\FileSystem::', '')
# Since Rename-Item can rename only base folder and not full path, string replacement is done only for base folder
$NewFolderBaseName = $Folder.BaseName.Replace(',', '')
$NewFolderFullName = $Parent + '\' + $NewFolderBaseName
Rename-Item -Path $Folder.FullName -NewName $NewFolderFullName -WhatIf
}
@alexw
I actually figured it out after additional testing. This is the syntax I used to remove the commas once the folders are already created (not when the folders are first getting created)
Get-ChildItem "PathToFolderLocation" -Filter "*,*" -Recurse |
Where-Object {$_.Name -match ","} |
Rename-Item -NewName {$_.Name -replace ",",""}
The only other thing I’m trying to figure out as an example is to take the string “Inc” and change it to read as “INC”. I was hoping I would have been able to use the syntax as such:
Get-ChildItem "PathToFolderLocation" -Filter "*Inc" -Recurse |
Where-Object {$_.Name -match "Inc"} |
Rename-Item -NewName {$_.Name -replace "Inc","INC"}
When I run that I receive the following error:
Rename-Item : Source and destination path must be different.
At line:3 char:1
Rename-Item -NewName {$_.Name -replace “Inc”,“INC”}
CategoryInfo : WriteError: (C:\Users\AH2548…technology, Inc:String) [Rename-Item], IOException
FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Not sure why it’s complaining about the source and destination paths since of course the source and destination are the same since I’m just changing the string of the same folder names. This should be similar to when I perform the rename-item function when just removing the commas so I’m not sure why this is different, unless it is because the letters are the same and just a couple of them are changing case from lower to upper and it doesn’t like that for some reason.
-replace is not case sensitive, -creplace should do the trick
Edit: Nevermind above comment, nothing to do with your problem
Not sure if there is another method to change capitalization, but you could just first rename “inc” to “inc_” and afterwards change “inc_” to “INC”
@jonesbones
Good point, was just seeing if there was a quicker way, and it’s not that this request has come up but I’m just trying to cover all bases in case someone does make the request. Thank you.
DoctorDNS
(DoctorDNS)
June 29, 2020, 1:04pm
16
Just a quickie
The error message says there is an error passing a script block = the code did this:
ForEach-Object {Rename-Item $_ -NewName {$_.Replace(",","")}}
The clue is that the parameter value is enclosed in {} which makes it a script block, Instead what about this:
ForEach-Object {Rename-Item $_ -NewName ($_.Replace(",","")) }
1 Spice up