mibuso.com

Microsoft Business Solutions online community
It is currently Fri Aug 22, 2014 9:37 pm

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [Solved] Windows Script Host Object Model Folders and Files
PostPosted: Tue Sep 27, 2005 2:57 pm 
Offline
Moderator
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Wed Jul 02, 2003 10:13 am
Posts: 7686
Location: Milan
Country: Italy (it)
For once I am not resolving a problem, but posting one.
I need to scan a subdirectory for files and subdirectories. In general virtual table "File" works, but now the directory-structure is so deep that the filter on field "Path" generates an error (value in the filter .... is too long for the field type).
So I am trying Automation.
These are the variables I declared:
Code: Select all
Lint   Integer      
LautFileSystemObject   :'Windows Script Host Object Model'.FileSystemObject   
LautFolder   :'Windows Script Host Object Model'.Folder   
LautFolder2   :'Windows Script Host Object Model'.Folder   
LautFolders   :'Windows Script Host Object Model'.Folders   


and this is the code:
Code: Select all
CREATE(LautFileSystemObject);
LautFolder :=  LautFileSystemObject.GetFolder('c:\temp');
LautFolders := LautFolder.SubFolders();

FOR Lint := 1 TO LautFolders.Count() DO BEGIN
  LautFolder2 := LautFolders.Item(Lint); // This goes wrong
END;

CLEAR(LautFileSystemObject);

When I run this peace of code I get the error:"An exception occurred from an external component. The component did not provide the exception description".
What am I doning wrong?

_________________
Regards,Alain Krikilion
Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title! || Read Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2014 - 20 & 21 November 2014 ««« Visit the conference website: http://www.navtechdays.com


Last edited by kriki on Sat Oct 22, 2005 5:40 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 7:12 am 
Offline
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Thu Oct 16, 2003 8:50 am
Posts: 12439
Location: Brno
Country: Czech Republic (cz)
From documentation of the Subfolders class:

Code: Select all
[VBScript]
Function ItemDemo
   Dim d   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Athens"   ' Add some keys and items.
   d.Add "b", "Belgrade"
   d.Add "c", "Cairo"
   ItemDemo = d.Item("c")   ' Get the item.
End Function


It seems, that the index is not no. but may be some other key...

_________________
Kamil Sacek
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.


Top
 Profile E-mail WWW  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 1:47 pm 
Offline
Moderator
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Wed Jul 02, 2003 10:13 am
Posts: 7686
Location: Milan
Country: Italy (it)
I also found this documentation, but I fail to see how I can use it to solve my problem.

_________________
Regards,Alain Krikilion
Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title! || Read Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2014 - 20 & 21 November 2014 ««« Visit the conference website: http://www.navtechdays.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 2:36 pm 
Offline
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Thu Oct 16, 2003 8:50 am
Posts: 12439
Location: Brno
Country: Czech Republic (cz)
I am afraid that without iterating functions you will be not able to do this in Navision in this way... (or you need to search for keys which are associated as default with the folders)

_________________
Kamil Sacek
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.


Top
 Profile E-mail WWW  
 
 Post subject: Why iterate using automation???
PostPosted: Fri Oct 14, 2005 11:48 am 
Offline

Joined: Thu Sep 29, 2005 7:49 pm
Posts: 39
Location: The Netherlands
Country: Netherlands (nl)
Hi Kine,

I suppose your trying to get a list of folders using Navision.

If I were you I would not use Windows Script Host Object Model. Navision has the table file that you can use to walk through Files & Folders.

Here's an example:
Code: Select all
Variables:
Name   DataType   Subtype   Length
FileRec   Record   File   

Code:
FileRec.SETRANGE(Path, 'C:\Temp');
FileRec.SETRANGE("Is a file", False);           // TRUE for files, FALSE for folders...
IF FileRec.FIND('-') THEN
  REPEAT
    if not (FileRec.Name IN ['.', '..']) then   // Skip current and parent folder...
      MESSAGE(FileRec.Name);
  UNTIL FileRec.NEXT = 0;


I think this will be a easier way to accomplish the same. Hope this is some help! :wink:

_________________
Gerard Robbertsen

