我和一个朋友开始构建一个 VBScript,目标是当我们在计算机上插入外部硬盘或 USB 时复制任何打开的文件/某些特定格式的文档(如 pdf、pptx)。我们的脚本仅将特定格式的所有文档从外部硬盘/USB 复制到我的计算机,但我们必须在插入外部硬盘/USB 后手动执行脚本。
我们希望脚本执行的操作:
- 插入外部硬盘/USB 时自动启动
- 复制后不显示弹出窗口
- 可能只复制用户打开的文件(pdf、jpeg、pptx)
这是我们到目前为止所做的:
' foreach.vbs
' Testing the for each function on files
'
' BEGIN
' Create a File System Object to handle files and folders
Set fso = CreateObject("Scripting.FileSystemObject")
' Some vars
src = "C:\" ' The source of search, should be changed before use
dest = "c:\temp\1\" ' destination where we will copy files to
' It would be a bright idead to force and/or create dest before
' starting copy
fso.CreateFolder(dest)
Set ofolder = fso.GetFolder(src) ' set as object
' get all files inside the specified folder
Set allfiles = ofolder.Files
' Enter a For Each Loop that will process each of the files
For Each sfile in allfiles
' Better get all extensions in lower case
If LCASE(fso.GetExtensionName(sfile.Name)) = "bat" then
' Print out what we find
wscript.echo sfile.Name
fso.CopyFile sfile, dest
End If
Next