Find number pattern in string and remove it - c#

I need to remove a pattern from a string, I think regex could do the job, but I'm having trouble solving this.
The pattern must be in the end of the string.
string fileName = "File (123)";
string pattern = " (0)";
string cleanName = PatternRemover(fileName, pattern);
//Should result in: cleanName == "File"
Edit:
Ok, here is the code that I'm using now after your answers:
public static string GetNextFilePath2(string fullPath, ref uint id, string idFormat)
{
string dir = Path.GetDirectoryName(fullPath);
string ext = Path.GetExtension(fullPath);
string fileNameNoExt = Path.GetFileNameWithoutExtension(fullPath);
if (ext.Length > 0 && ext[0] != '.')
ext = "." + ext;
string baseName = Regex.Replace(fileNameNoExt, #"\s\(\d+\)", "");
string fileName = baseName + " (" + id.ToString(idFormat) + ")" + ext;
string path = Path.Combine(dir, fileName);
while (File.Exists(path))
{
id++;
fileName = baseName + " (" + id.ToString(idFormat) + ")" + ext;
path = Path.Combine(dir, fileName);
}
return path;
}
It works, but:
It always start to count from id, I think it may be better to start
from the file name number.
I was hopping to use something like "(0)" as a method parameter that would indicate the pattern to be removed and also the "(" would be parametrized. I'm doing it "manually" now on this line: string fileName = baseName + " (" + id.ToString(idFormat) + ")" + ext;

You can do that without REGEX like:
string newFileName = new String(fileName
.Where(r => !char.IsDigit(r)
&& r != '('
&& r != ')'
&& r != ' ').ToArray());
This would give you File.jpg
If you only want to get the file name then you can use:
string fileNameWithoutPath = Path.GetFileNameWithoutExtension(newFileName);
// it would give you `File`

Using regex:
var subject = "File (123).jpg";
var fileNameWithExtension = Regex.Replace(subject,#"\s*\(\d+\)","");
var fileNameWithoutPath = Path.GetFileNameWithoutExtension(fileNameWithExtension);
And thanks for #habib, I'd not have come with Path.GetFileNameWithoutExtension in this for stripping the extension.

You could use:
\s\(\d+\)\.jpg
assuming you do actually want the extension removed and the extension is always ".jpg". Otherwise:
\s\(\d+\)
Looks for a set of digits in brackets proceeded by a space.

Related

Dropbox API - UploadAsync Value Should Match

I'm trying to use the Dropbox API in order to do a file upload.
string accesstoken = "token";
using (DropboxClient client =
new DropboxClient(accesstoken, new DropboxClientConfig("NameOfApp")))
{
string[] spitInputFileName = file.FileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
string filenameAndExtension = spitInputFileName[spitInputFileName.Length - 1];
string[] filenameAndExtensionSplit = filenameAndExtension.Split('.');
string originalFileName = filenameAndExtensionSplit[0];
string originalExtension = filenameAndExtensionSplit[1];
String filename = "" + originalFileName + Guid.NewGuid().ToString().Replace("-", "") + "." + originalExtension;
var updated = client.Files.UploadAsync(
filename,
mode: WriteMode.Overwrite.Overwrite.Instance,
body: file.InputStream).Result;
var result = client.Sharing.CreateSharedLinkWithSettingsAsync(filename).Result;
Within the body: file.InputStream.Result; section, It encounters below error :
An exception of type 'System.ArgumentOutOfRangeException' occurred in Dropbox.Api.dll but was not handled in user code
Value should match pattern '\A(?:(/(.|[\r\n])*)|(ns:[0-9]+(/.*)?)|(id:.*))\z'
What is the reason of this exception?
Paths should start with '/'.
File path should be something like this:
/test.txt
In your case:
String filename = "/" + originalFileName + Guid.NewGuid().ToString().Replace("-", "") + "." + originalExtension;

How to obtain regex matched string 's file path?

I have successfully regex matched multiple string from a folder with txt.files with "streamreader" but i also need to obtain the matched string's file path.
How am i able to obtain the matched string's file paths?
static void abnormalitiescheck()
{
int count = 0;
Regex regex = new Regex(#"(#####)");
DirectoryInfo di = new DirectoryInfo(txtpath);
Console.WriteLine("No" + "\t" + "Name and location of file" + "\t" + "||" +" " + "Abnormal Text Detected");
Console.WriteLine("=" + "\t" + "=========================" + "\t" + "||" + " " + "=======================");
foreach (string files in Directory.GetFiles(txtpath, "*.txt"))
{
using (StreamReader reader = new StreamReader(files))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Match match = regex.Match(line);
if (match.Success)
{
count++;
Console.WriteLine(count + "\t\t\t\t\t" + match.Value + "\n");
}
}
}
}
}
If possible , i want to have output of the strings's file path as well.
For e.g.,
C:/..../email_4.txt
C:/..../email_7.txt
C:/..../email_8.txt
C:/..../email_9.txt
As you already have the DirectoryInfo, you could get the FullName property.
You also have the filename called files. To get the name and location of the file, you could use Path.Combine
Your updated code could look like:
Console.WriteLine(count + "\t" + Path.Combine(di.FullName , Path.GetFileName(files)) + "\t" + match.Value + "\n");
I'm guessing that we might just want to maybe match some .txt files. If that might be the case, let's start with a simple expression that would collect everything from the start of our input strings up to .txt, then we add .txt as a right boundary:
^(.+?)(.txt)$
Demo
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"^(.+?)(.txt)$";
string input = #"C:/..../email_4.txt
C:/..../email_7.txt
C:/..../email_8.txt
C:/..../email_9.txt";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}

FileUpload if exist concat (counter) to the name [duplicate]

This question already has answers here:
FileUpload - if file name exist concat a number between parentheses at the end of name
(3 answers)
Closed 5 years ago.
I have a fileupload control, What I want to achieve is to concat a nuber to it, in order it to be unique.
All the posts that I found are talking about adding a GUID or DateTime
What i want to achieve is if the file exist in folder then the file name will be filename + (counter)
Example:
The folder contain a file name- file.png
1) When I upload the same file name again, the existing one wont get delete and the new one will be called file(1).png.
2) When I upload the same file name again, (file.png)
now the new file will be called file(2)
I have this code which handle the 1'st case but not the second:
public static string GetUniqueName(string fileName)
{
string dir = Globals.Directories.GetCustomCategoryThumbnailDir();
string fileExtension = Path.GetExtension(fileName);
string fileNameWE = Path.GetFileNameWithoutExtension(fileName);
string[] files = Directory.GetFiles(dir, "*" + fileExtension)
.Select(Path.GetFileName)
.ToArray();
string uniqueName = fileNameWE;
int nextNum = 0;
bool fileExist = false;
string pattern = #"(.*)\(([\d]+)\)";
foreach (var file in files)
{
var tempFileName = Path.GetFileNameWithoutExtension(file);
var match = Regex.Match(tempFileName, pattern);
if (tempFileName.Equals(fileNameWE))
{
// file exist in folder
fileExist = true;
}
if (tempFileName.StartsWith(fileNameWE) && match.Success)
{
// there is a file name that start with "fileToUpload" name, we want to to take the number
nextNum = Convert.ToInt32(match.Groups[2].Value);
nextNum++;
}
}
if (nextNum == 0 && !fileExist)
{
// filename dont exist
return fileNameWE + fileExtension;
}
if (nextNum == 0 && fileExist)
{
// the file name exist without (1)
fileNameWE = $"{fileNameWE}(1)";
return fileNameWE + fileExtension;
}
else
{
var haveParentessis = Regex.Match(fileNameWE, pattern);
if (haveParentessis.Success)
{
// we need to reset the nextNum
nextNum = 1;
}
// return the new unique name with suffix
fileNameWE = string.Format("{0}({1})", fileNameWE, nextNum);
return fileNameWE + fileExtension;
}
}
How can I achieve that?
Yeah, that's cause your while loop as pointed below is local to the function and thus the counter variable which is also a local variable won't be maintained it's current value. So in essence every request you get a new page instance and thus your counter will get re-initialized to 0
while (File.Exists(Path.Combine(dir, fileNameWE + fileExtension)))
{
fileNameWE = fileNameWE + "(" + counter.ToString() + ")";
counter++;
}
To get this done, you should store the counter variable in Session and retrieve it back. Don't forget it's an web application and thus the state won't be maintained unless you do it explicitly
if (File.Exists(Path.Combine(dir, fileNameWE + fileExtension)))
{
counter = (Session["counter"] != null) ? ((int)Session["counter"])++ : default(int);
fileNameWE = fileNameWE + "(" + counter.ToString() + ")";
Session["counter"] = counter;
}
I got this to work by using regular expressions, below. No persistent counter is required. I think the concat problem is because there's nothing done to deal with the existing (1) suffix. I coded this potential solution and hope it helps! NB: only lightly tested in debugger, and I am a novice
{
string fileName = #"C:\Uploads\Testfile.bin";
string fileExtension = Path.GetExtension(fileName);
string fileNameWE = Path.GetFileNameWithoutExtension(fileName);
string pattern = #"(.*)\(([\d]+)\)$";
var match = Regex.Match(fileNameWE, pattern);
if (match.Success)
{
int nextNum = Convert.ToInt32(match.Groups[2].Value);
nextNum++;
fileNameWE = $"{match.Groups[1].Value}({nextNum})";
}
else
{
fileNameWE = $"{fileNameWE}(1)";
}
}

