2013-02-13 156 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()如有必要
相關問題