将Excel工作表转换为CSV并在TXT中存储文件名。 在未来脚本执行时跳过文件名

我想制作一个批处理脚本

a。)将XLS或XLSX转换为CSV b)将excel的名称写入TXT c。)在脚本的任何连续运行中跳过此excel

完整的脚本下面的问题


我目前有以下几点:

1.)运行转换器的batch file(由以下链接中的某人提供)

FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "Sheet2" "%%i" "%%i.csv" "CSVlog.txt" 

2.)转换器由ScottF&Christian Lemer 在命令行上将XLS转换为CSV

 if WScript.Arguments.Count < 3 Then WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <xls/xlsx source file> <csv destination file>" Wscript.Quit End If csv_format = 23 Set objFSO = CreateObject("Scripting.FileSystemObject") src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1)) dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2)) CSVlog = objFSO.GetAbsolutePathName(WScript.Arguments.Item(3)) Dim oExcel Set oExcel = CreateObject("Excel.Application") Dim oBook Set oBook = oExcel.Workbooks.Open(src_file) oBook.Sheets(WScript.Arguments.Item(0)).Select oBook.Application.Columns("A:J").NumberFormat = "@" oBook.SaveAs dest_file, csv_format oBook.Close False objFSO.opentextfile(CSVlog) CSVlog.Write(dest_file) oExcel.Quit 

该脚本退出后出现错误,“需要的对象:”CSVlog“,做了CSV。

有人可以帮助我吗?

1.)从CSV名称中删除“xlsx”? (目前CSVs存储为“Workbook.xlsx.csv”)

2.)将xlsx名称写入CSVlog.txt

3.)在开始转换之前查询日志文件。 如果该文件已经被处理,不要再处理它。

先谢了,亨宁


完整的脚本

嗨,所有需要它,附上整个脚本。

该脚本将在其文件夹中查找* .xls并将其转换为* .csv。

您可以将* .xlsreplace为* .xlsb,* .xlsm,* .xlsm

感谢ScottF,Christian Lemer和MC ND的原创和帮助!

batch file:

 :: Feed something into CSVLog to avoid "no search strings" errors echo ;%date: =_%_%time: =_% >> CSVLog.txt FOR /f "delims=" %%i IN ( 'DIR *.xls /b ^| findstr /v /r /x /g:CSVlog.txt' ) DO ExcelToCSV.vbs "Details" "%%i" "%%~ni.csv" "CSVlog.txt" 

我已经添加了/ r,因为/ l在某些文件名上会有问题。 如果您遇到问题,请更换

findstr /v /r /x /g:CSVlog.txt

findstr /v /l /x /g:CSVlog.txt'

VB脚本

 REM based on ScottF & Christian Lemer at https://stackoverflow.com/questions/1858195/ REM & MC ND at https://stackoverflow.com/questions/23381408/ REM script will convert a defined Worksheet from an XLS into CSV.Then store the name of the XLS in the CSVlog.txt. REM it will not process any XLS that are found in the CSVlog.txt if WScript.Arguments.Count < 4 Then WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <xls/xlsx source file> <csv destination file>" Wscript.Quit End If csv_format = 23 Set objFSO = CreateObject("Scripting.FileSystemObject") src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1)) dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2)) CSVlog = objFSO.GetAbsolutePathName(WScript.Arguments.Item(3)) entry = Wscript.Arguments.Item(1) Dim oExcel Set oExcel = CreateObject("Excel.Application") Dim oBook Set oBook = oExcel.Workbooks.Open(src_file) oBook.Sheets(WScript.Arguments.Item(0)).Select oBook.Application.Columns("A:J").NumberFormat = "@" oBook.SaveAs dest_file, csv_format,,,,,,,,,,True oBook.Close False oExcel.Quit objFSO.opentextfile(CSVlog,8,True).WriteLine(Wscript.Arguments.Item(1)) 

这将使分号分隔“CSV”。 要制作逗号分隔的CSV,

删除,,,,,,,,,,TrueoBook.SaveAs dest_file, csv_format,,,,,,,,,,True

对于1分和3分

 :: Feed something into CSVLog to avoid "no search strings" errors echo ;%date: =_%_%time: =_% >> CSVLog.txt FOR /f "delims=" %%i IN ( 'DIR *.xlsx /b ^| findstr /v /l /x /g:CSVlog.txt' ) DO ExcelToCSV.vbs "Sheet2" "%%i" "%%~ni.csv" "CSVlog.txt" 

findstr将过滤仅返回CSVlog.txt中未包含的文件的列表(只要确保该文件存在)。 %%~ni.csv (只有文件名,没有扩展名,添加.csv文本)将处理.xlsx移除

对于第二点

 .... oBook.Close False oExcel.Quit objFSO.opentextfile(CSVlog,8,True).WriteLine(Wscript.Arguments.Item(1)) 

如果你打开一个文件,你有两个select。 将对已打开文件的引用存储在variables中,并使用此引用写入文件,或直接使用获取的引用写入文件。 第二个选项是使用什么。 将.xlsx文件的名称写入日志,就像过滤input文件列表一样。 Close方法已被省略,因为当文件引用超出范围时文件将被closures。