Results 1 to 3 of 3

Thread: files

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    London
    Posts
    678

    files

    I am using the code below to get all the files in a particular folder.
    There are two questions
    1) I would like to get the file names only and not the fullpath on each loop
    i.e text1.txt, text2.txt, ...
    whereas right now I am getting
    C:\Farshad\Net\VB\Files\text1.txt
    C:\Farshad\Net\VB\Files\text2.txt
    ...

    2) instead of using the path ("C:\Farshad\Net\VB\Files\") I would like to use the path on a web server such as
    "http://www.test1.com/images"
    can this be done?

    Thanks

    Dim d() As String

    d = System.IO.Directory.GetFiles("C:\Farshad\Net\VB\Files\")

    Dim en As System.Collections.IEnumerator

    en = d.GetEnumerator

    While en.MoveNext
    Console.WriteLine(CStr(en.Current))
    End While

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    I can't help with question 2, but this is what I've done in the past for question 1.


    The first line strips off the path, the second line strips off the extension.

    VB Code:
    1. strFormatFileName = strFile.Substring(InStrRev(strFile, "\"))
    2. strFormatFileName = strFormatFileName.Substring(0, InStr(strFormatFileName, ".") - 1)
    To tie this up with your code, strFile is (for example) d(1) and strFormatFileName is what you want to output.



    Oh, just done some more research, you may be able to do it much more elegantly by using
    VB Code:
    1. Path.GetFileName(CStr(en.Current))
    This world is not my home. I'm just passing through.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    1st Question : Here is elegant way .It gets only file name in particular folder .

    VB Code:
    1. Dim dir As New IO.DirectoryInfo("c:\windows")
    2.         Dim fils() As IO.FileInfo = dir.GetFiles()
    3.  
    4.         Dim i As Integer
    5.  
    6.         For i = 0 To fils.Length - 1
    7.             ListBox1.Items.Add(fils(i).Name)
    8.         Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width