繁体   English   中英

如何通过.bat文件运行.vbs将.xls转换为.csv

[英]How to run .vbs through .bat file to convert .xls to .csv

我对脚本完全陌生,所以请原谅我的无知。
(运行 Windows 10)我找到了一个有效的解决方案,可以使用这篇文章中的.vbs 将.xls 文件转换为.csv:

将 xls 转换为 csv

我正在使用的文件有多个工作表,vbs 通过将文件拖到 vbs 文件图标上来执行。 我不明白 vbs 如何获取输入 arguments。 我复制了 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 命令调用 the.vbs:

Start "XlsToCsv"
Start "XlsToCsv.vbs"

但是这两个都具有在文本编辑器中打开.vbs的相同效果。

也许一个更简单的问题是,“我如何从 PowerShell 或命令提示符运行.vbs 文件?”

任何帮助将不胜感激。

此方法在批处理环境中始终如一地工作,但是不能使用双引号,因为对于带有空格的文件路径是可取的。

Start filepath\name.vbs

没有双引号。

此方法在 cmd.exe 控制台中始终有效,但在 .bat 程序中失败:

WSscript.exe "文件路径\名称.vbs"

Wscript/Window Script host 提供了一个环境来执行各种语言的脚本, Cscript在命令行环境中启动一个脚本。

因此,如果您想在控制台中运行脚本,请使用 cscript.exe 和 wscript.exe,如果您不想要控制台 window。 因此,正如 T3RR0R 提到的,您需要使用 WScript 命令来运行它。

有时,在 windows 中执行 VBscript 会打开文本编辑器而不是运行它。 这是由于默认文件关联发生了变化。 有时防病毒软件也会这样做以保护您的系统。 在这种情况下,您需要更改注册表检查此链接https://superuser.com/questions/1108349/change-default-program-for-opening-vbs-files

  • 看看这个file.gif

在此处输入图像描述


我知道这仅在您通过拖放使用文件作为 arguments 传递时才有效...

您可以在第 7 行看到作者留下的关于如何在中使用的说明:


 Drop.xl* files on me to export each sheet as CSV Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...


  • 看看这个file.gif ,你可以看到如何通过拖放中传递 arguments (与相同)文件:

在此处输入图像描述


此 file.png 展示了如何将 arguments 与


![在此处输入图像描述


您尝试过的代码没有使用/传递任何 arguments(参数),没有传递任何 arguments,(一个或多个文件)这将始终失败!

  • 如果您需要尝试使用类似的东西:

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

对不起我有限的英语

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM