@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
2、拖入文件,为文件增加“rev_”前缀,可用于标注版本、回复同事等。@echo offsetlocal enabledelayedexpansion:: 获取拖入的文件路径和文件名set "filepath=%~1"set "filename=%~n1"set "fileext=%~x1"ren "%filepath%" "rev_%filename%%fileext%"
3、上面那个没办法批量实现,支持批量的如下,想加什么前缀自己改。@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)
2、识别11位数字号,并移动excel and pdf到根据11位数字号创建的文件夹中@echo offsetlocal EnableDelayedExpansionset ORDER_NO_LENGTH=11set LOG_FILE=order_sort_log.txtecho 开始运行 > "%LOG_FILE%"for %%f in (*.xlsx *.pdf) do ( set name=%%~nxf set 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]" >nul if not errorlevel 1 ( set folder=前缀_!prefix!_尾缀 if not exist "!folder!" md "!folder!" move "%%f" "!folder!\" >nul echo 移动 %%f >> "%LOG_FILE%" ) else ( echo 跳过 %%f >> "%LOG_FILE%" ))echo 完成 >> "%LOG_FILE%"pause
最后,保存cmd文件之后,一定记得用 ANSI 另存为一下。另外,R语言读取df文件的时候,只推荐使用baseR中的函数读取csv文件,而不要用各种第三方R包读取excel格式。这里分享一个vbs脚本,拖入excel之后,可实现excel_2_csv文件的转换,然后再用R读取。' 创建一个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 "转换完成"