将文件名读入数组或字典以用作用户输入

时间:2017-08-09 12:26:23

标签: vbscript

我想有一个脚本读取特定文件夹并提取基本文件名,删除最后两个字符,然后使用结果填充输入框的文本。然后,用户从给定选项中进行选择,并且脚本的其余部分搜索并用所选文本替换第二文件夹中的文本。 初始目标文件夹中的示例文件名:

  

ABFA1
  ABFA3
  ABFA4
  HVA1
  HVA3
  HVA4
  ITALA1
  ITALA3
  ITALA4

显然,一旦删除了最后2个字符,我将留下重复项,我需要将其删除。 这是我到目前为止的脚本的一部分:

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = objFSO.BuildPath(objFSO.GetAbsolutePathName("."), "\dwgs\logos")  
If Not objFSO.FolderExists(strFilePath) Then
  wscript.echo("Folder does not exist, script exiting")
  wscript.quit
End if
'
Set objFolder = objFSO.GetFolder (strFilePath)
For Each objFile In objFolder.Files
 strFile = objFSO.GetBaseName(objFile.Name)
  strFile = LEFT(strFile, (LEN(strFile)-2))
'   wscript.echo(strFile)           
    Next

'delete all duplicate files names and add result to dictionary (or array?)

'create an inputbox and present a number of choices populated by the dictionary/array

user1 = InputBox("Select a Logo:"&(chr(13))&(chr(13))&(*array/dict*)), "Logo Replacement Script")

' Set arguments
strFilePath2 = objFSO.BuildPath(objFSO.GetAbsolutePathName("."), "\dwgs")
FindString = "dwgs\logos\"
ReplaceStringWith = "dwgs\logos\"&(user1)

' Find and replace function

我能够删除最后2个字符的基本文件名,但我不知道如何清除重复项,然后在输入框中使用结果? (我想象一个数字的输入框中的文本,然后选择,用户输入数字来表示使用哪个选项) 我的第一个想法是使用一个数组,但经过一些阅读,似乎字典方法可能会更好。不幸的是,我还没有弄清楚如何将它融入脚本中。 任何帮助将不胜感激。

更新了包含Ekkehard输入的脚本:

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = objFSO.BuildPath(objFSO.GetAbsolutePathName("."), "\dwgs\logos")  
'
Function ShowFilesInFolder(strFolderPath)
    Set oFolder = objFSO.GetFolder(strFolderPath)
    Set oFileCollection = oFolder.Files 
    For Each oTempFile in oFileCollection
        strTemp = strTemp & oTempFile.name 
        strTemp = LEFT(strTemp, (LEN(strTemp)-6))
    Next
    ShowFilesInFolder = strTemp
 End Function 
x = ShowFilesInFolder(strFilePath)
'
Function mkDic(aK, aV)
  Dim tmp : Set tmp = CreateObject("Scripting.Dictionary")
  Dim i
  For i = 0 To UBound(aK)
      tmp(aK(i)) = aV(i)
  Next
  Set mkDic = tmp
 End Function
'
 Dim a : a = Split (x)
 WScript.Echo Join(mkDic(a, a).Keys)

由于某种原因,我不能让mkDic函数从ShowFilesInFolder函数中分割输入? 有没有比我想出的更简单的方法呢?

2 个答案:

答案 0 :(得分:1)

唯一性的VBScript工具是The Dictionary。这个演示(参见here

Option Explicit

' based on an Array 2 Dictionary function from
' !! https://stackoverflow.com/a/45554988/603855
Function mkDic(aK, aV)
  Dim tmp : Set tmp = CreateObject("Scripting.Dictionary")
  Dim i
  For i = 0 To UBound(aK)
      ' tmp(aK(i)) = aV(i)
      tmp(Mid(aK(i), 1, Len(aK(i)) - 2)) = aV(i)
  Next
  Set mkDic = tmp
 End Function

 Dim a : a = Split("ABFA1 ABFA3 ABFA4 HVA1 HVA3 HVA4 ITALA1 ITALA3 ITALA4")
 WScript.Echo Join(a)
 WScript.Echo Join(mkDic(a, a).Keys), "=>", Join(mkDic(a, a).Items) 

输出:

cscript 45590698.vbs
ABFA1 ABFA3 ABFA4 HVA1 HVA3 HVA4 ITALA1 ITALA3 ITALA4
ABF HV ITAL => ABFA4 HVA4 ITALA4

显示了如何对数组进行重复数据删除以及如何对(唯一)键进行字符串化以连接到提示符。

答案 1 :(得分:0)

我设法获得了一个工作脚本,但是如果不使用几个临时文本文件来传递数据,就无法弄清楚如何做到这一点。 我想我会发布代码,以防它可能对某人有所帮助。

Const ForReading = 1, ForWriting = 2, ForAppending = 8, N = 0
Set fso = CreateObject("Scripting.FileSystemObject")
strFilePath = fso.BuildPath(fso.GetAbsolutePathName("."), "\dwgs\logos")
If Not fso.FolderExists(strFilePath) Then
  wscript.echo("The LOGO Folder Does Not Exist - Exiting Script")
  wscript.quit
End if
'
Set f = fso.OpenTextFile("xtempLogos.txt", ForWriting, True)
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace (strFilePath)
For Each strFileName in objFolder.Items
a = objFolder.GetDetailsOf (strFileName, N)
a = LEFT(a, (LEN(a)-6))
f.Writeline (a)
Next
f.Close
'
Set f = fso.OpenTextFile("xtempLogos.txt", ForReading)              
TheFile = f.ReadAll
f.Close
'
Function mkDic(aK, aV)
  Dim tmp : Set tmp = CreateObject("Scripting.Dictionary")
  Dim i
  For i = 0 To UBound(aK)
      tmp(aK(i)) = aV(i)
  Next
  Set mkDic = tmp
 End Function
'
Set f = fso.OpenTextFile("xtempLogos.txt", ForWriting, True)
 Dim a : a = Split(TheFile,vbcrlf)
 a = Join(mkDic(a, a).Keys)
 f.Writeline (a)
f.Close
'
Set f = fso.OpenTextFile("xtempLogos2.txt", ForWriting, True)
Set f = fso.OpenTextFile("xtempLogos.txt", ForReading)
theFile = f.ReadAll
number = 1
myArray = Split(theFile)
for i = 0 to Ubound(MyArray)-1
Set f = fso.OpenTextFile("xtempLogos2.txt", ForAppending, True)
If number < 10 then f.Writeline (number) & ".........." & myArray(i)
If number >=10 then f.Writeline (number) & "........." & myArray(i)
f.Writeline ""
Set f = fso.OpenTextFile("xtempLogos.txt", ForReading, True)
number=number+1
Next
f.Close
'
Set f = fso.OpenTextFile("xtempLogos2.txt", ForReading)             
TheFile = f.ReadAll
f.Close
'
user1 = InputBox("WHICH LOGO DO YOU WANT TO ADD?"&(chr(13))&(chr(13))&(chr(13))& (theFile), "Add Logo Script", 11)
choice = (user1) - 1
wscript.echo myArray(choice)
'
Set f = fso.GetFile("xtempLogos.txt")
f.Delete
Set f = fso.GetFile("xtempLogos2.txt")
f.Delete