几个好用的科研、办公批处理脚本;excel转csv的VBS脚本
- 2026-09-17 23:14:18
几个好用的科研、办公批处理脚本;excel转csv的VBS脚本两个操作逻辑:拖入、双击。 拖入类: 1、拖入文件,为文件增加日期前缀。 2、拖入文件,为文件增加“rev_”前缀,可用于标注版本、回复同事等。 3、上面那个没办法批量实现,支持批量的如下,想加什么前缀自己改。 4、这个是加尾缀的 5、增加前缀和尾缀 双击类: 1、移动子目录内所有的文件到当前脚本所在的根目录 2、识别11位数字号,并移动excel and pdf到根据11位数字号创建的文件夹中 最后,保存cmd文件之后,一定记得用 ANSI 另存为一下。 另外,R语言读取df文件的时候,只推荐使用baseR中的函数读取csv文件,而不要用各种第三方R包读取excel格式。 这里分享一个vbs脚本,拖入excel之后,可实现excel_2_csv文件的转换,然后再用R读取。
@echo offsetlocal enabledelayedexpansionrem 生成日期:YYYYMMDDset "datestr=%date:~,4%%date:~5,2%%date:~8,2%"set "datestr=%datestr:/=%"rem 遍历所有拖入的文件for %%F in (%*) do (ren "%%~fF" "%datestr%_%%~nF%%~xF")endlocal
@echo offsetlocal enabledelayedexpansion:: 获取拖入的文件路径和文件名set "filepath=%~1"set "filename=%~n1"set "fileext=%~x1"ren "%filepath%" "rev_%filename%%fileext%"
@echo offsetlocal enabledelayedexpansion:: 处理所有拖入的文件for %%F in (%*) do (set "filepath=%%~F"set "filename=%%~nF"set "fileext=%%~xF":: 重命名文件,增加前缀"前缀_"ren "%%~F" "前缀_%%~nF%%~xF")
@echo offsetlocal enabledelayedexpansion:: 处理所有拖入的文件for %%F in (%*) do (set "filepath=%%~F"set "filename=%%~nF"set "fileext=%%~xF":: 重命名文件,增加尾缀“_尾缀”ren "%%~F" "%%~nF_尾缀%%~xF")
@echo offsetlocal enabledelayedexpansion:: 设置前缀和后缀set "prefix=前缀_"set "suffix=_后缀":: 处理所有拖入的文件for %%F in (%*) do (set "filepath=%%~F"set "filename=%%~nF"set "fileext=%%~xF":: 重命名文件,增加前缀和后缀ren "%%~F" "!prefix!%%~nF!suffix!%%~xF")
@echo offsetlocal enabledelayedexpansionset "count=1"for /f "delims=" %%a in ('dir /a-d /b /s') do (set "filename=%%~nxa"set "target=!filename!":: 检查目标目录是否已存在同名文件,存在则添加序号if exist "!target!" (set "target=%%~na_!count!%%~xa"set /a "count+=1")move "%%~a" "!target!" >nul)
@echo offsetlocal EnableDelayedExpansionset ORDER_NO_LENGTH=11set LOG_FILE=order_sort_log.txtecho 开始运行 > "%LOG_FILE%"for %%f in (*.xlsx *.pdf) do (set name=%%~nxfset prefix=!name:~0,%ORDER_NO_LENGTH%!echo !prefix! | findstr /R "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" >nulif not errorlevel 1 (set folder=前缀_!prefix!_尾缀if not exist "!folder!" md "!folder!"move "%%f" "!folder!\" >nulecho 移动 %%f >> "%LOG_FILE%") else (echo 跳过 %%f >> "%LOG_FILE%"))echo 完成 >> "%LOG_FILE%"pause
' 创建一个Excel Application 对象Dim objExcel: Set objExcel = CreateObject("Excel.Application")objExcel.Visible = False' 打开 Excel 文件Dim objWorkbook: Set objWorkbook = objExcel.Workbooks.Open(WScript.Arguments.Item(0))' 获取 Excel 文件名和路径Dim strSourceFilePath: strSourceFilePath = WScript.Arguments.Item(0)Dim strSourceFileName: strSourceFileName = Mid(strSourceFilePath, InStrRev(strSourceFilePath, "\") + 1)Dim strSourceFileBaseName: strSourceFileBaseName = Left(strSourceFileName, InStrRev(strSourceFileName, ".") - 1)' 构造 CSV 文件名和路径Dim strCsvFilePath: strCsvFilePath = Left(strSourceFilePath, InStrRev(strSourceFilePath, "\")) & strSourceFileBaseName & ".csv"' 将活动工作表保存为 CSV 格式objWorkbook.ActiveSheet.SaveAs strCsvFilePath, 6 ' 6 表示 csv 格式' 关闭 Excel 文件和应用程序objWorkbook.Close False ' False 表示不保存更改objExcel.Quit' 释放对象Set objWorksheet = NothingSet objWorkbook = NothingSet objExcel = Nothing' 显示完成提示MsgBox "转换完成"
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。