gpt4 book ai didi

excel - VBScript:从 Excel 创建具有文件夹结构的文件

转载 作者:行者123 更新时间:2023-12-04 21:25:41 26 4
gpt4 key购买 nike

我将以下 VBS 文件拼凑在一起,以读取 Excel 源文件并创建名称基于 Excel A 列和内容基于 B 列(连接)的文件。这一切都有效...

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 |
+----------------------+----------------------+

我将如何在这些文件夹中创建文件夹和文件?
仅供引用:我认为文件夹结构的深度不应超过 1(即 /folder/file.xxx 而不是 /folder/folder/file.xxx )。

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

最佳答案

使用 FSO。

  • .GetParentFolderName()从文件规范
  • 中获取目录
  • .BuildPath()在公共(public)路径前缀前添加
  • .FolderExists()查看是否需要创建目录
  • .CreateFolder()如有必要
  • 关于excel - VBScript:从 Excel 创建具有文件夹结构的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14859889/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com