PowerShell - Remove certain string from multiple filenames - string

I have multiple .mp3 files in the catalogue and want to remove artist's name from their filenames.
All the files' names contain string
"Adhesive Wombat - "
followed by the song's title.
I want to remove (not replace) every occurence of that specific string in every filename in current folder.
Not to mention, I've searched for answer on this site and failed to find an elegant and satisfying solution.
This is my first encounter with PowerShell.

As you know what you have is really close.
Dir | ren -NewName { $_.Name -replace "Adhesive Wombat - ", "" }
You added a stipulation that "Adhesive Wombat" may or may not contain a space. -replace uses regular expressions so lets harness that and make that space optional using a ?. I am also going to show you the full cmdlet name as supposed to aliases so you are aware of what is happening behind the scenes. It's hard to see but I tried to highlight the ? in bold for it to "pop".
Get-ChildItem | Rename-Item -NewName {$_.Name -replace "Adhesive ?Wombat - ", "" }
So that will match both
"other text Adhesive Wombat - other text "
"other text AdhesiveWombat - other text "
Also, depending on how much white space there is you could use \s+ where you have space which will match all consecutive white space.

From the above answers, the quick working example for simply copy&paste, that worked on win 10 Powershell:
change directory to your target directory
define files with e.g. *.txt or set all files with *.*
insert your Strings for replacement
cd C:\....
Get-ChildItem *.* | Rename-Item -NewName {$_.Name -replace "StringtoReplace", "newString"}

Related

Replace whole text in a text file using command prompt?

Is there a way to replace whole text from a text file using command prompt? As far as I have come across is about replacing part of a text through find and replace. Is there a way to replace the complete texts from text file?
powershell -Command "(gc Pro.txt) -replace 'One', 'Two' | Out-File -encoding ASCII Pro.txt"
Found it, the way to replace whole text is:
#echo myText > Pro.txt

PowerShell unable to remove space between text and String

In PowerShell I am trying to find a way to remove the text from an output of text and a String.
Write-Host 'File located at C:\'$Fileline.FilePath -
I get an output of
c:\ program files\path
The space between c:\ and "Program files" is what I want to remove. Do I have to convert the text to a string, and then output it as two strings and then remove the spaces?
This is happening because you are passing multiple strings to Write-Host, which it is then joining with spaces. This behaviour is somewhat unique to Write-Host.
You can meet your need by sending a single double quoted string to Write-Host, which you can then put your variable inside and it will be expanded. However because you are accessing a property of your variable, you need to wrap it in a sub-expression: $():
Write-Host "file located at C:\$($Fileline.FilePath) -"
Try using the PowerShell -f formatting operator:
Write-Host ("File located at C:\{0} -" -f $FileLine.FilePath)
There's good info on -f at SS64 and at TechNet

Recursively find text in files (PowerShell)

I want to find all files in a particular directory that contain a certain string. Specifically, I want to find the carriage return (\r) and then manually go through the files and remove occurrences of it. I don't want PowerShell to remove them because there may be certain subdirectories that require them in my directory structure.
Your question seems to boil down to: I want to find all files in a directory that contain the carriage return. Is there any reason why these answers are not adequate?
For example:
This should give the location of the files that contain your pattern:
Get-ChildItem -recurse | Select-String -pattern "dummy" | group path | select name
You could use Select-String to find patterns in a group of files, however this will only search for the pattern within each line of text. It will not work in your case because you want to search for the line separator.
An easier way is to use Get-Content, this converts a file to an array of strings, one item per line. Therefore any file that has a return and a line feed will produce an array with more than one item. Try something like this:
Get-ChildItem -recurse | ? {(Get-Content $_.FullName).count -gt 1}
If your files are quite large and you want to speed it up then add a -TotalCount like this:
Get-ChildItem -recurse | ? {(Get-Content $_.FullName -TotalCount 2).count -gt 1}
This will only find where the return and line feed are together, if you want instances where they could appear on their own then you will need to force Get-Content to not read the file as an array of strings but as one big string instead. Something like this:
Get-ChildItem -recurse | ? {[regex]::Matches((Get-Content $_FullName -Raw), '[\r\n]+').Count -gt 0}

Handle a CSV file with single quoted string and occasional comma or single quote within the single quoted string

I have a CSV file with text columns quoted in single quote around it and other non text columns don't have a quote around it.The text columns might have a comma or single quote within these single quoted text columns. I found a script online but it doesn't handle this kind of situation.
Is there way to handle this in PowerShell?
Example:
123,678.89,'hello there1', 'xyz1#gmail.com', 'abc,nds'\n
123,678.89,'hello 'there2', 'xyz2#gmail.com', 'akiu-'nds'\n
Output:
123,678.89|hello there1|xyz1#gmail.com|abc,nds \n
123,678.89|hello 'there2|xyz2#gmail.com|akiu-'nds \n
Example 2:
123,6272,678.89,,,'hello ,there1',,,,'abc1','tw,es',,'xyz1#gmail.com',,,,,,'abc,nds1'\n
124,8272,928.89,,,,'hello 'there2',,,'abc2','twes',,,'xyz2#gmail.com',,'biej',,,'abc'nds2'\n
125,9272,328.89,,'hello 'there3',,'abc3',', outyi',,,,'xyz3#gmail.com',,,,,,'ahct','abc'nds3'\n
Output:
123|6272|678.89|||hello ,there1||||abc1|tw,es||xyz1#gmail.com||||||abc,nds1\n 124|8272|928.89||||hello 'there2|||abc2|twes|||xyz2#gmail.com||biej|||abc'nds2\n
125|9272|328.89||hello 'there3||abc3|, outyi||||xyz3#gmail.com||||||ahct|abc'nds3\n
Similar to Kiran's answer. There are a couple of things that need to change so I don't think that there is a one size fits all solution. We need to chain these couple of changes. First being the commas that are actually delimiters and second the special end of line character sequence.
$path = "c:\temp\file.csv"
$newDelimiter = "|"
(Get-Content $path) -replace "'\s*?,\s?'|,\s?'|'\s?,",$newDelimiter -replace "'\s*?\\n$","\n" | Set-Content $path
I have a regex101 link that explains with more detail. The regex doing the greater work is the first with three potential alternate matches. This effectively ignores quotes that are off by themselves. If there is data that has a quote and comma combo then I think it would be following to program this without more information.
'\s*?,\s?': Comma enclosed in quotes optionally surrounded by variant white-space.
,\s?': Comma with optional space followed by a quote
'\s?,: Quote with optional space followed by a comma
So a match of any of the above groups would be replaced with $newDelimiter. Second regex is just looking for '\n$ while accounting for potential optional white-space between the quote and \n that happens at the end of the line. This is how the last single quote is removed.
something like this?
Get-Content C:\temp\file.txt |
ForEach-Object {$_ -replace ",'|',\s+'",'|' -replace "'\\n",' \n'} |
Set-Content C:\temp\newfile.txt
Note: the code is just one line, broken into 3 separate lines to read better.

Searching for specific string in a file

I need to find this pattern, "LogEntry=", across multiple lines in the following file:
C:\test.conf
And change the line to read: "LogEntry=&MAC=" instead.
The script works fine so far to do that, but if someone runs the script twice it will re-add the same pattern, doubling it up.
I need to find a way to put a check in place to know if the file already has that pattern in it. Could someone please give me a hand with this one ?
Use negative lookahead in your regex e.g.:
'LogEntry=(?!&MAC=)'
That regex will not match the already modified line. Read more about look ahead/behind zero length assertions.
BTW if you have the PowerShell Community Extensions, you can do this edit operatio with a single command:
Edit-File C:\test.conf -Pattern 'LogEntry=(?!&MAC=)' -Replace 'LogEntry=&MAC='
And if you can't use PSCX's Edit-File, here is roughly the equivalent:
$content = Get-Content C:\test.temp
$content | Foreach {$_ -replace 'LogEntry=(?!&MAC=)','LogEntry=&MAC='} | Out-File -Encoding ASCII
I don't know what the file encoding is for your file. You need to know that and use the appropriate encoding on the Out-File command. If you don't specify the encoding, it defaults to Unicode.

Resources