Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Names

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I have a folder and I am copying files from one folder to another.

I need a way to get JUST the filename and extension and not the path.

I already have the path as a separate variable.

I want to use FileCopy.

Any ideas?

Thanks
 
if you already have the path ie c:\myfile.txt stored in a variable then you can try this:

To get the file name, myfile.txt:
dim path as string 'this holds "c:\myfile.txt"
dim temp() as string 'used to hold the pieces of the path
temp = split(path,"\") 'the split function returns a zero based array of strings

dim fileName as string
fileName = temp(ubound(temp)) 'to retrieve the last item in the array which is the filename.

'now if you want the extension and the name of the file in seperate variables:
dim ext as string
dim fName as string 'holds the name of the file minus the extension

temp = split(fileName,".")
fname = temp(0) 'the name of the file
ext = temp(1) 'the extension


Good luck


Troy Williams B.Eng.
fenris@hotmail.com

 
try this ........ ::)

Code:
function getfilename(byval s as string) as string

  dim ch     as string
  dim i      as long
  dim output as string


  for i = len(s) to 1 step -1
    ch = mid$(s,i,1)
    if ch <> &quot;\&quot; then
      output = ch & output
    else
      exit for
    end if
  next

  getfilename = output
end function

bluenote@uyuyuy.com
(excuse my english)
 
I have decided to break down and use the FileSystem Object's GetFileName.

How would I use that for EACH file in the folder?

If the path is C:\second and there are several files in the folder, I want to return a filename for each of the files.

Thanks again
 
The Form1 contains a commandbox(cmdScan) and a listbox(lstFileNames). Click the commandbutton and all files (even in subfolders) will be listed.

Const c_strFolderName As String = &quot;C:\Second&quot;

Private Sub cmdScan_Click()
ListFileNames
End Sub

Private Sub ListFileNames()

Dim objFso As FileSystemObject
Dim objFolder As Folder

Set objFso = New FileSystemObject
Set objFolder = objFso.GetFolder(c_strFolderName)


'avoid flickering

Form1.lstFileNames.Visible = False

ScanFolders objFolder
Form1.lstFileNames.Visible = True

Set objFolder = Nothing
Set objFso = Nothing

End Sub

Private Sub ScanFolders(objFolder As Folder)

Dim objSubFolder As Folder
Dim objFile As File


'Folder may contain subfolders too

For Each objSubFolder In objFolder.SubFolders


'Scan for SubFolders in this (Sub-)Folder

ScanFolders objSubFolder

Next objSubFolder


'Scan for Files in this folder.

For Each objFile In objFolder.Files

Form1.lstFileNames.AddItem objFile.Name

Next objFile

End Sub


In real life you should replace the Const with a DialogBox to allow for flexibility and the choosen foldername should be passed as a parameter to the ListFileNames Subroutine _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top