2009-10-19 6 views
0

En utilisant VB6Vous remplacez un problème de nom de fichier?

Dans un dossier, je suis d'avoir un nombre n de fichiers texte, les noms de fichiers sont comme abc.mis-txt, def.fin @ txt, donc je veux renommer les noms de fichiers comme abcmis.txt, deffin.txt. J'ai utilisé une fonction pour renommer.

code

Dim filename3 As String 
filename3 = Dir$(txtSourceDatabaseFile & "\*.txt", vbDirectory) 
Do While filename3 <> "" 

« Fonction pour renommer

Dim strInput As String 
Dim stroutput As String 
Dim strChar As String 
Dim intChar As Integer 
Dim intLoop As Integer 
strInput = filename3 
For intLoop = 1 To Len(strInput) 
strChar = Mid$(strInput, intLoop, 1) 
intChar = Asc(strChar) 
If ((intChar >= 48) And (intChar <= 57)) Or _ 
((intChar >= 65) And (intChar <= 90)) Or _ 
((intChar >= 97) And (intChar <= 122)) Or _ 
(intChar = 95) Then 
stroutput = stroutput & strChar 
End If 
Next 

Name txtSourceDatabaseFile & "\" & filename3 As txtSourceDatabaseFile & "\" & stroutput & ".txt" 


filename3 = Dir$ 
Loop 

Au-dessus de codage fonctionne, le problème est me sers tout conditon, il renomme tous les fichiers txt, il donne un nom J'aime

Par exemple. Il ajoute aussi un nom de fichier précédent, car j'obtiens un nom de fichier dans while loop lui-même.

Comment modifier mon code.

Répondre

1

Déplacez votre fonction en fonction de VB6 qui prend le nom de fichier comme argument et renvoie la nouvelle, par exemple:

Private Function GetTextOnlyFileName(ByVal strOldName as String) As String 
    Dim strTemp As String 
    Dim strChar As String 
    Dim intChar As Integer 
    Dim intLoop As Integer 
    For intLoop = 1 To Len(strOldName) 
     strChar = Mid$(strOldName, intLoop, 1) 
     intChar = Asc(strChar) 
     If ((intChar >= 48) And (intChar <= 57)) Or _ 
      ((intChar >= 65) And (intChar <= 90)) Or _ 
      ((intChar >= 97) And (intChar <= 122)) Or _ 
      (intChar = 95) Then 
     strTemp = strTemp & strChar 
     End If 
    Next 
    GetTextOnlyFileName = strTemp 
End Function 

Ensuite, utilisez quelque chose comme ça ce pour obtenir le nouveau nom de fichier:

Do While filename3 <> "" 
    strNewName = GetTextOnlyFileName(strFileName3) 
    'next rename the file to strNewName 


    'get name of next file 
Loop 

Cependant, que voulez-vous arriver si deux noms de fichiers sources différents donnent le même nom? Par exemple, actuellement [email protected] et ab @ cd @ ef.txt renverraient la même chose.

Votre code ajoute actuellement le nom de fichier au précédent car vous réutilisez les mêmes variables encore et encore dans votre boucle sans effacer les valeurs précédentes. Ma fonction va fonctionner, mais vous pouvez simplement ajouter la ligne strOutPut = "" après la ligne strInput = nomfichier3 et cela devrait fonctionner aussi.

Questions connexes