使用 OpenText 和 SaveAs 方法在 VBS 中打开相对路径 [英] Open Relative path in VBS with OpenText and SaveAs method

查看:42
本文介绍了使用 OpenText 和 SaveAs 方法在 VBS 中打开相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在 vbs 文件中有一些代码,基本上我想打开一个 CSV 文件.执行一些更改(我可以稍后解决),然后将其另存为 xls.xml 文件.当使用完全限定的文件路径运行时,代码可以完美运行.但是我需要文件路径是相对的.这些文件将始终打开并保存到运行脚本的同一路径.我看过 GetParentFolderName 和 GetAbsolutePathName.但是我不知道如何在文本中调用完全限定的路径.我试过只将变量放在文件名带和不带引号的位置,并在其后附加一个句点.

ok so I have some code in a vbs file, where basically I want to open a CSV file. Perform some changes (I can work that out later) and then save it as an xls. The code works flawlessly when run with a fully qualified file path. However I need the file path to be relative. The files will always be opened and saved to the same path the script is run from. Ive looked at GetParentFolderName and GetAbsolutePathName. However I cant figure how to call the fully qualified path in the text. I've tried just putting the variable where the file name is with and without quotes, appended it with a period.

任何有关构建函数或其他内容的示例,然后在下面的代码中调用它都会有很大帮助.

Any example on building the function or whatever, and then calling it in the code below would be a huge help.

Dim myXL
Const xlDelimited = 1
Const xlWorkbookNormal = -4143

Set myXL=CreateObject("Excel.Application")
myXL.Visible=False
myXL.WorkBooks.OpenText "file.csv", , , xlDelimited, , , , , True 

myXL.DisplayAlerts=False

MORE CODE WILL GO HERE

myXL.ActiveWorkbook.SaveAs "new_file.xls", xlWorkbookNormal 

myXL.DisplayAlerts=True

myXL.ActiveWorkbook.Close False
myXL.Quit

Set myXL = Nothing

推荐答案

您可以使用我在关于 相对路径

You can get the path of your script by using this code that i Found at an answer about Relative Path

p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

p 应该是脚本的路径,然后您可以将 "file.csv" 编辑为 p &"\file.csv" 等等.

p should be the path to your script you can then edit "file.csv" to p & "\file.csv" and so.

这篇关于使用 OpenText 和 SaveAs 方法在 VBS 中打开相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