大概是这样子的,不知道如何定义文件和文件夹相同的关键字- @echo off
- setlocal EnableDelayedExpansion
-
- rem 设置源目录和目标目录
- set "sourceFolder=C:\Users\Administrator\Desktop\测试\1移动文件"
- set "targetFolder=C:\Users\Administrator\Desktop\测试\2目的地"
-
- rem 定义关键词
- set "keyword=赵小强"
-
- rem 遍历源目录下的文件
- for /r "%sourceFolder%" %%f in (*.*) do (
- set "fileName=%%~nxf"
- if not "!fileName!"=="!fileName:*%keyword%=!" (
- rem 在目标目录下查找包含关键词的文件夹
- for /d %%d in ("%targetFolder%\*%keyword%*") do (
- set "targetSubFolder=%%d"
- echo Moving "%%f" to "!targetSubFolder!"
- move "%%f" "!targetSubFolder!" >nul 2>nul
- if!errorlevel! NEQ 0 (
- echo Failed to move "%%f" to "!targetSubFolder!". Error code:!errorlevel!
- rem 尝试处理一些常见错误
- if!errorlevel! equ 2 (
- echo Permission denied. Check folder permissions.
- ) elseif!errorlevel! equ 3 (
- echo File not found. Double-check the source file path.
- ) elseif!errorlevel! equ 5 (
- echo Access denied. Check file permissions.
- ) else (
- echo Unknown error occurred. Error code:!errorlevel!
- )
- )
- )
- )
- )
-
- echo All files have been processed.
- pause
- endlocal
复制代码
|