[Catégorie modifiée .Net --> VBS] codes vbs (effacement de fichiers)

olisab1 Messages postés 12 Date d'inscription dimanche 1 juillet 2007 Statut Membre Dernière intervention 4 juillet 2012 - 12 sept. 2010 à 21:12
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 27 sept. 2010 à 12:44
Bonjour,

J'ai le code suivant en vbs qui permet d'effacer des fichiers txt plus ancien que 24h. A chaque fois j'ai le message suivant

Line:16
Char:5
Error:fichier introuvable

Ce que je ne comprend pas c'est qu'il y a des fichiers.

Pouvez vous me dire d'ou vient l'erreur.
Merci d'avance


Dim courant, temp_attente
courant = "C:\envoi\attente_echantillon"
ShowFolderList(courant)
Function ShowFolderList(strPath)

Const ForReading 1, ForWriting 2
Dim objFso, colFile, objTextStream, objTextFile
Dim strLine, nom_fichier, extension, nom_ss_date, date_f, loc_date
Dim fichier_gestion, num_fich, i, loc_gestion, loc_gestion2, dossier_dest

Set objFso = CreateObject("Scripting.FileSystemObject")
For Each colFile in objFso.GetFolder(strPath).Files
If LCase(objFso.GetExtensionName(colFile.Name)) = "txt"
Then

num_fich = 0
nom_fichier = Ucase(objFso.GetBaseName
(colFile.Name))

date_f = Right(nom_fichier, 14)
loc_date = InStr(nom_fichier, date_f)
nom_ss_date = Left(nom_fichier, loc_date - 1)

loc_gestion = InStr(LCase(nom_fichier), "gestion")
extension = "." & objFso.GetExtensionName
(colFile.Name)

fichier_gestion = nom_ss_date & "gestion" &
extension
If loc_gestion = 0 Then

If Len(Int(date_f)) = 14 Then
Dim annee, mois, jour, heure, min, sec
date_1 = Split(Date, "/")
jour = date_1(0)

mois = date_1(1)
annee = date_1(2)

date_2 = Split(time, ":")
heure = date_2(0)
min = date_2(1)
sec = date_2(2)

date_3 = Int(annee & mois & jour & heure &
min & sec)

'Recuperation heure + date du fichier
locheure = Mid(date_f, 9, 2)
locdate = Mid(date_f, 7, 2)
'Recuperation heure + date du PC
locheure3 = Mid(date_3, 9,2)
locdate3 = Mid(date_3, 7,2)

'Si la date du fichier n'est pas egale
a 30 ou 31 et la date du PC n'est pas egale à 1
If (locdate <> 30 or 31) and locdate3
<> 1 Then

If Int(date_f) < date_3 - 1000000
Then
If InStr(UCase(nom_fichier),
"_MP_") Then
objFso.DeleteFile courant &
nom_fichier & extension
Else
If InStr(UCase(nom_fichier),
"_CE_") Then
dossier_dest = "C:\envoi
\incomplets\centrale_mesure"
Elseif Instr(LCase(nom_fichier),
"_ce_") Then
dossier_dest = "C:\envoi
\incomplets\centrale_mesure"

Elseif InStr(UCase(nom_fichier),
"_ME_") Then
dossier_dest = "C:\envoi
\incomplets\metrologie"
Elseif InStr(LCase(nom_fichier),
"_me_") Then
dossier_dest = "C:\envoi
\incomplets\metrologie"

Elseif Instr(UCase(nom_fichier),
"_3D_") Then
dossier_dest = "C:\envoi
\incomplets\3D"
Elseif Instr(LCase(nom_fichier),
"_3d_") Then
dossier_dest = "C:\envoi
\incomplets\3D"
ElseIf Instr(LCase(nom_fichier),
"pivot_ms_") Then
dossier_dest = "C:\envoi
\incomplets\Autres"
Else
dossier_dest = "C:\envoi
\incomplets\Autres"
End If
objFso.MoveFile courant &
nom_fichier & extension, dossier_dest
End If
If objFso.FileExists
(courant & nom_ss_date & "gestion" & extension) Then
objFso.DeleteFile
courant & nom_ss_date & "gestion" & extension
End If
End If
Else
'Call MsgBox("Date
incorrecte sur le fichier " & nom_fichier)
End If

End If
End If
End If
Next
End Function

6 réponses

cs_ghuysmans99 Messages postés 3982 Date d'inscription jeudi 14 juillet 2005 Statut Membre Dernière intervention 30 juin 2013 16
12 sept. 2010 à 23:07
T'as essayé de faire un MsgBox du nom du fichier que tu veux supprimer ?

VB.NET is good ... VB6 is better
Utilise Réponse acceptée quand un post répond à ta question
0
olisab1 Messages postés 12 Date d'inscription dimanche 1 juillet 2007 Statut Membre Dernière intervention 4 juillet 2012
14 sept. 2010 à 17:42
Bonjour,

J'ai essaye avec un Msgbox mais il ne prend pas le .txt c pour ca qu'il me met fichier introuvable. Que faut-il ajouter pour qu'il me prend en compte ce .txt
Merci par avance

Olivier
0
cs_ghuysmans99 Messages postés 3982 Date d'inscription jeudi 14 juillet 2005 Statut Membre Dernière intervention 30 juin 2013 16
14 sept. 2010 à 18:58
Si tu pouvais poster sur PasteBinle code indenté, ça serait déjà mieux : là on comprend rien.

VB.NET is good ... VB6 is better
Utilise Réponse acceptée quand un post répond à ta question
0
titifxy Messages postés 4 Date d'inscription lundi 20 septembre 2010 Statut Membre Dernière intervention 27 septembre 2010
26 sept. 2010 à 08:08
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
titifxy Messages postés 4 Date d'inscription lundi 20 septembre 2010 Statut Membre Dernière intervention 27 septembre 2010
27 sept. 2010 à 09:05
Ce n'est pas la réponse que j'attendais Olisab1.
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
27 sept. 2010 à 12:44
 Bonjour,


Beaucoup d'incohérences dans ce script.

En supposant qu'un fichier correct soit "gestion_CE_20100926084009.txt"

Option Explicit
Const PathInput = "C:\envoi\attente_echantillon"
ShowFolderList()
Function ShowFolderList()
   Dim objFso, colFile
   Dim strFile, strDateFile, PathOutput 
   Set objFso = CreateObject("Scripting.FileSystemObject")
   
   For Each colFile in objFso.GetFolder(PathInput).Files
       strFile = objFso.GetBaseName(colFile.Name)

       If LCase(objFso.GetExtensionName(colFile.Name)) = "txt" And _
          InStr(LCase(colFile), "gestion") <> 0 And _
          Len(objFso.GetBaseName(colFile.Name)) >= 14 Then

          strDateFile = Right(strFile, 14)
          
          If IsDate(checkDate(strDateFile)) = True Then
             MsgBox "date ok" & vbCrLf & checkDate(strDateFile),,colFile.Name
             If DateDiff("h", checkDate(strDateFile), Now) >= 24 Then
                If InStr(UCase(strFile),"_MP_") Then
                   objFso.DeleteFile colFile.Path
                Else
                   If InStr(UCase(strFile),"_CE_") Then
                      PathOutput = "C:\envoi\incomplets\centrale_mesure"
                   ElseIf InStr(UCase(strFile),"_ME_") Then
                      PathOutput = "C:\envoi\incomplets\metrologie"
                   ElseIf Instr(UCase(strFile),"_3D_") Then
                      PathOutput = "C:\envoi\incomplets\3D"
                   Else
                      PathOutput = "C:\envoi\incomplets\Autres"
                   End If
                   If Not objFso.FileExists(PathOutput & colFile.Name) Then
                      objFso.MoveFile colFile.Path, PathOutput
                   Else
                      MsgBox PathOutput & colFile.Name,,"fichier non déplacé car déjà existant en sortie"
                   End If
                End If
             Else
                MsgBox strFile,,"date antérieure à 24h"         
             End if
          Else
             MsgBox colFile.Path &vbCrLf& strDateFile,,"Date incorrecte"
          End If
       Else
          MsgBox strFile,,"Fichier non conforme"   
       End if
   Next
   Set objFso = Nothing
End Function

Function checkDate(strDateFile)
   checkDate = Mid(strDateFile,7,2)  & "/" &_
               Mid(strDateFile,5,2)  & "/" &_
               Left(strDateFile,4)   & Space(1) &_
               Mid(strDateFile,9,2)  & ":" &_
               Mid(strDateFile,11,2) & ":" &_
               Right(strDateFile,2)
End Function
 



jean-marc
0
Rejoignez-nous