How to remove illegal characters from path and filenames?
Try something like this instead;
string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
foreach (char c in invalid)
{
illegal = illegal.Replace(c.ToString(), "");
}
But I have to agree with the comments, I'd probably try to deal with the source of the illegal paths, rather than try to mangle an illegal path into a legitimate but probably unintended one.
Edit: Or a potentially 'better' solution, using Regex's.
string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
illegal = r.Replace(illegal, "");
Still, the question begs to be asked, why you're doing this in the first place.
C# Remove Invalid Characters from Filename
no invalid chars returned by System.IO.Path.GetInvalidFileNameChars() being removed. – Bhuvan 5 mins ago
The first method you posted works OK for the characters in Path.GetInvalidFileNameChars()
, here it is at work:
static void Main(string[] args)
{
string input = "abc<def>ghi\\1234/5678|?9:*0";
string output = CleanFileName1(input);
Console.WriteLine(output); // this prints: abcdefghi1234567890
Console.Read();
}
I suppose though that your problem is with some language-specific special characters. You can try to troubleshoot this problem by printing out the ASCII codes of the characters in your string:
string stringFromDatabase = "/5678|?9:*0"; // here you get it from the database
foreach (char c in stringFromDatabase.ToCharArray())
Console.WriteLine((int)c);
and consulting the ASCII table: http://www.asciitable.com/
I again suspect that you'll see characters with codes larger than 128, and you should exclude those from your string.
C# string.replace to remove illegal characters
Regular expressions are generally a good way to do that, but not when you're replacing every character with something different. You might consider replacing them all with the same thing, and just using System.IO.Path.GetInvalidFileNameChars()
.
string filename = tVS.Nodes[r].Text;
foreach(char c in System.IO.Path.GetInvalidFileNameChars()) {
filename = filename.Replace(c, '_');
}
Remove illegal characters from a file name but leave spaces
Illegal characters are listed here. To replace them use this regex /[/\\?%*:|"<>]/g
like this:
var filename = "f?:i/le> n%a|m\\e.ext";
filename = filename.replace(/[/\\?%*:|"<>]/g, '-');
console.log(filename);
Javascript: How to remove illegal URL characters from a file name?
Add the .
literal as well then, \.
:
var fileName = "I am a file name + two.doc";
fileName.replace(/[^a-zA-Z0-9-_\.]/g, ''); // 'Iamafilenametwo.doc'
It's worth pointing out that the .
character in a regular expression will match any single character except the newline character. Therefore you needed to escape the character in order for it to match the literal character, \.
Also, \w
is equivilant to [A-Za-z0-9_]
, therefore you could shorten your expression to:
/[^\w.]/g
And as hwnd points out, if you don't want to allow other dot characters inside the filename, you can use subtraction:
.replace(/(?!\.[^.]+$)\.|[^\w.]+/g, '')
Remove illegal characters from filename to burn on CD
Although, in this case your file name is valid, to catch invalid file names, it is suggested to use GetInvalidFileNameChars() method.
string fileName = "The Animals - House of the Rising Sun ? (1964) + clip compilation ♫♥ 50 YEARS - counting.mp3";
byte[] bytes = Encoding.ASCII.GetBytes(fileName);
char[] characters = Encoding.ASCII.GetChars(bytes);
string name = new string(characters);
StringBuilder fileN = new StringBuilder(name);
foreach (char c in Path.GetInvalidFileNameChars())
{
fileN.Replace(c, '_');
}
string validFileName = fileN.ToString();
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidfilenamechars?view=netframework-4.7.2
How to strip illegal characters before trying to save filenames?
Casting the character array to System.String
actually seems to join the array elements with spaces, meaning that
[string][System.IO.Path]::GetInvalidFileNameChars()
does the same as
[System.IO.Path]::GetInvalidFileNameChars() -join ' '
when you actually want
[System.IO.Path]::GetInvalidFileNameChars() -join ''
As @mjolinor mentioned (+1), this is caused by the output field separator ($OFS
).
Evidence:
PS C:\> [RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars())
"\ \ \|\ \ ☺\ ☻\ ♥\ ♦\ ♣\ ♠\ \\ \t\ \n\ ♂\ \f\ \r\ ♫\ ☼\ ►\ ◄\ ↕\ ‼\ ¶\ §\ ▬\ ↨\ ↑\ ↓\ →\ ←\ ∟\ ↔\ ▲\ ▼\ :\ \*\ \?\ \\\ /
PS C:\> [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ' '))
"\ \ \|\ \ ☺\ ☻\ ♥\ ♦\ ♣\ ♠\ \\ \t\ \n\ ♂\ \f\ \r\ ♫\ ☼\ ►\ ◄\ ↕\ ‼\ ¶\ §\ ▬\ ↨\ ↑\ ↓\ →\ ←\ ∟\ ↔\ ▲\ ▼\ :\ \*\ \?\ \\\ /
PS C:\> [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
"\| ☺☻♥♦\t\n♂\f\r♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼:\*\?\\/
PS C:\> $OFS=''
PS C:\> [RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars())
"\| ☺☻♥♦\t\n♂\f\r♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼:\*\?\\/
Change your function to something like this:
Function Remove-InvalidFileNameChars {
param(
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$Name
)
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
return ($Name -replace $re)
}
and it should do what you want.
Remove illegal characters while saving workbook Excel VBA
Because there could be more illegal characters in the filename. Your approach is right but it's not comprehensive list of illegal characters to remove or replace from the filename before saving it. For eg. these characters are missing from the array in your code -> : & . However it is advised to keep filename rid of other allowed special characters too.
Below, I am providing the function which returns a safe string that can be used to produce filename before saving.
Function ReplaceIllegalCharacters(strIn As String, strChar As String) As String
Dim strSpecialChars As String
Dim i As Long
strSpecialChars = "~""#%&*:<>?{|}/\[]" & Chr(10) & Chr(13)
For i = 1 To Len(strSpecialChars)
strIn = Replace(strIn , Mid$(strSpecialChars, i, 1), strChar)
Next
ReplaceIllegalCharacters = strIn
End Function
Specifically, in your code, replace the ActiveWorkbook.SaveAs line with this line:
ActiveWorkbook.SaveAs Filename:= _
WBPath & "\BOM_" & Range("G2").Value2 & "_" & ReplaceIllegalCharacters(Range("H2").Value2, "_") & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Remove Special characters from file name
You have put private static string GetValidFileName
in public static void Main()
and in C# is not allowed. Just simple change the code as follow and it will work:
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class Test {
public static void Main() {
// your code goes here
var file_name = GetValidFileName("this is)file<ame.txt");
Console.WriteLine(GetValidFileName(file_name));
}
private static string GetValidFileName(string fileName) {
// remove any invalid character from the filename.
String ret = Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_. ]+", "")
return ret.Replace(" ", String.Empty);
}
}
Remove invalid (disallowed, bad) characters from FileName (or Directory, Folder, File)
Try the following
public string MakeValidFileName(string name) {
var builder = new StringBuilder();
var invalid = System.IO.Path.GetInvalidFileNameChars();
foreach ( var cur in name ) {
if ( !invalid.Contains(cur) ) {
builder.Append(cur);
}
}
return builder.ToString();
}
Related Topics
How to Count Number of Visitors for Website in ASP.NET C#
What Regular Expression Would I Use to Remove Everything After the Second Backslash
How to Split Json String in C# and Store in Separate Variables
How to Change Cell Background Color in Wpf Datagrid
Select Multiple Fields from List in Linq
How to Redirect to Login Page If Session Is Not Available in MVC
Cast Class into Another Class or Convert Class to Another
Select Distinct by Two Properties in a List
How to Remove Illegal Characters from Path and Filenames
How to Disable Cursor in Textbox
Sorting a Collection Containing Strings And/Or Numbers
Asp.Net Core Get Json Array Using Iconfiguration
Redirecting to Another Page in ASP.NET MVC Using Javascript/Jquery
How to Determine If a Json Object Contains Only a Specific Key
C# Foreach (Property in Object)... Is There a Simple Way of Doing This