插入时从 USB 复制具有特定扩展名的文件的 Vbscript [英] Vbscript to copy files with specific extension from usb when plugged in

查看:25
本文介绍了插入时从 USB 复制具有特定扩展名的文件的 Vbscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个可视化的基本脚本,当插入机器时,它允许我从 USB 驱动器复制所有内容.

I have this visual basic script that allows me to copy all the content from a usb drive when inserted in the machine.

'Sauvegarde automatique des clés USB et SDCARD dés leurs insertion.
'Ce Programme sert à copier automatiquement chaque clé USB nouvellement insérée ou bien une SDCard.
'Il sert à faire des Sauvegardes incrémentielles de vos clés USB.
'Pour chaque clé USB, il crée un dossier de cette forme "NomMachine_NomVolumeUSB_NumSerie" dans le dossier %AppData% et
'il fait une copie totale pour la première fois, puis incrémentielle , càd ,il copie juste les nouveaux fichiers et les fichiers modifiés.
'Crée le 23/09/2014 © Hackoo
Option Explicit
Do
   Call AutoSave_USB_SDCARD()
   Pause(30)
Loop
'********************************************AutoSave_USB_SDCARD()************************************************
Sub AutoSave_USB_SDCARD()
   Dim Ws,WshNetwork,NomMachine,AppData,strComputer,objWMIService,objDisk,colDisks
   Dim fso,Drive,NumSerie,volume,cible,Amovible,Dossier,chemin,Command,Result
   Set Ws = CreateObject("WScript.Shell")
   Set WshNetwork = CreateObject("WScript.Network")
   NomMachine = WshNetwork.ComputerName
   AppData= ws.ExpandEnvironmentStrings("%AppData%")
   cible = AppData & "\"
   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colDisks = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_LogicalDisk")

   For Each objDisk in colDisks
      If objDisk.DriveType = 2 Then
         Set fso = CreateObject("Scripting.FileSystemObject")
         For Each Drive In fso.Drives
            If Drive.IsReady Then
               If Drive.DriveType = 1 Then
                  NumSerie=fso.Drives(Drive + "\").SerialNumber
                  Amovible=fso.Drives(Drive + "\")
                  Numserie=ABS(INT(Numserie))
                  volume=fso.Drives(Drive + "\").VolumeName
                  Dossier=NomMachine & "_" & volume &"_"& NumSerie
                  chemin=cible & Dossier
                  Command = "cmd /c Xcopy.exe " & Amovible &" "& chemin &" /I /D /Y /S /J /C"
                  Result = Ws.Run(Command,0,True)
               end if
            End If   
         Next
      End If   
   Next
End Sub
'***************************************Fin du AutoSave_USB_SDCARD()*********************************************
'****************************************************************************************************************
Sub Pause(Sec)
   Wscript.Sleep(Sec*1000)
End Sub 
'****************************************************************************************************************

我需要修改它,以便它只复制带有 .doc 和 .pdf 扩展名的文件.

I need to modify it so that it copies only the files with .doc and .pdf extensions.

推荐答案

试试这个修改后的脚本:AutoSave_USB_SDCARD.vbs

Try this modified script : AutoSave_USB_SDCARD.vbs

'Automatic backup USB sticks and SD CARD dice their insertion.
'This program is used to automatically copy each newly inserted USB key or a SDCard.
'It is used to make incremental backups of your USB key.
'For each USB drive, it creates a folder for this form "ComputerName_VolumeUSB_SerialNumber" in the D:\USB_Backup folder and
'It is a total copy first and then incrementally, ie, it just copies the new files and changed files.
'Created on 23/09/2014 © Hackoo
'04/02/2016
'Edited and translated from french to english and changed the directory target to be copied to D:\USB_Backup
'And copies only the files with .doc .docx and .pdf extensions
Option Explicit
Do
   Call AutoSave_USB_SDCARD()
   Pause(30)
Loop
'********************************************AutoSave_USB_SDCARD()************************************************
Sub AutoSave_USB_SDCARD()
   Dim Ws,WshNetwork,ComputerName,strComputer,objWMIService,objDisk,colDisks
   Dim fso,Drive,SerialNumber,volume,Target,Amovible,Folder,Command1,Command2,Command3,Result1,Result2,Result3
   Set Ws = CreateObject("WScript.Shell")
   Set WshNetwork = CreateObject("WScript.Network")
   ComputerName = WshNetwork.ComputerName
   Target = "D:\USB_Backup"
   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colDisks = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_LogicalDisk")

   For Each objDisk in colDisks
      If objDisk.DriveType = 2 Then
         Set fso = CreateObject("Scripting.FileSystemObject")
         For Each Drive In fso.Drives
            If Drive.IsReady Then
               If Drive.DriveType = 1 Then
                  SerialNumber=fso.Drives(Drive + "\").SerialNumber
                  Amovible=fso.Drives(Drive + "\")
                  SerialNumber=ABS(INT(SerialNumber))
                  volume=fso.Drives(Drive + "\").VolumeName
                  Folder=ComputerName & "_" & volume &"_"& SerialNumber
                  Target=Target &"\"& Folder
                  Command1 = "cmd /c Xcopy.exe " & Amovible &"\*.pdf "& Target &" /I /D /Y /S /J /C"
                  Command2 = "cmd /c Xcopy.exe " & Amovible &"\*.doc "& Target &" /I /D /Y /S /J /C"
                  Command3 = "cmd /c Xcopy.exe " & Amovible &"\*.docx "& Target &" /I /D /Y /S /J /C"
                  Result1 = Ws.Run(Command1,0,True)
                  Result2 = Ws.Run(Command2,0,True)
                  Result3 = Ws.Run(Command3,0,True)
               end if
            End If   
         Next
      End If   
   Next
End Sub
'****************************************************************************************************************
Sub Pause(Sec)
   Wscript.Sleep(Sec*1000)
End Sub 
'****************************************************************************************************************

这篇关于插入时从 USB 复制具有特定扩展名的文件的 Vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