Bonjour,

J'ai quasi terminé un script mais il me manque un tout petit truc à régler : ce truc s'appelle une foutue virgule... Je me prend la tête depuis 4h sans résultats

J'utilise un script allègrement inspiré de : http://blogs.msdn.com/b/gstemp/archi...10/212113.aspx

Le voici :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Dim Folder2crawl(1)
Folder2crawl(0)="C:\Nouveau dossier\TEST"
Folder2crawl(1)="C:\Nouveau dossier\TEST2"
strOutput = "C:\wamp\www\site1\index_output.json"
 
' create file object
Set oFSO = CreateObject("Scripting.FileSystemObject")
' create output file
Set Output = oFSO.CreateTextFile(strOutput, True)
 
Output.WriteLine("{""folder"": [") 'start json file
' main loop
For i = 0 to UBound(Folder2crawl)
	strFolder = Folder2crawl(i)
	Set oFolder = oFSO.GetFolder(strFolder)
	' start new folder
	Output.WriteLine("{""file"": [")
 
	Set colFiles = oFolder.Files
	For Each objFile In colFiles ' loop to write each file
	  Output.WriteLine("{""name"": """ & Replace(objFile.Path,"\","\\") & """,""lastmodified"": """ & objFile.DateLastModified & """},")
	Next
	ShowSubFolders(oFolder) ' calls function for recursivity support
 
	' end new folder
	If i = UBound(Folder2crawl) Then ' if last folder not write ","	at the end
		Output.WriteLine("]}")
	Else
		Output.WriteLine("]},")
	End if	
Next
' end main loop
Output.WriteLine("]}") ' end json file
 
' function for recursivity support
Sub ShowSubFolders(oFolder)
  Set colFolders = oFolder.SubFolders
  For Each objSubFolder In colFolders
    Set colFiles = objSubFolder.Files
    For Each objFile In colFiles
	  Output.WriteLine("{""name"": """ & Replace(objFile.Path,"\","\\") & """,""lastmodified"": """ & objFile.DateLastModified & """},")
	Next
    ShowSubFolders(objSubFolder) ' calls function for recursivity support while subfolder exists
   Next
End Sub
 
' end of script - output file closure
Output.Close

Le but étant d'explorer une arborescence récursivement et de générer un fichier json structuré de la façon suivante :

folder
--file
---path
---lastmodifieddate
--file
--file
---path
---lastmodifieddate
--file
folder
folder
--file
---path
---lastmodifieddate
--file
--file
---path
---lastmodifieddate
--file
folder

Tout se passe nickel sauf que pour respecter la syntaxe json il me faut enlever les virgules avant chaque "]}". j'arrive à le faire pour la fin du fichier avec un simple If, mais pas pour les éléments enfants des folders...

Après avoir longuement réfléchi, il me semble que je ne peux pas faire cette modif pendant la création du fichier ligne par ligne car la structure des boulcles ne le permet pas... Ou alors il me faudrait complétement repenser le code...

Je cherche donc un system D pour, une fois le fichier complété, utiliser Replace() pour changer mes ",]}" en "]}" !!!
C'est surement tout con mais je comprends pas comment

Si une âme charitable voudrais bien....
Merci à vous