Using VB6
I want to delete the last 5 words of the filename, then i want to give so other filename.\
Code.
Name FileName As NewFileName
The above code is working for rename, but i don't want to rename, i want to delete the last 5 letter of the filename.
Expected Output
Filename
sufeshcdk.txt - I want to take (sufeshcd) only
Modifyulla.txt - I want to take (Modifyul) only
How to do this?
Need VB6 Code Help.
Here you go.
private function RemoveLast5(FileName as string) as String
if len(FileName) > 5 then
RemoveLast5 = left$(FileName, Len(FileName) - 5)
else
RemoveLast5 = FileName
end
end function
dim FileName as string
FileName = "Modifyulla.txt"
dim NewFileName as string
NewFileName = RemoveLast5(FileName)
Name FileName As NewFileName
Untested, but this is the basic idea...
FileNameLength = Len(FileName)
NewFileName = Mid$(FileName, 1, FileNameLength - 5)
Name FileName As NewFileName
edit: fixed the syntax per below comments.
Related
Is there an way to dynamically change a new folder name if a folder with the same name already exists? For example, if I were to create a new file using fso.CreateFolder, would there be any way for the script to check for the folder and add an extra character, e.g from ("example") to ("example a"). I realize that I could use a file name from a variable and add characters like so:
dim filename
filename = ("example")
folderexists = fso.FolderExists(filename)
if (folderexists) then
fso.CreateFolder(filename + "a")
else
fso.CreateFolder(filename)
endif
but this would only work once, and after that would just continue to create and overwrite (filename + "a"). I would like the script to be able to detect, so for example:
first folder = (filename)
second folder = (filename + "a")
Third folder = (filename + "aa")
fourth folder = (filename + "aaa")
and so forth.
Something like this should do what you want:
i = 0
If fso.FolderExists(filename) Then
Do
i = i + 1
newname = filename & String(i, "a")
Loop While fso.FolderExists(newname)
filename = newname
End If
fso.CreateFolder(filename)
Personally I'd prefer a number suffix over a string of increasing length, though:
i = -1
If fso.FolderExists(filename) Then
Do
i = i + 1
newname = filename & Right("000" & i, 3)
Loop While fso.FolderExists(newname)
filename = newname
End If
fso.CreateFolder(filename)
Or you could append a timestamp as #Noodles suggested in the comments to the question:
Function LPad(n) : LPad = Right("00" & n, 2) : End Function
timestamp = Year(Now) & LPad(Month(Now)) & LPad(Day(Now)) & LPad(Hour(Now)) & _
LPad(Minute(Now)) & LPad(Second(Now))
fso.CreateFolder(filename & timestamp)
If you're just looking to create a temporary folder name, I usually do something like this:
Do
strFolder = "c:\" & Left(fso.GetTempName(), 8)
Loop While fso.FolderExists(strFolder)
How does it work?
GetTempName() returns an 8.3 filename in the following format:
radXXXXX.tmp
where each X represents a random hexadecimal digit. So you can just extract the first 8 chars to use for your folder name (or you can use the full name -- there's nothing stopping a folder from having an "extension"). Then just loop until you generate one that doesn't already exist.
just looking for some help on this. I've tried a few different things and I'm stumped. I'm trying to take a check if a file exists in a directory, and if it does, then I want to rename add - Copy to the file name. Then it should check again if there is a conflict, and if there isn't it should move the file. Sounds simple enough, but it isn't working at all. Since I can't convert from String to DirectoryInfo, I have to declare multiple variables, and it just doesn't feel right. What can I do to fix this?
Dim fileExt As String = ""
Dim oldFileName As String = file.FullName
Dim newFileName As String = oldFileName
Dim newFileLocation = Environment.GetSpecialFolder(Environment.SpecialFolder.MyPictures) + "\" + file.Name
While FileIO.FileSystem.FileExists(newFileLocation) 'While File exists in new directory
'Add copy to filename
fileExt = fileType.Replace("*", "")
newFileName = newFileName.Remove(newFileName.LastIndexOf("."), (newFileName.Length - newFileName.LastIndexOf(".")))
newFileName += " - Copy"
newFileName += fileExt
'Rename file
FileSystem.Rename(oldFileName, newFileName)
'Declare a new DirInf variable because I can't use a string to set one
Dim newFile As New DirectoryInfo(newFileName)
'Move the new file to
newFile.MoveTo("C:\Users\" + Environ("USERNAME") + "\Pictures\")
ProgressBar.Value += 1
End While
You're looking for File.Move(), which takes two strings.
Also, "C:\Users\" + Environ("USERNAME") + "\Pictures\" is very wrong; many users do not have C: drives.
You should call Environment.GetSpecialFolder(Environment.SpecialFolder.MyPictures)
I have a vbscript that is used to rename files. What I need to implement into the script is something that deletes the "new file" if it already exists.
For example: I have a batch of files that are named like this 11111111.dddddddd.pdf where the files get renamed to 11111111.pdf. The problem is that when I rename to the 11111111.pdf format I end of with files that are duplicated and then makes the script fail because you obviously cant have 2 files with the same name. I need it to rename the first one but then delete the others that are renamed the same.
Here is what I have so far for my IF statement but it doesnt work and I get and error that says "Type mismatch: 'FileExists". I am not sure how to get this part of the code to execute the way I would like. Any help or suggestions would be greatly appreciated.
dim infolder: set infolder = fso.GetFolder(IN_PATH)
dim file
for each file in infolder.files
dim name: name = file.name
dim parts: parts = split(name, ".")
dim acct_, date_
acct_ = parts(0)
date_ = parts(1)
' file format of a.c.pdf
if UBound(parts) = 2 then
' rebuild the name with the 0th and 2nd elements
dim newname: newname = acct_ & "." & parts(2)
' use the move() method to effect the rename
file.move fso.buildpath(OUT_PATH, newname)
if newname = FileExists(file.name) Then
newname.DeleteFile()
end if
end if
next 'file
You're close, you just need to delete the file before trying to over-write it.
dim infolder: set infolder = fso.GetFolder(IN_PATH)
dim file: for each file in infolder.Files
dim name: name = file.name
dim parts: parts = split(name, ".")
if UBound(parts) = 2 then
' file name like a.c.pdf
dim newname: newname = parts(0) & "." & parts(2)
dim newpath: newpath = fso.BuildPath(OUT_PATH, newname)
' warning:
' if we have source files C:\IN_PATH\ABC.01.PDF, C:\IN_PATH\ABC.02.PDF, ...
' only one of them will be saved as D:\OUT_PATH\ABC.PDF
if fso.FileExists(newpath) then
fso.DeleteFile newpath
end if
file.Move newpath
end if
next
fileExists() is a method of FileSystemObject, not a global scope function.
You also have an issue with the delete, DeleteFile() is also a method of FileSystemObject.
Furthermore, it seems you are moving the file and then attempting to deal with the overwrite issue, which is out of order. First you must detect the name collision, so you can choose the rename the file or delete the collision first. I am assuming for some reason you want to keep deleting the new files until you get to the last one, which seemed implied in your question.
So you could use the block:
if NOT fso.FileExists(newname) Then
file.move fso.buildpath(OUT_PATH, newname)
else
fso.DeleteFile newname
file.move fso.buildpath(OUT_PATH, newname)
end if
Also be careful that your string comparison with the = sign is case sensitive. Use strCmp with vbText compare option for case insensitive string comparison.
IF both POS_History_bim_data_*.zip and POS_History_bim_data_*.zip.trg exists in Y:\ExternalData\RSIDest\ Folder then Delete File Y:\ExternalData\RSIDest\Target_slpos_unzip_done.dat
Using VB6
Code.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
fName = Right(flname, Len(flname) - posn)
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
FileName: Clockings8.mis06042009 120442.fin
But it is showing a filename is “Clockings8”. It should show “Clockings8.mis06042009 120442”
How to modify a code?
Need vb6 code Help
It is a bit cleaner to use the Scripting.FileSystemObject component. Try:
Dim fso as New Scripting.FileSystemObject
GetFileName = fso.GetBaseName(fname)
The reason why your code stops short is that InStr works from the beginning of the string to the end and stops wherever it finds a match. The filename "Clockings8.mis06042009 120442.fin" contains two periods. For this reason, you should use InStrRev instead to start the search from the end of the string.
Going with FileSystemObject's GetBaseName like David suggests is a good idea. If you can't or don't want to (and there are reasons why you might not want to) work with the FileSystemObject there's a simple solution: Remove all characters from a filename string starting with the last dot in the name.
Here's what I mean:
Dim fn As String
fn = "Clockings8.mis06042009 120442.fin"
Dim idx As Integer
idx = InStrRev(fn, ".")
GetFileName = Mid(fn, 1, idx - 1)
If your filename does not have an extenstion but has a dot somewhere in the filename string, then this method will return bad results.
How to get a file name only from file's full path?
MY path - C:\Documents and Settings\Arshad\My Documents\ravi.txt
Have a look at this: Retrieve Filename without Path or Extension.
Here's the function from the above link:
Public Function GetFileName(flname As String) As String
'Get the filename without the path or extension.
'Input Values:
' flname - path and filename of file.
'Return Value:
' GetFileName - name of file without the extension.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
'find the position of the last "\" character in filename
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
'get filename without path
fName = Right(flname, Len(flname) - posn)
'get filename without extension
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
End Function
When inputting
C:\Documents and Settings\Arshad\My Documents\ravi.txt
this function returns
ravi
The function is called as such:
Dim FileName As String
FileName = GetFileName("C:\Documents and Settings\Arshad\My Documents\ravi.txt")
This is the shortest way:
Public Function GetFileName(FilePath As String) As String
Dim l() as string
l=split(FilePath,"\")
GetFileName=l(UBound(l))
End Function
Tell me, if you can find shorter code;)
Here you go:
http://www.vbforums.com/showthread.php?t=475667
-
Here are the better and short way.
Return file name from FullPath
' ** C:\Windows\System32\calc.exe > Ret: calc.exe
Private Function GetFilenameFromPath(FullPath As String) As String
GetFilenameFromPath = Right(FullPath, Len(FullPath) - InStrRev(FullPath, "\"))
End Function
Return Path from FullPath
' C:\Windows\System32\calc.exe > Ret: C:\Windows\System32\
Private Function GetDirectoryFromPath(FullPath As String) As String
GetDirectoryFromPath = Left(FullPath, InStrRev(FullPath, "\"))
End Function