VBScript自动查找和移动文件 [英] VBScript to find and move files automatically

查看:89
本文介绍了VBScript自动查找和移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是尝试使工作自动化,因为我们最近遇到了人们记忆犹新的问题.

I've been tasked with trying to automate a task at work, because we've had issues lately with people remembering to do it.

通常,这是我需要脚本执行的操作:

In general, here's what I need a script to do:

  1. 以YYYYMMDD格式获取前一天的日期
  2. 输入具有指定名称的文件夹
  3. 在该位置下的所有文件夹中搜索4个特定文件
  4. 将这些文件复制到几个不同的位置

我遇到的问题是,对于我要查找的4个文件,它们位于2个不同的文件夹中. 3合1、1合1.这些文件夹的名称每天都会更改,具体取决于它们在由其他软件生成时放入的队列.我需要移动这些文件,以便可以在它们上运行另一个脚本.我在弄清楚如何做到这一点时遇到了麻烦.有人有想法吗?

The issue I'm having is that, for the 4 files I'm looking for, they're located in 2 different folders. 3 in 1, 1 in the other. The names of these folders changes daily, depending on what queue they got put into when generated by some other software. I need these files to be moved so that another script can be run on them. I'm having trouble figuring out how to accomplish this. Anyone have some ideas?

推荐答案

如果包含有趣文件的文件夹是带日期的目录的子文件夹,则可以使用嵌套循环:

If the folders containing the interesting files are subfolders of your dated directory, you can use a nested loop:

  Dim sDFolder : sDFolder    = "..\data\20110105"
  Dim dicFiNa  : Set dicFiNa = CreateObject("Scripting.Dictionary")
  dicFiNa("1.txt") = ""
  dicFiNa("3.txt") = ""
  dicFiNa("5.txt") = ""
  Dim oRDir     : Set oRDir  = goFS.GetFolder(sDFolder)
  Dim oSDir
  For Each oSDir In oRDir.SubFolders
      Dim oFile
      For Each oFile In oSDir.Files
          WScript.Echo "looking at", oFile.Path
          If dicFiNa.Exists(oFile.Name) Then
             WScript.Echo "found", oFile.Name, "will copy"
          End If
      Next
  Next

输出:

looking at E:\trials\SoTrials\answers\8750206\data\20110105\whatever\6.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\whatever\5.txt
found 5.txt will copy
looking at E:\trials\SoTrials\answers\8750206\data\20110105\unknown\4.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\unknown\3.txt
found 3.txt will copy
looking at E:\trials\SoTrials\answers\8750206\data\20110105\puzzle\2.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\puzzle\1.txt
found 1.txt will copy

完整的递归遍历会更加复杂,因此,如果需要的话,可以这么说.

A full recursive walk would be a bit more complex, so say so, if you need it.

只是为了好玩:一个递归版本:

Just for fun: a recursive version:

  Dim sDFolder : sDFolder    = "..\data\20110105"
  Dim dicFiNa  : Set dicFiNa = CreateObject("Scripting.Dictionary")
  dicFiNa("1.txt")  = ""
  dicFiNa("3.txt")  = ""
  dicFiNa("55.txt") = ""
  Dim oRDir     : Set oRDir  = goFS.GetFolder(sDFolder)
  walk oRDir, dicFiNa, "whatever you need to copy the files"

Sub walk(oDir, dicFiNa, vCargo)
  Dim oItem
  For Each oItem In oDir.Files
      WScript.Echo "looking at", oItem.Path
      If dicFiNa.Exists(oItem.Name) Then
         WScript.Echo "found", oItem.Name, "will copy"
      End If
  Next
  For Each oItem In oDir.SubFolders
      walk oItem, dicFiNa, vCargo
  Next
End Sub

输出:

looking at E:\trials\SoTrials\answers\8750206\data\20110105\whatever\6.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\whatever\5.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\unknown\4.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\unknown\3.txt
found 3.txt will copy *
looking at E:\trials\SoTrials\answers\8750206\data\20110105\puzzle\2.txt
looking at E:\trials\SoTrials\answers\8750206\data\20110105\puzzle\1.txt
found 1.txt will copy *
looking at E:\trials\SoTrials\answers\8750206\data\20110105\puzzle\deep\deeper\55.txt
found 55.txt will copy *

解决许可问题后,立即

(*).

(*) as soon as the permission problem is solved.

这篇关于VBScript自动查找和移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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