PDA

View Full Version : Help needed with LuaCom & VBscript!



blubberhoofd
December 11th, 2006, 12:20 PM
hi,

I've used this method to search for files in a folder
Path = "D:\\incoming2\\"

fso = luacom.CreateObject("Scripting.FileSystemObject")
VideoFolder = fso:GetFolder(Path)
FileList = luacom.GetEnumerator(VideoFolder.Files)
CurrentFile = FileList:Next()

while CurrentFile do
print(CurrentFile.Name)
CurrentFile = FileList:Next()
end

fso = nil
collectgarbage()this finds all files within the folder but not in the subfolders. I'm now trying to search within subfolders aswell. I've already found a VBscript example to do this, but I haven't got a clue how to make it work with LuaCom
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripts"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub

hope someone can help ;)

Promixis
December 11th, 2006, 02:02 PM
you are better off using the file functions in the win library. Examples are in the lwinfunc GML

blubberhoofd
December 11th, 2006, 02:36 PM
hi Mike,

I've looked through the examples but can't find an example that allows to search through the subfolders of a given folder.

correct me if I'm wrong ;)

Promixis
December 11th, 2006, 03:27 PM
use the Files attribute... see the manual :D

blubberhoofd
December 11th, 2006, 10:34 PM
Hi Mike,

thanks for the hint...

I've got a second and third level search going now.
will play around with it some more
Path = "D:\\incoming2"

incoming2 = {}
for fa in win.Files (Path..'\\*.*') do
FilePath = Path..'\\'..fa.FileName
if math.band (fa.FileAttributes, win.FILE_ATTRIBUTE_DIRECTORY) == 0 then
table.insert(incoming2,FilePath)
else
if string.len(fa.FileName) >=3 then
for fb in win.Files (Path..'\\'..fa.FileName..'\\*.*') do
FilePath = Path..'\\'..fa.FileName..'\\'..fb.FileName
if math.band (fb.FileAttributes, win.FILE_ATTRIBUTE_DIRECTORY) == 0 then
table.insert(incoming2,FilePath)
else
if string.len(fb.FileName) >=3 then
for fc in win.Files (Path..'\\'..fa.FileName..'\\'..fb.FileName..'\\*. *') do
FilePath = Path..'\\'..fa.FileName..'\\'..fb.FileName..'\\'.. fc.FileName
if math.band (fc.FileAttributes, win.FILE_ATTRIBUTE_DIRECTORY) == 0 then
table.insert(incoming2,FilePath)
end
end
end

end
end

end
end

end

print('resulting table= ')
table.foreachi(incoming2,print)

harleydude
December 12th, 2006, 04:18 AM
Here is the script I use to backup folders in the Girder tree. It will search down into subfolders as well.


--[[
Girder Backup Script

Use the Locations table below to indicate which folders in the girder folder
to backup.

TODO:
Create backups of other locations
Save backups to other locations
Send backups to optional FTP sites
Convert to a full blown LUA object
--]]
local Locations = { "luascript", "plugins", "GML"}
local MyZip = nil
local fs = nil

local BackupDir = win.GetDirectory('GIRDERDIR').."\\Backups"
if not win.PathExists (BackupDir) then
win.CreateDirectory (BackupDir)
end

local d = date.now()
local OutFile = BackupDir..'\\Backup_'..d.Month..'_'..d.Day..'_'.. d.Year..'.zip'

local function AddFiles(id, location)
local loc = win.GetDirectory('GIRDERDIR').."\\"..location
print (loc)
local dir = fs:GetFolder(loc)
local folders = luacom.GetEnumerator(dir.SubFolders)
local folder = folders:Next()
while folder do
AddFiles(id, location..'\\'..folder.Name)
folder = folders:Next()
end
local files = luacom.GetEnumerator(dir.Files)
local file = files:Next()
while file do
--print ('Adding files for location: '..loc..'\\'..file.Name)
--print ('location',location)
--print ('file.Name', file.Name)
--print ('loc', loc)
res, err, msg = MyZip:AddFile(location..'\\'..file.Name, loc..'\\'..file.Name)
file = files:Next()
end
end