C# Filter differences from a path

DirectoryPath = C:\Pics
filePath = C:\Pics\Dogs\dog.PNG
newPath should be: Dogs\dog.PNG
How do I get the newPath?
My code snippet is not right
string directoryPath = "C:\\Pics";
string filePath = "C:\\Pics\\Dogs\\dog.PNG";
if (!directoryPath.EndsWith("\\"))
directoryPath = directoryPath + "\\";
string newPath = filePath.Substring(filePath.LastIndexOf(directoryPath) + 1);
Thanks in advance!
Could you append a backslash to the directory path & then in the filepath replace the directory path with an empty string
newPath = filePath.Replace(DirectoryPath + #"\", string.Empty);
If the directoryPath does not match the start of the filePath, then newPath will be unchanged.
I did post this before you edited your code to show the conditional adding of the backslash - so that can be removed in the above code.
The int Index you get from LastIndexOf() will always starts with the rightmost value, in your case 0. You have also to add the String.Lenght for this.
if (filePath.StartsWith(directoryPath))
{
string newPath =
filePath.Substring(filePath.LastIndexOf(directoryPath) + directoryPath.Length + 1);
}
I would first check if filePath contains DirectoryPath,so i'd do something like this:
var newPath=filePath.Contains(DirectoryPath)?filePath.Substring(DirectoryPath.Length + 1)
:filePath;
Or even better,using StartsWith
var newPath=filePath.StartsWith(DirectoryPath)?filePath.Substring(DirectoryPath.Length + 1)
:filePath;

Need Help the best overloaded method match for 'string.endswith(string)' has some invalid arguments

dynamic counter = 1;
string FileNameWithoutExtestion = "";
FileNameWithoutExtestion = file.Split('.')[0];
string FileExtestion = file.Split('.')[1];
while (System.IO.File.Exists(Dir + file))
{
if (true)
{
counter = counter + 1;
if (FileNameWithoutExtestion.EndsWith('_'))
{
file = FileNameWithoutExtestion + counter.ToString() + "." + FileExtestion;
}
else
{
file = FileNameWithoutExtestion + "_" + counter.ToString() + "." + FileExtestion;
}
}
}
if (FileNameWithoutExtestion.EndsWith('_')) //the error occurred here
Whats wrong ?
String.EndsWith() only has overloads with string as parameter, you insert a char.
Replace
.EndsWith('_')
with
.EndsWith("_")
and i would use those path-methods to parse filenames and extensions
string FileNameWithoutExtestion = System.IO.Path.GetFileNameWithoutExtension(file);
string FileExtestion = System.IO.Path.GetExtension(file); //.jpg
because FileNameWithoutExtestion = file.Split('.')[0]; will lead to a invalid value in case of a filename like foo.bar.jpg

Categories