Login | |
|
 |
RE: Saving ftp directory listing to databse - 10/10/2006 5:38:51 AM
|
|
 |
|
| |
caryma77
Posts: 27
Score: 0
Joined: 9/19/2006
Status: offline
|
I am not using the Set oX = CreateObject( "Internet.communication" ) command at all. I saved the code with a vbs extension and run it by just clicking it for now. The code is below: ' Consts Const adClipString = 2 ' 00000002 Const SW_SHOWMINNOACTIVE = 7 ' Specs Dim sFCFSpec : sFCFSpec = "C:\ftpdir.txt" Dim sLGFSpec : sLGFSpec = "C:\ftpdir.log" Dim sDBFSpec : sDBFSpec = "C:\ftpdir.mdb" Dim sServer : sServer = "10.85.20.85" Dim sUser : sUser = "username" Dim sPWord : sPWord = "password" Dim oRE : Set oRE = New RegExp oRE.Pattern = "^d.+?(\w+)\s*$" oRE.Global = True oRE.MultiLine = True Dim sWhat : sWhat = "DFWCU" ' create DB : FTP get : write table : compare : update ' KISS way to specify steps during development/testing ' UCase: do it | LCase: skip ' need a FSO for pathes and IO Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) sFCFSpec = oFS.GetAbsolutePathName( sFCFSpec ) sLGFSpec = oFS.GetAbsolutePathName( sLGFSpec ) sDBFSpec = oFS.GetAbsolutePathName( sDBFSpec ) ' Connection for DB work Dim sCS : sCS = "Provider=" + "Microsoft.Jet.OLEDB.4.0" + ";" _ + "Data Source=" + sDBFSpec + ";" Dim oCNCT : Set oCNCT = CreateObject( "ADODB.Connection" ) Dim oTS Dim sSQL If "D" = Mid( sWhat, 1, 1 ) Then ' create DB and tables If oFS.FileExists( sDBFSpec ) Then oFS.DeleteFile sDBFSpec CreateObject( "ADOX.Catalog" ).Create sCS oCNCT.Open sCS oCNCT.Execute "CREATE TABLE FtpDir0 ( sDir VARCHAR( 250 ) CONSTRAINT pmk PRIMARY KEY )" oCNCT.Execute "CREATE TABLE FtpDir1 ( sDir VARCHAR( 250 ) CONSTRAINT pmk PRIMARY KEY )" Else oCNCT.Open sCS End If If "F" = Mid( sWhat, 2, 1 ) Then ' get first level dir listing from ftp server Dim sFTPCmd : sFTPCmd = "open " + sServer + vbCrLf _ + sUser + vbCrLf _ + sPWord + vbCrLf _ + "ls -l" + vbCrLf _ + "bye" Set oTS = oFS.CreateTextFile( sFCFSpec ) oTS.WriteLine sFTPCmd oTS.Close sFTPCmd = "%comspec% /c ftp -i -s: " + sFCFSpec + " > """ + sLGFSpec + """" WSCript.Echo sFTPCmd If oFS.FileExists( sLGFSpec ) Then oFS.DeleteFile sLGFSpec CreateObject( "WScript.Shell" ).Run sFTPCmd, SW_SHOWMINNOACTIVE, True End If If "W" = Mid( sWhat, 3, 1 ) Then ' parse log and write to DB Dim oMTS : Set oMTS = oRE.Execute( oFS.OpenTextFile( sLGFSpec ).ReadAll ) Dim oMT For Each oMT In oMTS WScript.Echo oMT.SubMatches( 0 ) oCNCT.Execute "INSERT INTO FtpDir1 (sDir ) VALUES ( '" + oMT.SubMatches( 0 ) + "' )" Next End If If "C" = Mid( sWhat, 4, 1 ) Then ' compare FtpDir0 (master) with FtpDir1 (current) Dim oRS WScript.Echo "same directories" sSQL = " Select C.sDir " + vbCrLf _ + " FROM FtpDir1 AS C " + vbCrLf _ + " INNER JOIN FtpDir0 AS M " + vbCrLf _ + " ON C.sDir = M.sDir " WScript.Echo sSQL Set oRS = oCNCT.Execute( sSQL ) If oRS.EOF Then WScript.Echo "empty recordset", vbCrLf Else WScript.Echo oRS.GetString( adClipString, , vbTab, vbCrLf, "NULL" ) End If WScript.Echo "new directories" sSQL = " Select C.sDir " + vbCrLf _ + " FROM FtpDir1 AS C " + vbCrLf _ + " LEFT JOIN FtpDir0 AS M " + vbCrLf _ + " ON C.sDir = M.sDir " + vbCrLf _ + " WHERE M.sDir IS NULL " WScript.Echo sSQL Set oRS = oCNCT.Execute( sSQL ) If oRS.EOF Then WScript.Echo "empty recordset", vbCrLf Else WScript.Echo oRS.GetString( adClipString, , vbTab, vbCrLf, "NULL" ) End If WScript.Echo "deleted directories" sSQL = " Select M.sDir " + vbCrLf _ + " FROM FtpDir0 AS M " + vbCrLf _ + " LEFT JOIN FtpDir1 AS C " + vbCrLf _ + " ON M.sDir = C.sDir " + vbCrLf _ + " WHERE C.sDir IS NULL " WScript.Echo sSQL Set oRS = oCNCT.Execute( sSQL ) If oRS.EOF Then WScript.Echo "empty recordset", vbCrLf Else WScript.Echo oRS.GetString( adClipString, , vbTab, vbCrLf, "NULL" ) End If End If If "U" = Mid( sWhat, 5, 1 ) Then ' update FtpDir0 (master) from FtpDir1 (current) sSQL = "DELETE FROM FtpDir0" oCNCT.Execute sSQL sSQL = "INSERT INTO FtpDir0 SELECT * FROM FtpDir1" oCNCT.Execute sSQL End If oCNCT.Close
|
|
| |
|
|
|
|
|