local function BackerUp()
print ('Starting Backup')
fs = luacom.CreateObject("Scripting.FileSystemObject")
--print ('OutFile', OutFile)
MyZip = zip.Open(OutFile, zip.CREATE , zip.FILE)
if MyZip then
table.foreach(Locations, AddFiles)
else
print ('MyZip error')
end
MyZip:Close()
fs = nil
collectgarbage()
print ('Backup Complete')
end

BackerUp()

The AddFile() routine does most of the work.

Rick

blubberhoofd
December 13th, 2006, 04:48 AM
@Harleydude

thanx, I'll try to extrapolate the code I need.

harleydude
December 13th, 2006, 08:42 AM
If you need help let me know.

blubberhoofd
February 13th, 2007, 03:53 PM
Almost forgot about this....

@harleydude:


HELP!


;)

harleydude
February 14th, 2007, 04:21 AM
Fire away...

blubberhoofd
February 14th, 2007, 10:45 AM
thanx harleydude,

I got this far:
local Path = "D:\\incoming2"
print ('Getting files in '..Path)
local fs = nil
fs = luacom.CreateObject("Scripting.FileSystemObject")

local function GetFiles()
local dir = fs:GetFolder(Path)
local folders = luacom.GetEnumerator(dir.SubFolders)
local folder = folders:Next()
while folder do
GetFiles(id, Path..'\\'..folder.Name)
folder = folders:Next()
end
local files = luacom.GetEnumerator(dir.Files)
local file = files:Next()
while file do
res, err, msg = print(Path..'\\'..file.Name, loc..'\\'..file.Name)
file = files:Next()
end
end



GetFiles()
print ('List of files complete')
local fs = nil
collectgarbage()

needless to say it doesn't work, so what's wrong?

harleydude
February 14th, 2007, 05:10 PM
Try this


local Path = "D:\\incoming"
print ('Getting files in '..Path)
local fs = nil
fs = luacom.CreateObject("Scripting.FileSystemObject")

local function GetFiles(sPath)
local dir = fs:GetFolder(sPath)
local folders = luacom.GetEnumerator(dir.SubFolders)
local folder = folders:Next()
while folder do
GetFiles(sPath..'\\'..folder.Name)
folder = folders:Next()
end
local files = luacom.GetEnumerator(dir.Files)
local file = files:Next()
while file do
res, err, msg = print(sPath..'\\'..file.Name)
file = files:Next()
end
end



GetFiles(Path)
print ('List of files complete')
local fs = nil
collectgarbage()

You assigned variable Path as a global to the module, so it was really recursing on Path each time eventually causing a stack overflow. I changed Path to sPath in the GetFiles() routine and passed Path into GetFiles() torwards to bottom.

This code will do fine as long as you do not need to run inside a thread. LuaCOM and threads do not work well together. If you need to run in a thread then I can post similar code using the built in file routines.

blubberhoofd
February 14th, 2007, 09:26 PM
Thanx Harleydude,

I knew I was close...., but I still consider myself to be a newbie when it comes to using functions.


This code will do fine as long as you do not need to run inside a thread. LuaCOM and threads do not work well together. If you need to run in a thread then I can post similar code using the built in file routines.

please explain....or show me the way to some theory about this ;)

harleydude
February 15th, 2007, 04:10 AM
When the script runs, as is, it will run in the main Girder thread. If the operation takes to long the script could block other events/scripts from triggering until it is complete. So putting the code into a new thread will allow it to run without blocking other events/scripts. I will look at how I redid my code to use the girder file routines and post it when I get back home. It will allow you to run it in threads.

blubberhoofd
February 15th, 2007, 11:53 AM
@ Harleydude

Thanx, sounds like I could make my scripts run a lot smoother together. I will study some Lua documentation in the mean time so I can get a better understanding of the underlying concepts.

gugi
May 6th, 2007, 03:41 PM
Is there any way to execute VBscript (or any DOS commands) from withing Luascript? Thanks.

gugi
May 6th, 2007, 04:08 PM
Take a look here http://femm.foster-miller.net/list/msg01627.html