Reputation: 105
I am currently using the following code to remove the defined special characters and white spaces from any file names within my defined directory:
For Each file As FileInfo In files
newName = Regex.Replace(file.Name, "[!@#$%^&*()_ ]", "")
If (file.Name <> newName) Then
newPath = Path.Combine(dir, newName)
file.CopyTo(newPath)
End If
Next
Edit: How do I trim the characters of the new file name (newName) to all but the first 26 characters?
Answer:
For Each file As FileInfo In files
If (file.Name.Length >= 36) Then
Dim maxLen As Integer = 26 - file.Extension.Length
newName = ${Regex.Replace(Path.GetFileNameWithoutExtension(file.Name), "[!@#$%^&*()_ ]",
"").Substring(0, maxLen)}{file.Extension}"
newPath = Path.Combine(dir, newName)
file.CopyTo(newPath, True)
ElseIf (file.Name.Length < 36) Then
newName = Regex.Replace(file.Name, "[!@#$%^&*()_ ]", "")
If (file.Name <> newName) Then
newPath = Path.Combine(dir, newName)
file.CopyTo(newPath)
End If
End If
Next
Upvotes: 1
Views: 1111
Reputation: 6111
If you are asking how to get a subset of a String, you would get the Substring. If you want the first 26 characters it would be the second overload.
Example:
Dim filename As String = "this is a terrible f^ln@me! that is (way) too long.txt"
Dim newFilename As String = Regex.Replace(filename, "[!@#$%^&*()_ ]", String.Empty).Substring(0, 26)
Live Demo: Fiddle
Update
As per your comment, here is how you'd trim down just the filename without the extension:
Dim filename As String = "this is a terrible f^ln@me! that is (way) too long.txt"
Dim extension As String = Path.GetExtension(filename)
Dim shortFilename As String = Path.GetFileNameWithoutExtension(filename)
Dim newFilename As String = Regex.Replace(shortFilename, "[!@#$%^&*()_ ]", String.Empty).Substring(0, 26) & extension
Live Demo: Fiddle
Upvotes: 0
Reputation:
You can use Linq as follows:
Dim dir = "c:\myFolder"
Dim except = "[!@#$%^&*()_ ]".ToArray
For Each file As FileInfo In files
Dim maxLen As Integer = 26 - file.Extension.Length
Dim newPath = Path.Combine(dir,
$"{New String(Path.GetFileNameWithoutExtension(file.Name).
ToCharArray.
Except(except).
Take(maxLen).
ToArray)}{file.Extension}")
file.CopyTo(newPath, True)
Next
Suppose you have a file with name:
abcdefg_hijk!lm@pno#pq%r(stuvy)x$z.dbf
The newPath output will be:
c:\myFolder\abcdefghijklmpnoqrstuv.dbf
If that is what you need to do.
Edit:
Alternative using RegEx:
Dim except = "[!@#$%^&*()_ ]"
For Each file As FileInfo In files
Dim maxLen As Integer = 26 - file.Extension.Length
Dim newName = $"{Regex.Replace(Path.GetFileNameWithoutExtension(file.Name),
except,
"").Substring(0, maxLen)}{file.Extension}"
Dim newPath = Path.Combine(dir, newName)
file.CopyTo(newPath, True)
Next
So the newPath
for a file with name:
n_BrucesMiddle NH 12 34 5 W3_H.dbf
... will be:
c:\myFolder\nBrucesMiddleNH12345W3.dbf
The unwanted characters have been removed and the maximum length of the new file name (newName
) including the extension is 26.
Here's regex101 example.
Again, if that is what you need. Good luck.
Upvotes: 1
Reputation: 32455
To rename files you can use .MoveTo
method.
From the docs:
Moves a specified file to a new location, providing the option to specify a new file name.
You probably want to rename only the "name" part so extension remain unchanged.
This approach will support any file extension (not only extension with 3 characters)
For Each file As FileInfo In files
Dim newName As String = Path.GetFileNameWithoutExtension(file.Name).Remove(26)
Dim newPath = Path.Combine(file.DirectoryName, $"{newName}{file.Extension}")
file.MoveTo(newPath)
Next
Upvotes: 1
Reputation: 471
Use String.Remove
newName = newName.Remove(26)
Note that: string length should be greater than or equal to 26
EDIT:
If you want the extension to remain. use this instead:
newName = newName.Remove(26, newName.length - 30)
Upvotes: 1