Copying .txt file from folder to USB Flash

irade92

Registered User.
Local time
Today, 16:19
Joined
Dec 26, 2010
Messages
229
I have created .txt file from a table. I want to transfer it from folder to USB flash with some VBA code...Can anyone help
 
Since the drive letter can change for the USB drive (unless it has been set to a permanent drive letter in the drives management of a computer) you may want to give the ability to specify where it should go. You could have a default and then change it if necessary. I think there may be some code out there to find a flash drive and return the drive letter but I don't know where.

So, one way is to use:

Code:
Function SaveFileToUSB(strFilePathAndName As String, strDriveLetterToUse As String, Optional strNewPath As String)
Dim strNewName As String
Dim strFileName As String
 
strFileName = Right(strFilePathAndName, Len(strFilePathAndName) - InstrRev(strFilePathAndName, "\", True, vbTextCompare))
 
If strNewPath <> vbNullString Then
     strNewName = strNewPath & "\" & strFileName
Else
      strNewName = strDriveLetterToUse & "\" & strFileName
End If
 
Name strFilePathAndName strNewName
 
End Function

Not tested but should work. You would pass the parameters

Call SaveFileToUSB("C:\Temp\MyFile.txt", "F:")

or

Call SaveFileToUSB("C:\Temp\MyFile.txt", "F:", "F:\SomeFolder")
 
If i recall, DCrake posted a sample that can identify a flash drive letter.

N
 

Users who are viewing this thread

Back
Top Bottom