loop in a folder

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
abenitez77
Posts: 8
Last visit: Fri Dec 23, 2011 2:07 am

loop in a folder

Post by abenitez77 »

I want to loop in a folder and subfolder to run a compact database script below. How do i do the loop? Below is the hardcoded script. I want to loop and select all *.accdb files and run the script below on them.

Dim objFSO Dim objEngine Dim strLckFile Dim strSrcName Dim strDstName Dim strPassword

strLckFile = "C:Accesswebforumsfoo.laccdb" strSrcName = "C:Accesswebforumsfoo.accdb" strDstName = "C:Accesswebforumscompacted.accdb" strBackup = "C:Accesswebforumsfoobackup.accdb" strPassword = "foo"
Set objEngine = CreateObject("DAO.DBEngine.120")
Set objFSO = CreateObject("Scripting.FileSystemObject") If Not (objFSO.FileExists(strLckFile)) Then If (objFSO.FileExists(strBackup)) Then objFSO.DeleteFile strBackup End If If (objFSO.FileExists(strDstName)) Then objFSO.DeleteFile strDstName End If objFSO.CopyFile strSrcName, strBackup ''dbVersion120 = 128 objEngine.CompactDatabase strSrcName, strDstName, , 128, ";pwd=" & strPassword objFSO.DeleteFile strSrcName objFSO.MoveFile strDstName, strSrcName
End If 'LckFile
User avatar
abenitez77
Posts: 8
Last visit: Fri Dec 23, 2011 2:07 am

loop in a folder

Post by abenitez77 »

I want to loop in a folder and subfolder to run a compact database script below. How do i do the loop? Below is the hardcoded script. I want to loop and select all *.accdb files and run the script below on them.

Dim objFSO Dim objEngine Dim strLckFile Dim strSrcName Dim strDstName Dim strPassword

strLckFile = "C:Accesswebforumsfoo.laccdb" strSrcName = "C:Accesswebforumsfoo.accdb" strDstName = "C:Accesswebforumscompacted.accdb" strBackup = "C:Accesswebforumsfoobackup.accdb" strPassword = "foo"
Set objEngine = CreateObject("DAO.DBEngine.120")
Set objFSO = CreateObject("Scripting.FileSystemObject") If Not (objFSO.FileExists(strLckFile)) Then If (objFSO.FileExists(strBackup)) Then objFSO.DeleteFile strBackup End If If (objFSO.FileExists(strDstName)) Then objFSO.DeleteFile strDstName End If objFSO.CopyFile strSrcName, strBackup ''dbVersion120 = 128 objEngine.CompactDatabase strSrcName, strDstName, , 128, ";pwd=" & strPassword objFSO.DeleteFile strSrcName objFSO.MoveFile strDstName, strSrcName
End If 'LckFile
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

loop in a folder

Post by jvierra »

Hi. Here is onform of looping statement in VBscript.

Do [{While | Until} condition] [statements] [Exit Do] [statements]Loop

Another useful method is:
For Each element In group
[statements]
[Exit For]
[statements]
Next [element]And of course you can use a while loop.While condition
[statements]
Wend
This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked