No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Copie de fichiers (VBScript)

Le script suivant copie le fichier C:\test.txt vers C:\test2.txt

  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2. If objFSO.FileExists("C:\test.txt") Then
  3. objFSO.CopyFile "C:\test.txt" , "C:\test2.txt"
  4. Else
  5. MsgBox "Fichier C:\test.txt introuvable", vbExclamation
  6. End If
  7. Set objFSO = Nothing

Procédure pas à pas

Nous devons d'abord créer une instance de l'objet Scripting.FileSystemObject, et l'assigner à objFSO (nom que nous déterminons à notre guise).

Set objFSO = CreateObject("Scripting.FileSystemObject")

Nous devons tester si le script peut trouver le fichier à l'adresse indiquée.

If objFSO.FileExists("C:\test.txt") Then

Nous pouvons aussi tester de cette manière :

If Not objFSO.FileExists("C:\test.txt") Then

Dans ce cas, si le fichier n'est pas trouvé, nous pouvons effectuer certaines actions.

Dans le cas où le fichier est présent, nous le copions au même endroit en le renommant test2.txt

objFSO.CopyFile "C:\test.txt" , "C:\test2.txt"

Nous pouvons à présent libérer l'objet objFSO.

Set objFSO = Nothing

Variantes: avec une fonction vbs

Ici, nous travaillons avec une fonction.

Les fichiers de source et de destination sont passés en argument, ce qui rend le code plus évolutif : vous pouvez par exemple faire un appel de cette fonction après avoir demandé à l'utilisateur quels sont les fichiers source et destination.

Nous pouvons aussi remarquer la déclaration de la constante OverwriteExisting, qui argumente ensuite objFSO.CopyFile pour déclarer que si le fichier de destination existe, il sera écrasé.

Nous pouvons réaliser un copier/coller du script dans un fichier texte dont nous aurons renommé l'extension en .vbs.
Nous remarquons que l'appel de la fonction se fait à la suite du code, et que les paramètres ne sont pas encadrés par des parenthèses.

  1. Function FileCp (strSourceFile, strTargetFile)
  2. Const OverwriteExisting = True
  3. Set objFSO = CreateObject("Scripting.FileSystemObject")
  4. If Not objFSO.FileExists(strSourceFile) Then
  5. MsgBox "Fichier " & strSourceFile & " introuvable", vbExclamation
  6. Exit Function
  7. End If
  8. objFSO.CopyFile strSourceFile , strTargetFile, OverwriteExisting
  9. Set objFSO = Nothing
  10. End Function
  11. FileCp "c:\test.txt", "c:\test2.txt"

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 11/06/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/vbs-copie-fichier.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.