2013-02-13 124 views
1

我将以下VBS文件拼凑在一起以读取Excel源文件,并使用基于Excel列A的名称和基于列B(连续)的内容创建文件。这所有的作品...VBScript:使用Excel中的文件夹结构创建文件

Dim xlsFile 
Dim objExcel 
Dim outFile 
Dim path 

path = "C:\Documents and Settings\Andy\Desktop\SHORTURLs\JSPs" 
xlsFile = path & "\urls.xls" 
Set objExcel = WScript.CreateObject("Excel.Application") 
objExcel.Workbooks.open(xlsFile) 
' Create the File System Object 
Set objFSO = CreateObject("Scripting.FileSystemObject") 

intRow = 2 'Row 1 contains headings 

' Here is the loop that cycles through the cells 
Do Until objExcel.Cells(intRow,1).Value = "" 
    strFile = objExcel.Cells(intRow, 1).Value 
    strDestURL = objExcel.Cells(intRow, 2).Value 

    ' -- The heart of the create file script 
    'Set objTextFile = objFSO.CreateTextFile("./" & strFile & ".jsp", True) 
    outFile = path & "" & strFile & ".jsp" 
    Set objTextFile = objFSO.CreateTextFile(outFile, True) 

    ' Prep file contents 
    sText = "<%" & vbCrLf 
    sText = sText & "//REDIRECT301" & vbCrLf 
    sText = sText & "//System.out.println(""<LOGGED> "" + " & strDestURL & ");" & vbCrLf 
    sText = sText & "String dest = """ & strDestURL & """;" & vbCrLf 
    sText = sText & "response.setStatus(response.SC_MOVED_PERMANENTLY); // response.SC_MOVED_TEMPORARILY [OR] response.SC_MOVED_PERMANENTLY" & vbCrLf 
    sText = sText & "response.setHeader(""Location"", dest);" & vbCrLf 
    sText = sText & "response.setHeader(""Connection"", ""close"");" & vbCrLf 
    sText = sText & "%>" 
    ' Write a line. 
    objTextFile.Write(sText) 

    objTextFile.Close 
    intRow = intRow + 1 
Loop 

objExcel.Quit 
WScript.Quit 

不过,我现在需要修改VBS基于列中的文件夹结构创建文件(B列不受影响)。 Excel的架构:

+----------------------+----------------------+ 
|  Column A  |  Column B  | 
+----------------------+----------------------+ 
| /folder/filename.jsp | /url/destination.jsp | 
+----------------------+----------------------+ 
| /folder/filename.jsp | /url/destination.jsp | 
+----------------------+----------------------+ 
| /folder/filename.jsp | /url/destination.jsp | 
+----------------------+----------------------+ 

我将如何去这些文件夹中创建的文件夹和文件? 供参考:我相信文件夹结构应该不超过1深度(即/folder/file.xxx而不是/folder/folder/file.xxx)。

PS。我知道在JSP文件中使用response.setHeader()是不好的做法,但可悲的是,我们被迫这样做。

回答

4

使用FSO。

  1. .GetParentFolderName()来从文件规范
  2. .BuildPath()目录前面加上一个共同的路径前缀
  3. .FolderExists()检查是否需要创建目录
  4. .CreateFolder()如有必要