opening new files in a folder

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
radio_man
Newbie
Posts: 3
Joined: Fri Jun 20, 2008 12:11 pm

opening new files in a folder

Post by radio_man » Fri Jun 20, 2008 1:37 pm

Hi,
Really new to scripting and jumping in head first. I'm sure I'll be nearly swimming after this though. I'm trying to create a macro that will detect when a file is created in a folder open it in MS access making some version conversions along the way and then export to a text file.
At first I thought I could just record it and then realized there were some variables involved and I'm not sure where to go from here.

Thanks

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Jul 02, 2008 8:17 am

To do this I would make a macro which gets the latest file in the folder. See:
http://www.mjtnet.com/forum/viewtopic.php?p=8011

This gets the latest file. Add your code to it to open it in Access:

Run>c:\....\msaccess.exe %filename%

Now under Macro Properties/Trigger select "Folder Event" and "New File in Folder" and specify the folder path.

Now when a new file arrives in the folder, the macro is triggered, gets the latest file (therefore the new file) and opens it in Access.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

radio_man
Newbie
Posts: 3
Joined: Fri Jun 20, 2008 12:11 pm

Post by radio_man » Wed Jul 02, 2008 12:10 pm

So I've set this up as follows:

VBSTART
Function NewestFile(\\sheila2\c\bsi32\logs)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(\\sheila2\c\bsi32\logs)
dPrevDate = "0"
For Each oFile In oFolder.Files
If DateDiff("s", dPrevDate, oFile.DateLastModified) > 0 Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
End If
Next
NewestFile = sNewestFile
End Function
VBEND
VBEval>NewestFile("\\sheila2\c\bsi32\logs"),filename

Run>C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE %filename%

I'm getting a couple of errors, the first is:
Microsoft VBScript compilation error: 1010
Expected identifier
Line 2, Column 20

and, the second:

Microsoft VBScript runtime error:13
Type mismatch: 'NewestFile'
Line 2, Column 0

This is all really new to me so if there was something that I was supposed to know to change I apologize, I didn't/don't. Thanks so much for your help on this and if there is someplace I can go to enhance a better understaing of writing scripts I'd gladly receive the input.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Jul 02, 2008 12:16 pm

You didn't need to change the VBScript code. This is what your script should look like:

Code: Select all

VBSTART
Function NewestFile(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oFile In oFolder.Files
  If DateDiff("s", dPrevDate, oFile.DateLastModified) > 0 Then
    sNewestFile = oFile.Path
    dPrevDate = oFile.DateLastModified
  End If
Next
NewestFile = sNewestFile
End Function
VBEND

VBEval>NewestFile("\\sheila2\c\bsi32\logs"),filename

Run>"C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE" "%filename%"
This will open the newest file in the folder in Access. Of course as it stands it does not care whether this is a mdb file or something else. But assuming you are only putting mdb files in this folder then it needs no modification. Just set it up on a trigger to activate when a new file appears in the folder.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

radio_man
Newbie
Posts: 3
Joined: Fri Jun 20, 2008 12:11 pm

Post by radio_man » Wed Jul 02, 2008 1:25 pm

oh. ok.
Thanks.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts