trim string after "\" (1 Viewer)

Tfa

Registered User.
Local time
Today, 21:26
Joined
Nov 29, 2016
Messages
32
hello hello
i am back with one more small problem this time
in C it was pretty easy here i need a bit of help

i have this function that i take the full path name
ex. C:\Folder1\test.xls
i want to rename and nome this file to this folder with this kind of format
D:\Folder2\new\BBB_test.xls
i am looking for a way to cut the C:\Folder1\
and then replace it with D:\Folder2\new\BBB_
is there a fuction that can trim a string after it first encounters the character "" from the right
or even from the left even if that means that i have to reverse the string
????
note that the first file will not always be the same
ex C:\Folder1\test.xls
E:\something.xls
and they will always be moved to a specific file iunder the format that i mentioned
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:26
Joined
Oct 17, 2012
Messages
3,276
Nope, but you can make one with InStrRev to find the location of the final slash, then trim everything to the right of that.

Alternately, if the file already exists on your computer, you can use the Dir function.

Here's the Dir version, since I already have that handy:

Code:
Public Function GetFilePath(ByVal FilePath As String, _
                            Optional ByVal IncludeEndSlash As Boolean = True) As String
 
    GetFilePath = Left(FilePath, Len(FilePath) - Len(Dir(FilePath)) + Not IncludeEndSlash)
        
End Function

And the InStrRev version:
Code:
Public Function GetFilePath2(ByVal FilePath As String, _
                            Optional ByVal IncludeEndSlash As Boolean = True) As String
 
    GetFilePath2 = Left(FilePath, Len(FilePath) - (Len(FilePath) - InStrRev(FilePath, "\")) + Not IncludeEndSlash)
        
End Function
 

Tfa

Registered User.
Local time
Today, 21:26
Joined
Nov 29, 2016
Messages
32
well i did this and works great
but i would like your opinion

Code:
filepath = c:\folder\test1.xls
filepath2 = StrReverse(filepath)
filepath2 = Trim(Left([filepath2], (InStr(1, [filepath2], "\") - 1)))
filepath2 = StrReverse(filepath2)
filepath2 = "D:\folder2" & filepath2
FileCopy filepath, filepath2
and then i delete the original file
 
Last edited:

Tfa

Registered User.
Local time
Today, 21:26
Joined
Nov 29, 2016
Messages
32
there is a slash char between the "" but for some reason it doesn't seem to want to appear in my thread (InStr(1, [filepath2], "") - 1)))
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 14:26
Joined
Oct 17, 2012
Messages
3,276
Argh, I misread your post, thought you just wanted the path.

For your last question, I think that's a really cumbersome way to do it. More efficient would be Dir(FilePath) if the file already exists on your machine, and
Code:
Right(FilePath, Len(FilePath) - InStrRev(FilePath, "\"))
if you don't (replacing FilePath with the actual path).

Edit: Something like this would work:
Code:
Public Function ChangeFilePath(ByVal CurrentPath As String, _
                               ByVal OutputFolder As String) As String
 
        ChangeFileName = OutputFolder & IIf(Right(OutputFolder, 1) = "\", "", "\") & Right(CurrentPath, Len(CurrentPath) - InStrRev(CurrentPath, "\"))
 
End Function

Then just do your filecopy.
 

Users who are viewing this thread

Top Bottom