http://www.codegenius.nl


Top
 Profile  
 
 Post subject: Re: Why iterate using automation???
PostPosted: Fri Oct 14, 2005 12:02 pm 
Offline
Moderator
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Wed Jul 02, 2003 10:13 am
Posts: 7686
Location: Milan
Country: Italy (it)
Hi CodeGenius,

I think you are suffering the Friday-afternoon-disease. :wink:

I know very well this table. Problem is that it doesn't work if the path goes to deep. So I am forced to find another solution.
I tried that automation, but I got stuck on retrieving all the subdirs and files in a subdir.

_________________
Regards,Alain Krikilion
Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title! || Read Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2014 - 20 & 21 November 2014 ««« Visit the conference website: http://www.navtechdays.com


Top
 Profile  
 
 Post subject: Re: Why iterate using automation???
PostPosted: Fri Oct 14, 2005 12:39 pm 
Offline

Joined: Thu Sep 29, 2005 7:49 pm
Posts: 39
Location: The Netherlands
Country: Netherlands (nl)
kriki wrote:
I think you are suffering the Friday-afternoon-disease. :wink:


No Friday-afternoon-disease, just looking for an alternative for your problem. Collection types in WSH do not work very well with Navision. Why? I cannot tell you, I didn't make the IFolderCollection interface :wink:

I was just trying to be helpful... :)

_________________
Gerard Robbertsen

http://www.codegenius.nl


Top
 Profile  
 
 Post subject: Re: Why iterate using automation???
PostPosted: Fri Oct 14, 2005 12:43 pm 
Offline
Moderator
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Wed Jul 02, 2003 10:13 am
Posts: 7686
Location: Milan
Country: Italy (it)
CodeGenius wrote:
kriki wrote:
I think you are suffering the Friday-afternoon-disease. :wink:


No Friday-afternoon-disease, just looking for an alternative for your problem. Collection types in WSH do not work very well with Navision. Why? I cannot tell you, I didn't make the IFolderCollection interface :wink:

I was just trying to be helpful... :)

Thanx for the help.
But you should read the problem better. :wink: I wrote in it that I had problems with the File-table.

_________________
Regards,Alain Krikilion
Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title! || Read Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2014 - 20 & 21 November 2014 ««« Visit the conference website: http://www.navtechdays.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:09 pm 
Offline

Joined: Fri Oct 07, 2005 8:16 am
Posts: 14
Location: Bielsko
Country: Poland (pl)
In folders collection folders are indexed by name.
Use Microsoft Script Control:

Variables:
scr Automation 'Microsoft Script Control 1.0'.ScriptControl
txtCode Text 1024
RetVal Variant
count Integer
i Integer
txtCR Text 30
FolderName Text 1024

And code:

txtCR := ' '; txtCR[1] := 13;
create(scr);
scr.Language := 'VBScript';

//declare global collection
txtCode :=
'dim Folders' + txtCR;
scr.AddCode(txtCode);

//find folders and copy them to collection
txtCode :=
'sub LoadFolderList(folderspec)' + txtCR +
' Dim fso, f, f1, s, sf' + txtCR +
'' + txtCR +
' set Folders = CreateObject("Scripting.Dictionary")' + txtCR +
'' + txtCR +
' Set fso = CreateObject("Scripting.FileSystemObject")' + txtCR +
' Set f = fso.GetFolder(folderspec)' + txtCR +
' Set sf = f.SubFolders' + txtCR +
' For Each f1 in sf' + txtCR +
' Folders.Add (Folders.Count+1), f1' + txtCR +
' Next' + txtCR +
'End sub' + txtCR;
scr.AddCode(txtCode);

txtCode :=
'function GetCount' + txtCR +
' GetCount = Folders.Count' + txtCR +
'end function' + txtCR;
scr.AddCode(txtCode);

txtCode :=
'function GetFolder(i)' + txtCR +
' getFolder = Folders(i)' + txtCR +
'end function' + txtCR;
scr.AddCode(txtCode);

scr.ExecuteStatement('LoadFolderList "C:\temp\"');

RetVal := scr.Eval('GetCount');
count := RetVal;

for i:=1 to count do begin
RetVal := scr.Eval('GetFolder(' + format(i) + ')');
FolderName := RetVal;
message(FolderName);
end;

CLEAR(scr);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:16 pm 
Offline
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Thu Oct 16, 2003 8:50 am
Posts: 12439
Location: Brno
Country: Czech Republic (cz)
:shock: indexing something I want to know by the value I want to know... #-o

I know that in VB no problem... but... ](*,)

_________________
Kamil Sacek
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.


Top
 Profile E-mail WWW  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:36 pm 
Offline

Joined: Wed Jan 23, 2002 8:01 am
Posts: 166
Location: Norwell, MA
Country: United States (us)
azwierzchowski,

This is a very clever technique. Thanks for sharing it.


Top
 Profile E-mail  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:46 pm 
Offline

Joined: Mon Sep 19, 2005 11:58 am
Posts: 171
Location: Lucerne
Country: Switzerland (ch)
azwierzchowski wrote:
In folders collection folders are indexed by name.
Use Microsoft Script Control:
Variables:
scr Automation 'Microsoft Script Control 1.0'.ScriptControl
txtCode Text 1024
RetVal Variant
...


nice!
I used to write and run *.vbs files dynamically at runtime, but your code is in navision - that's cool 8)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 7:38 pm 
Offline

Joined: Fri Oct 07, 2005 8:16 am
Posts: 14
Location: Bielsko
Country: Poland (pl)
Another solution, now almost everything is done in Navision.

Script control allows to add Automation controls from your code, so there is another solution. This time VBScript is used only for copying folders objects from collection indexed by folder name to collection indexed by integer value.

Variables from oryginal code:

LautFileSystemObject Automation 'Windows Script Host Object Model'.FileSystemObject
LautFolder Automation 'Windows Script Host Object Model'.Folder
LautFolder2 Automation 'Windows Script Host Object Model'.Folder
LautFolders Automation 'Windows Script Host Object Model'.Folders
LInt Integer

My new variables:
FoldersCol Automation 'Microsoft Scripting Runtime'.Dictionary
txtCode Text 250
txtCR Text 30
scr Automation 'Microsoft Script Control 1.0'.ScriptControl

Code:

CREATE(LautFileSystemObject);
LautFolder := LautFileSystemObject.GetFolder('c:\temp');
LautFolders := LautFolder.SubFolders();
create(FoldersCol);

create(scr);
scr.Language := 'VBScript';
//variable LautFolders is visible in script as FoldersByName
scr.AddObject('FoldersByName', LautFolders);
//variable FoldersCol is visible in script as FoldersById
scr.AddObject('FoldersById', FoldersCol);

//copy objects using VBScript
txtCR := ' '; txtCR[1] := 13;
txtCode :=
'dim f1' + txtCR +
'For Each f1 in FoldersByName' + txtCR +
' FoldersById.Add (FoldersById.Count+1), f1' + txtCR +
'Next' + txtCR;
scr.ExecuteStatement(txtCode);

clear(scr);

//now we can use collection of folders in Navision
FOR Lint := 1 TO FoldersCol.Count() DO BEGIN
LautFolder2 := FoldersCol.Item(Lint);
message(LautFolder2.Name);
END;


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 5:39 pm 
Offline
Moderator
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Wed Jul 02, 2003 10:13 am
Posts: 7686
Location: Milan
Country: Italy (it)
Thanx azwierzchowski. :D :D :D :D
It works! (I have finally time to check it out. And I am still trying to understand it)

_________________
Regards,Alain Krikilion
Use the SEARCH,Luke! || No PM,please use the forum. || May the <SOLVED>-attribute be in your title! || Read Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2014 - 20 & 21 November 2014 ««« Visit the conference website: http://www.navtechdays.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 9:32 am 
Offline

Joined: Fri Feb 28, 2003 5:53 pm
Posts: 278
Location: Dordrecht
Country: Netherlands (nl)
Hey thanks for the vbscript example that is cool. I tried it once but didn't get any further because i thought navision was unable to handle some parameters; but so it seems to be possible =D>

_________________
In a world without Borders or Fences, who needs Windows and Gates?


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum


Search for:
Jump to: