(view source code of xl2csv.vbs as plain text)
Option Explicit
Dim blnForceOverwrite
Dim objExcel, objFSO
Dim strFileIn, strFileOut
Const xlCSV = 6
blnForceOverwrite = False
With WScript.Arguments
If .Named.Count > 1 Then Syntax
If .Named.Count = 1 Then
If .Named.Exists( "Q" ) Then
blnForceOverwrite = True
Else
Syntax
End If
End If
If .Unnamed.Count <> 1 Then Syntax
End With
Set objExcel = CreateObject( "Excel.application" )
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
With objFSO
strFileIn = WScript.Arguments.Unnamed(0)
If .FileExists( strFileIn ) Then
strFileIn = .GetAbsolutePathName( strFileIn )
objExcel.Workbooks.Open strFileIn, , True
strFileOut = .BuildPath( .GetParentFolderName( strFileIn ), .GetBaseName( strFileIn ) & ".csv" )
If blnForceOverwrite And .FileExists( strFileOut ) Then
.DeleteFile strFileOut, True
WScript.Echo "Existing CSV file replaced."
End If
On Error Resume Next
objExcel.ActiveWorkbook.SaveAs strFileOut, xlCSV
If Err.Number = 1004 Then
WScript.Echo "Existing CSV file not replaced."
End If
On Error Goto 0
objExcel.ActiveWorkbook.Close False
Else
Syntax
End If
End With
Set objFSO = Nothing
Set objExcel = Nothing
Sub Syntax( )
WScript.Echo vbCrLf _
& "XL2CSV.vbs, Version 1.00" _
& vbCrLf _
& "Convert an Excel sheet to CSV" _
& vbCrLf & vbCrLf _
& "Usage: " & UCase( WScript.ScriptName ) & " excel_file.xls [ /Q ]" _
& vbCrLf & vbCrLf _
& "Where: excel_file.xls is the Excel file to be converted" _
& vbCrLf _
& " /Q silently overwrites an existing CSV file" _
& vbCrLf _
& " (default: prompt for confirmation)" _
& vbCrLf & vbCrLf _
& "Notes: The CSV file will be saved with the name of the excel file," _
& vbCrLf _
& " extension "".csv"", in the Excel file's parent folder." _
& vbCrLf _
& " If the CSV file exists, a dialog window will prompt for" _
& vbCrLf _
& " confirmation before overwriting the existing file." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" _
& vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0081 seconds