2013-02-13 12 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 для создания файлов в структуре папок на основе столбца A (столбец B не влияет). Excel схема:

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

Как бы идти о создании папок и файлов в этих папках? FYI: Я считаю, что структура папок должна быть не более 1 глубины (т. Е. /folder/file.xxx не /folder/folder/file.xxx).

PS. Я знаю, response.setHeader() в JSP-файлах - плохая практика, но, к сожалению, мы были вынуждены сделать это таким образом.

ответ

4

Использование FSO.

  1. .GetParentFolderName() получить каталог из файла спецификации
  2. .BuildPath() предварять общий путь PREfix
  3. .FolderExists(), чтобы проверить, нужно ли вам создать каталог
  4. .CreateFolder() При необходимости