我是脚本新手,请原谅我的无知。
(运行Windows 10)
我从这篇文章中找到了一个可行的解决方案,可以使用.vbs将.xls文件转换为.csv:
我正在使用的文件有多个工作表,而vb则通过将文件拖到vbs文件图标上来执行。我不明白vbs如何获取输入参数。我复制了克里斯·鲁德(Chris Rudd)发布的这段代码
'Courtesy of Chris Rudd on stackoverflow.com
'Modified by Christian Lemer
'plang
'ScottF
' https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line
'* Usage: Drop .xl* files on me to export each sheet as CSV
'* Global Settings and Variables
'Dim gSkip
Set args = Wscript.Arguments
For Each sFilename In args
iErr = ExportExcelFileToCSV(sFilename)
' 0 for normal success
' 404 for file not found
' 10 for file skipped (or user abort if script returns 10)
Next
WScript.Quit(0)
Function ExportExcelFileToCSV(sFilename)
'* Settings
Dim oExcel, oFSO, oExcelFile
Set oExcel = CreateObject("Excel.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")
iCSV_Format = 6
'* Set Up
sExtension = oFSO.GetExtensionName(sFilename)
if sExtension = "" then
ExportExcelFileToCSV = 404
Exit Function
end if
sTest = Mid(sExtension,1,2) '* first 2 letters of the extension, vb's missing a Like operator
if not (sTest = "xl") then
if (PromptForSkip(sFilename,oExcel)) then
ExportExcelFileToCSV = 10
Exit Function
end if
End If
sAbsoluteSource = oFSO.GetAbsolutePathName(sFilename)
sAbsoluteDestination = Replace(sAbsoluteSource,sExtension,"{sheet}.csv")
'* Do Work
Set oExcelFile = oExcel.Workbooks.Open(sAbsoluteSource)
For Each oSheet in oExcelFile.Sheets
sThisDestination = Replace(sAbsoluteDestination,"{sheet}",oSheet.Name)
oExcelFile.Sheets(oSheet.Name).Select
oExcelFile.SaveAs sThisDestination, iCSV_Format
Next
'* Take Down
oExcelFile.Close False
oExcel.Quit
ExportExcelFileToCSV = 0
Exit Function
End Function
Function PromptForSkip(sFilename,oExcel)
if not (VarType(gSkip) = vbEmpty) then
PromptForSkip = gSkip
Exit Function
end if
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sPrompt = vbCRLF & _
"A filename was received that doesn't appear to be an Excel Document." & vbCRLF & _
"Do you want to skip this and all other unrecognized files? (Will only prompt this once)" & vbCRLF & _
"" & vbCRLF & _
"Yes - Will skip all further files that don't have a .xl* extension" & vbCRLF & _
"No - Will pass the file to excel regardless of extension" & vbCRLF & _
"Cancel - Abort any further conversions and exit this script" & vbCRLF & _
"" & vbCRLF & _
"The unrecognized file was:" & vbCRLF & _
sFilename & vbCRLF & _
"" & vbCRLF & _
"The path returned by the system was:" & vbCRLF & _
oFSO.GetAbsolutePathName(sFilename) & vbCRLF
sTitle = "Unrecognized File Type Encountered"
sResponse = MsgBox (sPrompt,vbYesNoCancel,sTitle)
Select Case sResponse
Case vbYes
gSkip = True
Case vbNo
gSkip = False
Case vbCancel
oExcel.Quit
WScript.Quit(10) '* 10 Is the error code I use to indicate there was a user abort (1 because wasn't successful, + 0 because the user chose to exit)
End Select
PromptForSkip = gSkip
Exit Function
End Function
这很适合我的需要,但是我想每小时运行一次,并将.csv文件保存到新目录。
我尝试使用任务计划程序运行.vbs,但它仅在我的文本编辑器中打开文件,但不执行。我的想法是创建一个运行.vbs的.batch文件。我以为可以用这样的PowerShell命令调用.vbs:
Start "XlsToCsv"
Start "XlsToCsv.vbs"
但是这两个在文本编辑器中打开.vbs的效果相同。
也许一个简单的问题是:“如何从PowerShell或命令提示符运行.vbs文件?”
任何帮助将不胜感激。
答案 0 :(得分:0)
此方法在批处理环境中始终有效,但是不能使用双引号,对于带空格的文件路径,建议使用双引号。
Start filepath\name.vbs
没有双引号。
此方法在cmd.exe控制台中始终有效,但是在.bat程序中失败:
WSscript.exe“文件路径\名称.vbs”
答案 1 :(得分:0)
Wscript / Window脚本主机提供了一种环境,可以执行多种语言的脚本,而 Cscript 在命令行环境中启动脚本。
因此,如果您想在控制台中运行脚本,请使用cscript.exe和wscript.exe(如果您不需要控制台窗口)。因此,正如T3RR0R所述,您需要使用WScript命令来运行它。
有时,在Windows中执行VBscript会打开文本编辑器,而不是运行它。这是由于默认文件关联的更改。有时防病毒软件也会这样做,以保护您的系统。在这种情况下,您需要更改注册表,请检查此链接https://superuser.com/questions/1108349/change-default-program-for-opening-vbs-files
答案 2 :(得分:0)
我了解这仅在您使用拖放作为参数传递的文件vbs时有效...
在第七行的 vbs 中,您可以看到作者留下的使用说明:
Drop .xl* files on me to export each sheet as CSV
Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
此file.png显示了如何在
中使用参数您尝试过的代码没有使用/传递任何参数(参数),而没有传递任何参数(一个或多个文件),这将始终失败!
"XlsToCsv.vbs" "one_file_dot.extensio_equal_xlsx_.xlsx" "one_more_another_file_dot.extensio_equal_xlsx_too.xlsx"
rem :: observing if you are try in a different folder where the vbs + xlsx file are, try this:
"d:\folder\where\vbs\are\XlsToCsv.vbs" "d:\folder\where\xlsx\file1\are\1file_dot.extension_equal_xlsx_.xlsx" "d:\folder\where\xlsx\file1\are\one\more\are\too\one_more_another_file_dot.extensio_equal_xlsx_too.xlsx"
__
这是我在与文件**。xlsx *相同的文件夹中使用您的 文件的人,因此,无需传递驱动器\文件夹路径:
wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
rem :: or ::
cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
对不起,我的英语有限