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

如何通过.bat文件运行.vbs以将.xls转换为.csv,csv,batch-file,vbscript,vbs,cmd,bat,Csv,Batch File,Vbscript,Vbs,Cmd,Bat,我对脚本完全陌生,请原谅我的无知。 (运行Windows 10) 我在这篇文章中找到了一个工作解决方案,可以使用.vbs将.xls文件转换为.csv: 我正在处理的文件有多个图纸,vbs通过将文件拖到vbs文件图标上执行来工作。我不明白vbs是如何获得输入参数的。我复制了Chris Rudd发布的代码 'Courtesy of Chris Rudd on stackoverflow.com 'Modified by Christian Lemer 'plang 'ScottF ' ht

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

我正在处理的文件有多个图纸,vbs通过将文件拖到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文件?”


任何帮助都将不胜感激。

此方法在批处理环境中始终有效,但不能使用双引号,因为对于带有空格的文件路径,建议使用双引号

Start filepath\name.vbs
没有双引号

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


WSscript.exe“filepath\name.vbs”

此方法在批处理环境中始终有效,但是不能使用双引号,这对于带有空格的文件路径是可取的

Start filepath\name.vbs
没有双引号

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


WSscript.exe“filepath\name.vbs”

Wscript/windowscript主机提供了一个以多种语言执行脚本的环境,并在命令行环境中启动脚本

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


有时,在windows中执行VBscript会打开文本编辑器,而不是运行它。这是由于默认文件关联发生了更改。有时防病毒软件也会这样做,以保护您的系统。在这种情况下,您需要更改注册表检查此链接

Wscript/Window Script主机提供了一个环境来以多种语言执行脚本,并且Cscript在命令行环境中启动脚本

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

有时,在windows中执行VBscript会打开文本编辑器,而不是运行它。这是由于默认文件关联发生了更改。有时防病毒软件也会这样做,以保护您的系统。在这种情况下,您需要更改注册表检查此链接
  • 查看此文件.gif

  • 我知道,只有当您通过拖放作为参数传递文件时,这才有效

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


    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    


    • 查看此文件.gif,您可以看到如何通过拖放文件中传递参数(与文件相同):


    此file.png显示如何将参数与



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

    • 如果需要,请尝试使用以下方法:

    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    
    __

    这就是我使用**.xlsx*所在文件夹中的文件的人,因此,无需传递驱动器\文件夹路径:


    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    

    很抱歉,我的英语有限

    • 查看此文件.gif


    我知道,只有当您通过拖放作为参数传递文件时,这才有效

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


    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    


    • 查看此文件.gif,您可以看到如何通过拖放文件中传递参数(与文件相同):


    此file.png显示如何将参数与



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

    • 如果需要,请尝试使用以下方法:

    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    
    __

    这就是我使用**.xlsx*所在文件夹中的文件的人,因此,无需传递驱动器\文件夹路径:


    Drop  .xl* files on me to export each sheet as CSV
    
    Obs.: .xl* is equal to "any file with (.xl)"+any" == *.xlsx, *.xlsm...
    
    "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"
    
    wscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    rem ::  or   ::
    cscript 58924333.vbs "012019.xlsx" "022019.xlsx"
    


    很抱歉,就我所知,我的英语水平有限,VBA只能在工作簿中运行。因此,您可以安排一个批处理文件,只打开该文件,并在工作表上运行宏。这就是为什么您将.xls文件拖到VB文件上时,该VB文件会运行的原因吗?是否使用工作簿运行VB代码?这是否回答了您的问题?到达那里。我必须弄清楚如何调用运行.vbs的.bat来运行“hello world”。现在我可以这样做了,我将尝试用上面的.vbs文件替换test.vbs。据我所知,VBA只在工作簿中运行。因此,您可以安排一个批处理文件,只打开该文件,并在工作表上运行宏