IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Envoi de fichier sur serveur FTP distant


Sujet :

VBScript

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Avril 2011
    Messages : 5
    Points : 1
    Points
    1
    Par défaut Envoi de fichier sur serveur FTP distant
    Bonjour,

    Je suis plutot debutant en VB script.
    Je souhaite effectuer une copie d'un fichier, du serveur ou setrouve ce script, vers un autre. Vers un remote server, vers un repertoire FTP.
    J'ai trouvé le script suivant qui correspond, mais j'ai quelques questions :

    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
    If UploadByFTP("C:\MonFichierLocal.txt" , "www/rep" , "FichierFTP.txt") Then
    	Msgbox "Upload OK"
    Else
    	Msgbox "problème lors du transfert"
    End If
     
    Function UploadByFTP(ByVal FichierLocal, ByVal Repertoire, ByVal FichierFTP)
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
        Dim fso, f_cmd, f_ret
    	
        FichierCommandeFTP = "Cmdftp.ftp"
        FichierRetourFTP = "return.txt"
       
        FTP = "ftp.site.com"
        Login = "user"
        Pass = "pass"
    	
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f_cmd = fso.OpenTextFile(FichierCommandeFTP, ForWriting, True)	
        f_cmd.Write "OPEN " & FTP & vbCrLf
        f_cmd.Write "USER " & Login & vbCrLf & Pass & vbCrLf
        f_cmd.Write "cd " & Repertoire & vbCrLf
        f_cmd.Write "put " & FichierLocal & " " & FichierFTP & vbCrLf
        f_cmd.Write "quit"
        f_cmd.Close
     
    	Set WSHShell = CreateObject("WScript.Shell")	
        WSHShell.Run "cmd /c ftp.exe -s:" & FichierCommandeFTP & " > " & FichierRetourFTP, 0 , True
       
        UploadByFTP = False
        Set f_ret = fso.OpenTextFile(FichierRetourFTP, ForReading)
    	while Not f_ret.AtEndOfStream And UploadByFTP = False
    		iF Trim(f_ret.ReadLine) = "226 File receive OK." Then
    			UploadByFTP = True
    		End If
        Wend
        f_ret.close
        fso.DeleteFile FichierCommandeFTP
        fso.DeleteFile FichierRetourFTP
        Set WSHShell = Nothing
        Set fso      = Nothing
        Set f_cmd    = Nothing
        Set f_ret    = Nothing
    End Function
    a quoi correspond :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    FichierCommandeFTP = "Cmdftp.ftp"
        FichierRetourFTP = "return.txt"
    et la commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    WSHShell.Run "cmd /c ftp.exe -s:" & FichierCommandeFTP & " > " & FichierRetourFTP, 0 , True
    Merci,

  2. #2
    Rédacteur
    Avatar de omen999
    Profil pro
    Inscrit en
    Février 2006
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 1 299
    Points : 3 554
    Points
    3 554
    Par défaut
    bonjour,
    si je lance ftp -? dans une fenêtre de commande, j'ai la réponse...
    FichierCommandeFTP désigne le fichier qui contient des commandes ftp qui seront exécutées après démarrage (dans ton cas Cmdftp.ftp)
    et les réponses de ftp seront loggées dans return.txt

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Avril 2011
    Messages : 5
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    Ah merci, mais quand je lance je n'arrive pas a faire une copie d'un fichier vers un serveur distant.
    Le script tourne sans erreur, mais dans le repertoire cible il n'y aucun fichier créé.
    J'ai peut etre fait une mauvaise manip,?.
    T'a réussi a faire une copie de fichier sur un serveur distant?

  4. #4
    Rédacteur
    Avatar de omen999
    Profil pro
    Inscrit en
    Février 2006
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 1 299
    Points : 3 554
    Points
    3 554
    Par défaut
    mais dans le repertoire cible il n'y aucun fichier créé.
    J'ai peut etre fait une mauvaise manip,?.
    difficile de savoir puisqu'on ignore le contenu de Cmdftp.ftp

  5. #5
    Nouveau Candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Avril 2011
    Messages : 5
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    Mon fichier de retour est :

    ftp> ftp> OPEN 220.xx.xx.xx
    Invalid command.

    ftp> userid
    Invalid command.

    ftp> password
    Not connected.

    ftp> cd D:\TEST\
    Not connected.

    ftp> put C:\depart\23.txt
    quit

    ------------------------------

    Ma connection au serveur ne marche pas.

  6. #6
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 040
    Points
    20 040
    Par défaut
    bonsoir,

    ton fichier de retour ne correspond pas au source que tu nous montre dans ton premier message tu devrais revoir ton adaptation ..

  7. #7
    Rédacteur
    Avatar de omen999
    Profil pro
    Inscrit en
    Février 2006
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 1 299
    Points : 3 554
    Points
    3 554
    Par défaut
    tu devrais revoir ton adaptation
    comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    WSHShell.Run "cmd /c ftp.exe -s: FichierCommandeFTP  > " & FichierRetourFTP, 0 , True

  8. #8
    Nouveau Candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Avril 2011
    Messages : 5
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    Le vb srcipt que j'ai lancé est le suivant :
    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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    Dim Source, sMsg, Hostname, objNetwork, objShell, dest
     
    Dim oFSO, contents, folderName, fileColl, Filedetails, MinuteMax
     
     
    Source = "C:\xxxx\xxxx\xxxx\depart\"
    dest = "C:\xxxxx\xxxxx\xxxxx\transfert\"
     
    MinuteMax = 11
     
    Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject ("Wscript.Network")
     
    Set folderName = oFSO.GetFolder(Source) 
    Set fileColl = folderName.Files 
     
    DateSysteme = Now  'On recupère la date système
     
     
    For Each contents in fileColl 
    	If oFSO.GetExtensionName(contents) = "txt" Then	
     
    		Set Filedetails = oFSO.GetFile(contents) 
     
    		If DateDiff("n",Filedetails.DateCreated,DateSysteme) < MinuteMax Then
     
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    		'oFSO.CopyFile Source& contents.Name, Destination,True
    ''''''''''''''''''''''''''''''''''''''''
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
        Dim fso, f_cmd, f_ret
     
        ' Initialisation de données de connexions au FTP   
        FTP = "220.xx.xx.xx"
        Login = "user"
        Pass = "password"
        FichierLocal = Source& contents.Name
        sRemotePath = "D:\MSJ_TEST\"
     
        FichierCommandeFTP = "Cmdftp.ftp"
        FichierRetourFTP = "return.txt"
     
     
        ' Écriture du fichier des commandes FTP 
        Set f_cmd = oFSO.OpenTextFile(FichierCommandeFTP, ForWriting, True)	
        f_cmd.Write "OPEN " & FTP & vbCrLf
        f_cmd.Write "" & Login & vbCrLf & Pass & vbCrLf
        f_cmd.Write "cd " & sRemotePath & vbCrLf
        f_cmd.Write "put " & FichierLocal & vbCrLf
        f_cmd.Write "quit"
        f_cmd.Close
     
     
        ' Appel a FTP.exe pour exécution du fichier de commande
        ' L'éxécution créera le fichier de retour
        Set WSHShell = CreateObject("WScript.Shell")
     
         WSHShell.Run "cmd /c ftp.exe -s:" & FichierCommandeFTP & " > " & FichierRetourFTP, 0 , True	
     
    oFSO.CopyFile Source& contents.Name, dest, true
     
        ' Si le fichier de retour existe
        ' Si inexistant => Probleme d'execution => Retour à False
        UploadByFTP = False
        If oFSO.FileExists(FichierRetourFTP) Then
            Set f_ret = oFSO.OpenTextFile(FichierRetourFTP, ForReading)
            ' Parcour du fichier de retour à la recherche de l'indication "Transfert OK"
        	while Not f_ret.AtEndOfStream And UploadByFTP = False
                    ' Si une des lignes commence par "226 ", alors il n'y a pas eu de probleme lors du transfert
        		iF Left(f_ret.ReadLine,4) = "226 " Then
            			UploadByFTP = True
        		End If
            Wend
            f_ret.close
            Set f_ret    = Nothing
            ' Suppression du fichier de retour
            'oFSO.DeleteFile FichierRetourFTP
        End If 
     
        ' Suppression du fichier de commande FTP
        oFSO.DeleteFile FichierCommandeFTP
     
        Set WSHShell = Nothing
        Set f_cmd    = Nothing
     
     
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     
    		end if
     
     
    'Gestion de lenvoi automatique de mail
     
     
    	If Err.Number <> 0 Then    
     
    'ENVOI DE MAIL AUTO
     
    	end if 'Fin err
    	end if
     
    Next 
    Set oFSO      = Nothing
    wscript.echo "VBS Complete"
    WScript.Quit
    Mon fichier de retour est le suivant :
    ftp> ftp> OPEN 220.xx.xx.xx
    Invalid command.

    ftp> uer
    Invalid command.

    ftp> password
    Not connected.

    ftp> cd D:\MSJ_TEST\
    Not connected.

    ftp> put C:\xxxx\xxxx\xxxx\depart\27.txt
    quit
    En utilisant le code que tu m'a fournit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    WSHShell.Run "cmd /c ftp.exe -s: FichierCommandeFTP > " & FichierRetourFTP, 0 , True
    le fichier retour est vide.


  9. #9
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Points : 4 061
    Points
    4 061
    Par défaut
    J'ai essayé la commande dans un cmd et elle ne marche pas.
    Voir ici ce tuto sur ftp.exe: http://cs.ecs.baylor.edu/~donahoo/cl...s/ftp/ftp.html

    Moi je ferais plus un truc dans le genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    WSHShell.Run "cmd /c ftp.exe 200.xx.xx.xx -s: " & FichierCommandeFTP & " > " & FichierRetourFTP, 0 , True

  10. #10
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 040
    Points
    20 040
    Par défaut
    bonsoir,

    je ne comprends toujours pas pourquoi tu as 2 fois ftp dans ta premiére ligne de ton fichier retour :

    ftp> ftp> OPEN 220.xx.xx.xx
    et donc je doute encore que tu nous montre le bon code ..

    de même pourquoi avoir enlever la commande "USER" ..?

    pourrai t'on voir le contenu du fichier "Cmdftp.ftp" généré ?

  11. #11
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Points : 4 061
    Points
    4 061
    Par défaut
    Citation Envoyé par bbil Voir le message
    bonsoir,

    je ne comprends toujours pas pourquoi tu as 2 fois ftp dans ta premiére ligne de ton fichier retour :


    et donc je doute encore que tu nous montre le bon code ..

    de même pourquoi avoir enlever la commande "USER" ..?

    pourrai t'on voir le contenu du fichier "Cmdftp.ftp" généré ?
    Perso j'ai essayé la commande ftp.exe avec un fichier de commande du genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    OPEN ip
    user
    pass
    CD path
    PUT path
    Et ftp exe met tout seul dans le fichier de resultat ftp > ftp >

    Peut être un bug de l'outil ?

    En tout cas je suis d'accord avec toi il serait bien de voir le contenu des fichiers originaux FichierCommandeFTP et FichierRetourFTP

  12. #12
    Nouveau Candidat au Club
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Avril 2011
    Messages : 5
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    le contenu de mon fichier "Cmdftp.ftp" généré

    ------------------------
    OPEN 220.xx.xx.xx
    user
    password
    cd D:\MSJ_TEST\
    put C:\xxxxx\xxxxx\xxxxx\depart\27.txt
    quit
    -------------------------------------

    Le code que j'avais posté la dernière est le code entier,... ah oui j'ai retirer l'envoi automatique de mail.... c'est tout...

    Je comprend pas aussi pourquoi j'ai

    ftp> ftp> OPEN 220.xx.xx.xx

    2 fois ftp....

    Merci a tous,

  13. #13
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 040
    Points
    20 040
    Par défaut
    Salut,

    moi ce que j'essai de comprendre c'est la suite

    ftp> ftp> OPEN 220.xx.xx.xx
    Invalid command.
    "Invalid command" ...

    as tu essayé de te connecte à ton serveur en ligne de commande ..(sans passer par un script ..)



  14. #14
    Rédacteur
    Avatar de omen999
    Profil pro
    Inscrit en
    Février 2006
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 1 299
    Points : 3 554
    Points
    3 554
    Par défaut
    as tu essayé de te connecte à ton serveur en ligne de commande
    par exemple en lançant en ligne de commande
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    telnet 220.xxx.xxx.xxx 21

Discussions similaires

  1. Réponses: 7
    Dernier message: 18/05/2011, 20h06
  2. [TinyMCE] Envoie de fichiers sur le FTP
    Par FrenchyNeo dans le forum Bibliothèques & Frameworks
    Réponses: 3
    Dernier message: 10/06/2008, 20h11
  3. créer un nouveau dossier sur serveur ftp distant
    Par marion5515 dans le forum Serveurs (Apache, IIS,...)
    Réponses: 1
    Dernier message: 18/05/2008, 22h39
  4. + de 2000 fichiers sur serveur FTP
    Par _seb_ dans le forum WinDev
    Réponses: 4
    Dernier message: 17/09/2007, 10h59
  5. Erreur programme d'envoi fichier sur serveur ftp
    Par batssa dans le forum Langage
    Réponses: 3
    Dernier message: 17/08/2007, 14h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo