Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   List All Files in Folder using VBScript (http://www.go4expert.com/articles/list-files-folder-using-vbscript-t927/)

bettlejus 12Mar2010 13:26

Re: List All Files in Folder using VBScript
 
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

andorosfire 19Mar2010 21:51

Re: List All Files in Folder using VBScript
 
Does anyone know how to perform this same task on a Mac running OS X.5?

donkey 3Apr2010 15:33

Re: List All Files in Folder using VBScript
 
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))

Jeff Greener 19Jul2010 20:04

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by DivinationX (Post 60185)
How do i code such that, the filename that is captures will be stored into an array of strings? Thanks!

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.

Kiru 16Aug2010 11:00

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by chmod777 (Post 57911)
I have tried this in both Windows Vista, and again on XP (thinking that maybe Vista had issues), and I cannot get this to read the folders. It just creates an empty 'filelist.txt' file. I have not modified this script in any way, and have tried diff. folders. Any suggestions from anyone?

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.

edytharceo 3Sep2010 13:36

Re: List All Files in Folder using VBScript
 
This site is really great, i am glad that there is a site, thread like this. The information here is very helpful.

Bacaryu 28Sep2010 18:08

Re: List All Files in Folder using VBScript
 
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.

sasaa86 15Jan2011 16:44

Re: List All Files in Folder using VBScript
 
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")

Impala 20Jan2011 19:15

Re: List All Files in Folder using VBScript
 
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

codvb 8Jul2011 09:13

Re: List All Files in Folder using VBScript
 
Quote:

Originally Posted by SaswatPadhi (Post 57913)
Hi,

Thanx for reporting. :)

There was a typo at this line :Set objColFolders = objFolder.Folders
It should read :Set objColFolders = objFolder.SubFolders.
...

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)

'MY ADAPTATION START HERE
Set objColFolders = objFolder.SubFolders
:AllFolders
For Each tfolder In objColFolders
    objNewFile.WriteLine(tfolder.Name)
    objColFolders = tfolder.SubFolders
    GoTo AllFolders

Next
'END HERE

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



All times are GMT +5.5. The time now is 06:04.