标题: [文件操作] 新手赐教批处理根据文件名创建文件夹并移动文件 [打印本页]
作者: oepcih 时间: 2022-5-9 15:00 标题: 新手赐教批处理根据文件名创建文件夹并移动文件
当前文件夹内文件名称示例:
通知:12345(办公室).doc
通知:23456(财务部).doc
通知:34567(后勤部).docx
通知:45678(办公室).docx
通知:56789(财务部).docx
根据以上示例,创建名为“办公室”的文件夹,把办公室的2份通知文件放入该文件夹,财务部的文件同理,但后勤部不创建、不移动,即2个文件以上的部门创建并移动到文件夹,1个文件的部门则不用
目前的代码:- @echo off
- for /f "delims=(). tokens=2" %%b in ('dir /b *.doc *.docx') do (
- echo %%a %%b %%c %%d
- if not exist "%%b\" md "%%b"
- move "%%a(%%b)%%c.%%d" "%%b\"
- )
- pause
复制代码
目前面临的问题:目前已成功创建文件夹,但无法移动,显示”系统找不到指定的文件
下一步问题:如何实现2个文件以上的部门创建文件夹,1个文件的部门不用处理
作者: flashercs 时间: 2022-5-9 16:49
- @echo off
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
-
- for /f "delims=" %%a in ('dir /a-d /b *.doc') do (
- for /f "tokens=2 delims=()" %%b in ("%%a") do (
- set group_%%b=!group_%%b!,"%%a"
- set /a "count_%%b+=1"
- )
- )
-
- for /f "tokens=1* delims==" %%a in ('set count_') do (
- if %%b geq 2 (
- for /f "tokens=1* delims=_" %%c in ("%%a") do (
- md "%%d\" 2>nul
- for %%e in (!group_%%d!) do (
- echo move %%e "%%d\"
- move %%e "%%d\"
- )
- )
- )
- )
-
- endlocal
- pause
- exit /b
复制代码
作者: qixiaobin0715 时间: 2022-5-10 09:43
回复 1# oepcih - @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=2 delims=()" %%a in ('dir /b /a-d *.doc') do set /a "_%%a+=1"
- for /f "tokens=1* delims==_" %%i in ('set _') do (
- if %%j neq 1 (
- md "%%i"
- move "*%%i*.doc?" "%%i">nul
- )
- )
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |