2010-10-01 6 views
1

J'essaie d'insérer une chaîne commençant par le titre du mot title: New string to be inserted dans un fichier dont le format est le suivant. Un tas de texte en haut, un bloc de lignes commençant par le mot title: et un tas de texte en bas.vbscript insertion d'une ligne par ordre alphabétique

Some content here at the top 
And more content 
and the titles will begin next 

title: Basic String 
title: How does it evaluate strings 
title: Identify code links 
title: Translating vbscript to string 

some content at the bottom 
and more content at the bottom 

La chose est que je préfère insérer cette nouvelle chaîne title: New string to be inserted afin qu'elle soit organisée par ordre alphabétique dans le bloc de titres pour le rendre plus facile de maintenir ce fichier. Comment puis-je faire cela avec vbscript. Je l'ai découvert il y a quelques heures et je pense que c'est une excellente alternative pour ce que j'essaie de faire, mais pas encore très bon, donc toute aide serait géniale

Comment je passerais à travers toutes les lignes de le fichier, copiez chaque ligne dans un nouveau fichier , jusqu'à ce que je frappe un titre qui est par ordre alphabétique après mon titre, ajouter mon titre dans le nouveau fichier à cet endroit, puis copiez le reste du dossier au nouveau classer et fermer.

Parce que je suis pauvre dans la syntaxe vbscript, je ne peux penser à un algorithme pseudo,

Loop to read through all lines 
    If the line does not start with the word title:, copy it as is into the new file 
    If the line starts with the word title, remove the `title:` sub-string, then check if it is alphabetically before or after my title 
     If before my title, copy it into the new file 
     If after my title, then copy my title there, and copy all rest of the file as is, and EXIT 
End loop 
+0

Dans votre boucle les deux premières lignes ont un sens pour moi. mais qu'entendez-vous par si avant mon titre sur votre troisième ligne dans la boucle? et que voulez-vous dire par après si mon titre, dans la ligne 4 dans la boucle, puis vous dites aussi copier mon titre là-bas? Je ne comprends pas. S'il vous plaît élaborer. – Pavan

Répondre

3

Il n'y a pas moyen facile de trier dans vbscript. Vous devez faire votre propre sous-routine de tri. Voici un peu de triche en utilisant le tri de Windows cmd.

strToInsert= WScript.Arguments(0) 
strFileName = WScript.Arguments(1) 
Set objFS = CreateObject("Scripting.FileSystemObject") 
If objFS.FileExists("temp") Then 
    objFS.DeleteFile("temp") 
End If 
Set objRE = New RegExp 
objRE.IgnoreCase = False 
objRE.Pattern = "^title.*" 
Set objFile = objFS.OpenTextFile(strFileName) 
Set objOutFile = objFS.OpenTextFile("temp",2 , True) 
Dim A() 
d=0 
Do Until objFile.AtEndOfStream  
    linenum=objFile.Line 
    strLine = objFile.ReadLine 
    Set Matches = objRE.Execute(strLine) 
    For Each Match in Matches ' Iterate Matches collection.    
     objOutFile.Write(Match.Value & vbCrLf) 
     ReDim Preserve A(d) 
     A(d)=linenum ' get position of title lines 
     d=d+1 
    Next  
Loop  
objOutFile.Write(strToInsert & vbCrLf) 
objFile.Close 
objOutFile.Close 

Set WshShell = CreateObject("WScript.Shell") 
Set objFile = objFS.OpenTextFile(strFileName) 
Do Until objFile.AtEndOfStream 
    linenum=objFile.Line 
    strLine = objFile.ReadLine 
    c=0 
    If linenum = A(0) Then 
     Set oExec = WshShell.Exec("sort temp ") 
     Do While Not oExec.StdOut.AtEndOfStream 
       sLine = oExec.StdOut.ReadLine 
       WScript.Echo sLine 
       c=c+1 
     Loop 
     oExec.Terminate 
    End If 
    If linenum <A(0) Or linenum > A(UBound(A)) Then 
     WScript.Echo strLine 
    End If 
Loop 

Sortie

C:\test>cscript //nologo myscript.vbs "title: to insert new string" file 
Some content here at the top 
And more content 
and the titles will begin next 

title: Basic String 
title: How does it evaluate strings 
title: Identify code links 
title: to insert new string 
title: Translating vbscript to string 

some content at the bottom 
and more content at the bottom 

Si vous voulez faire le tri avec vbscript, vous pouvez effectuer une recherche stackoverflow pour « tableaux de tri VBSCRIPT » et il y a des suggestions sur la façon de le faire.

1

EDIT: code modifié à VB.

Très simple. Vous pouvez réaliser cela en concaténant la chaîne dans VB.

Avant d'ajouter le stringToBeInserted faire tout cela avant la main

Dim stringToBeInserted As String = "WHATEVER THE NAME OF THE TITLE" 
Dim appendTitle As String = "Title: " 
Dim newStringToBeInserted As String = appendTitle & stringToBeInserted 

Hope this helps. Dites-moi si c'est le cas.

PK

+0

ive a mis à jour la réponse afin que vous puissiez le comprendre. c'est en VB maintenant – Pavan

Questions connexes