使用数组值vbscript重命名文件

时间:2012-02-15 19:59:00

标签: vbscript

我需要根据我在数组中创建的值重命名文件夹中的文件。我遇到了问题,因为我不知道如何声明需要重命名的文件部分。

文件如下:Xnnnnnnnnnnnnnn_stmt-n.pdf其中n = 0-9之间的任何数字。我想要做的是查看stmt之后的n值部分,并将其重命名为我已经声明的一组数组值。 n值可以是1-400。我希望脚本说如果n =数组值,那么将文件的那一部分重命名为数组值。

以下是我创建的一些示例数组值。 1 =“aa”2 =“ab”3 =“ac”... 50 =“bx”... 150 =“fv”....一直到400 =“ql”。

这是我到目前为止没有执行的代码:

Const INPATH = "\folder"
Const OUTPATH = "\folder"

' check that the directories exist. 
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(INPATH) then
err.raise 1,, "Path '" & INPATH & "' not found"
end if
if not fso.FolderExists(OUTPATH) then
err.raise 1,, "Path '" & OUTPATH & "' not found"
end if

dim Array(400)
Array(0)=" "
Array(1)="aa"
Array(2)="ab" ' this would continue until 400
Array(400)="ql"

dim infolder: set infolder = fso.GetFolder(INPATH)
dim file
for each file in infolder.files

dim name: name = file.name
dim parts: parts = split(name, ".")
dim nanname: nanname = left(parts(0), 21)
dim value: value = right(parts(0), 1)  
dim newname
dim newvalue 
    if value = 1 then newvalue = Array(1)
    if value = 2 then newvalue = Array(2)
    if value = 400 then newvalue = Array(400) 
        newname = nanname & newvalue & "." & parts(2) 'parts 2=pdf
        file.move fso.buildpath(OUTPATH & newname)
    endif 

next 

我遇到的一个主要问题是试图找到一种声明'值'的方法,因为文件名的值部分可以是任意数字1-400。任何想法或帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

第一个问题是“Array”是一个VBScript函数,你不能将它用作变量名。这是一个更新的脚本。我没有测试过,但你应该能够理解它。

dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(INPATH) then
  err.raise 1,, "Path '" & INPATH & "' not found"
end if
if not fso.FolderExists(OUTPATH) then
  err.raise 1,, "Path '" & OUTPATH & "' not found"
end if    

dim LetterArray(400)

dim vChrLoop1 = 0 'First letter loop
dim vChrLoop2 = 0 'Second letter loop
dim vArrayPointer = 0 'Total counter pointer

Do Until vArrayPointer = 400 'loop through every value
  Do Until vChrLoop2 = 25 ' loop until the letter "z"
     LetterArray(vArrayPointer) = Chr(97+vChrLoop1) & Chr(97+vChrLoop2)

     vChrLoop2 = vChrLoop2 + 1 'move to the next letter for the second letter
     vArrayPointer = vArrayPointer + 1 'increment the pointer
  Loop

  vChrLoop1 = vChrLoop1 + 1 'move to the next letter for first letter
  vChrLoop2 = 0 'Reset the second letter loop to "a"
Loop

dim infolder: set infolder = fso.GetFolder(INPATH)
dim file

for each file in infolder.files

  dim name: name = file.name
  dim parts: parts = split(name, ".")
  dim nanname: nanname = left(parts(0), 21)

  dim aValue(1)
  aValue = Split(parts(0),"-")
  dim value: value = aValue(1)
  dim newname

  newname = nanname & LetterArray(value) & "." & parts(2) 'parts 2=pdf
    file.move fso.buildpath(OUTPATH & newname)
next