List All Files in Folder using VBScript

Discussion in 'Visual Basic [VB]' started by pradeep, Jun 24, 2006.

  1. bettlejus

    bettlejus Banned

    Joined:
    Mar 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I have the following code for sending a mail with Outlook, my problem is that I need to sedn this mail 4 times a day with a diferent file, sometimes with 2 files in attach, and I am modifying this everytime with the name of the file, not much help there

    I was wondering how to tell the script to look for files in a folder and attach everything it finds to the mail

    Can anyone help me?
    Thanks!


    Dim objOutlk
    Dim objMail
    Dim strMsg
    Const olMailItem = 0
    Set objOutlk = createobject("Outlook.Application")
    Set objMail = objOutlk.createitem(olMailItem)
    objMail.To = "test@company.com"
    objMail.cc = ""
    objMail.subject = "Subject "
    strMsg = "Hello,"
    objMail.Attachments.Add(c:\folder\file.txt)
    objMail.body = strMsg
    objMail.send
    Set objMail = nothing
    Set objOutlk = nothing
     
  2. andorosfire

    andorosfire New Member

    Joined:
    Mar 19, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Does anyone know how to perform this same task on a Mac running OS X.5?
     
  3. donkey

    donkey New Member

    Joined:
    Apr 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    switzerland
    hi pradeep
    this is a perfect useful script exmple to me, if is it possible to import any reg files in a folder like regedit /i some_files.reg instead of create filelist.txt, that I looking for, I'm very pleased do you would help me :)
    cheers
    ((donkey))
     
  4. Jeff Greener

    Jeff Greener New Member

    Joined:
    Jul 16, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    You need to declare an undimensioned array, and then initialize it at the top:
    Dim myresults()
    ReDim myresults(0)

    then replace the output portion:
    objNewFile.WriteLine(tfolder.Name)
    with these two lines:

    myresults(ubound(myresults))=tfolder.Name
    redim Preserve Myresults(ubound(Myresults)+1)

    This will leave you with an array full of the results and one empty slot at the end.
     
  5. Kiru

    Kiru New Member

    Joined:
    Aug 16, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi I am Kiru. i went through these codes. Excellent.

    However, i have a situation here and i would need your help in this regard.

    I have a folder and i have so many sub folders. i getting the folder names in an excel file based on the last modified date.

    This excel file gets updated every hour.

    so, now, i want to check the current date with the last modified date.
    if yes, then i have to check for the name of the folder (which is usually employees name).

    I have a data base in excel file where i have fed employees name and his email address.

    if a last modified date of a folder indicates a current date, i need to shoot an email to that particular person based on the database cells.

    i know it is possible to write a scirpt in excel but i am not familiar with the codes.

    can you help me.
     
  6. edytharceo

    edytharceo New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This site is really great, i am glad that there is a site, thread like this. The information here is very helpful.
     
  7. Bacaryu

    Bacaryu New Member

    Joined:
    Sep 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I'm new to vb scripting

    I need something like this code but with addition of text to the txt output file.

    so the script should check for every file in the folder and give an output of :

    RESTORE LOG NewDatabase
    FROM DISK = ''D:\BackupFiles\TestDatabase_TransactionLogBackup1.trn'
    WITH NORECOVERY

    RESTORE LOG NewDatabase
    FROM DISK = ''D:\BackupFiles\TestDatabase_TransactionLogBackup2.trn'
    WITH NORECOVERY

    Can somebody help me with that.

    I don't think it should be hard to do, I only don't know how exactly.
     
  8. sasaa86

    sasaa86 New Member

    Joined:
    Jan 15, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    is there a way to only save the file name and not the extension

    I use it to list my mp3's

    (there all the same type of files ".mp3")
     
  9. Impala

    Impala New Member

    Joined:
    Jan 20, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I get a list of folders and subfolders in the target folder.
    What I like is this scrip that displays all files in the target folder and all the sub and sub sub folders also in one file output.

    Thanks Roald
     
  10. codvb

    codvb New Member

    Joined:
    Jul 8, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I adapted the code for the list folder and all sub-folders but the error below appears when I run the file:

    Line: 26
    Char: 5
    Error: Expected statement
    Code: 800A0400
    Source: Microsoft VBScript compilation error


    I need to list the all filenames in each folder too but, I'm beginner in VBScript! Can you help me? What I have to do?

    Adapted Code:
    Code:
    On Error Resume Next
    
    Const WINDOW_HANDLE = 0
    Const BIF_EDITBOX = &H10
    Const BIF_NONEWFOLDER = &H0200
    Const BIF_RETURNONLYFSDIRS = &H1
    
    Set objShell = CreateObject("Shell.Application")
    Set wshShell = CreateObject("WScript.Shell")
    Set objFSO   = CreateObject("Scripting.FileSystemObject")
    
    strPrompt     = "Please select the folder to process."
    intOptions    = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
    strTargetPath = wshShell.SpecialFolders("MyComputer")
    strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)
    
    Set objNewFile = objFSO.CreateTextFile(strFolderPath & "\ListaMP3.txt", True)
    Set objFolder  = objFSO.GetFolder(strFolderPath)
    
    [B]'MY ADAPTATION START HERE[/B]
    Set objColFolders = objFolder.SubFolders
    [I]:AllFolders[/I]
    For Each tfolder In objColFolders
        objNewFile.WriteLine(tfolder.Name)
    [I]    objColFolders = tfolder.SubFolders
        GoTo AllFolders[/I]
    Next
    [B]'END HERE[/B]
    
    objNewFile.Close
    
    Function Browse4Folder(strPrompt, intOptions, strRoot)
        Dim objFolder, objFolderItem
    
        On Error Resume Next
    
        Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
        If (objFolder Is Nothing) Then
          Wscript.Quit
        End If
        Set objFolderItem = objFolder.Self
        Browse4Folder = objFolderItem.Path
        Set objFolderItem = Nothing
        Set objFolder = Nothing
    End Function
    
     
  11. kaiwen21

    kaiwen21 New Member

    Joined:
    Jul 30, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Awesome script thanks

    Is it possible however to split the filename into different columns in an excel table.

    for e.g. the file is "001 - his name is known.mkv"

    and want to split it as follows:
    Column A: 001
    Column B: his name is known

    and drop the file extension from the file list
     
  12. AutosysR11

    AutosysR11 New Member

    Joined:
    Apr 10, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I am beginner in vbscript. Can someone help me to resolve my problem. I was trying to export all files in C:\windows with the file extension of .log and will list the file to Filelist.txt under the same path (c:\windows).
     
  13. pramodmhn

    pramodmhn New Member

    Joined:
    Oct 15, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I will do this way:
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set Folder = objFSO.GetFolder(".")
    Set NewFile = objFSO.CreateTextFile(Folder&"\FileList.txt", True)
    For Each File In Folder.Files
    'sNewFile = File.Name'including extension
    NewFile.WriteLine(left(File.Name,(len(File.Name)-4)))'exclude extenstion
    Next
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice