标题: [文件操作] 【分享】遍历多个关键词文件并移入指定名称文件夹,并做二次分类 [打印本页]
作者: zhengzhenhui 时间: 2024-8-17 18:55 标题: 【分享】遍历多个关键词文件并移入指定名称文件夹,并做二次分类
本帖最后由 zhengzhenhui 于 2024-8-17 20:04 编辑
遍历当前文件夹下的所有目录的多个关键字移入指定名称的文件夹,没有则创建- @echo off
- setlocal EnableDelayedExpansion
- cd /d "%~dp0"
- rem 定义要匹配的关键字和目标文件夹
- set "keywords=周杰伦 林俊杰 张惠妹 刘德华"
- set "targetFolder=爱听的文件夹"
- if not exist "%targetFolder%" (
- mkdir "%targetFolder%"
- )
- for /r %%a in (*.*) do (
- set "fileName=%%~nxa"
- for %%k in (%keywords%) do (
- if /i "!fileName!" neq "!fileName:*%%k=!" (
- echo Moving "%%a" to "%targetFolder%"
- move "%%a" "%targetFolder%" >nul 2>&1 || echo Error moving file: "%%a"
- )
- )
- )
-
- endlocal
复制代码
作者: zhengzhenhui 时间: 2024-8-17 19:04
方案2
遍历到关键词后移入文件夹,并做二次分类- @echo off
- setlocal EnableDelayedExpansion
- cd /d "%~dp0"
- rem 定义要匹配的关键字和目标文件夹
- set "keywords=周杰伦 林俊杰 张惠妹 刘德华"
- set "targetFolder=爱听的文件夹"
- if not exist "%targetFolder%" (
- mkdir "%targetFolder%"
- )
- for /r %%a in (*.*) do (
- set "fileName=%%~nxa"
- for %%k in (%keywords%) do (
- if /i "!fileName!" neq "!fileName:*%%k=!" (
- echo Moving "%%a" to "%targetFolder%"
- move "%%a" "%targetFolder%"
- )
- )
- )
-
- for %%k in (%keywords%) do (
- set "subFolder=%targetFolder%\%%~k"
- if not exist "!subFolder!" (
- mkdir "!subFolder!"
- )
- )
-
- for /r "%targetFolder%" %%b in (*) do (
- set "fileName=%%~nb"
- for %%k in (%keywords%) do (
- if /i "!fileName!" neq "!fileName:*%%k=!" (
- echo Moving "%%b" to "%targetFolder%\%%k"
- move "%%b" "%targetFolder%\%%k"
- )
- )
- )
-
- endlocal
复制代码
作者: 77七 时间: 2024-8-18 19:57
- @echo off
- cd /d "%~dp0"
- rem 保存为ansi
- set "targetFolder=爱听的文件夹"
- md "%targetFolder%" 2>nul
- if exist "%targetFolder%\" (
- for /f "delims=" %%d in ("%cd%\%targetFolder%") do (
- for /r %%i in (*周杰伦*.mp3 *林俊杰*.mp3 *张惠妹*.mp3 *刘德华*.mp3) do (
- if /i "%%~dpi" neq "%%~fd\" (
- move "%%i" "%%~fd\"
- )
- )
- )
- ) else (
- echo 文件夹创建失败
- )
- pause
复制代码
作者: zhengzhenhui 时间: 2024-8-18 20:36
回复 3# 77七
我又换了个思路,通过读取TXT换行的内容,这样方便输入,但是txt里面为什么要ANSI的,麻烦帮我解惑,这个是查阅了资料缝合出来的太长了,看帮我个减肥- @echo off
- setlocal EnableDelayedExpansion
- cd /d "%~dp0"
-
- rem 从 keywords.txt 文件中读取关键字
- set "keywordsFile=关键词.txt"
- set "keywords="
- for /f "delims=" %%k in (%keywordsFile%) do set "keywords=!keywords! %%k"
-
- rem 定义目标文件夹
- set "targetFolder=爱听的文件夹"
- if not exist "%targetFolder%" mkdir "%targetFolder%"
-
- for /r %%a in (*.*) do (
- set "fileName=%%~nxa"
- for %%k in (%keywords%) do (
- if /i "!fileName!" neq "!fileName:*%%k=!" (
- echo Moving "%%a" to "%targetFolder%"
- move "%%a" "%targetFolder%"
- )
- )
- )
-
- for %%k in (%keywords%) do (
- set "subFolder=%targetFolder%\%%~k"
- if not exist "!subFolder!" mkdir "!subFolder!"
- )
-
- for /r "%targetFolder%" %%b in (*) do (
- set "fileName=%%~nb"
- for %%k in (%keywords%) do (
- if /i "!fileName!" neq "!fileName:*%%k=!" (
- echo Moving "%%b" to "%targetFolder%\%%k"
- move "%%b" "%targetFolder%\%%k"
- )
- )
- )
-
- endlocal
复制代码
作者: 77七 时间: 2024-8-19 01:23
回复 4# zhengzhenhui
默认的936代码页对应 ansi批处理、文本
65001代码页对应 utf-8批处理、文本
- @echo off
- cd /d "%~dp0"
- rem 保存为ansi
- set "targetFolder=爱听的文件夹"
- set "keywordsFile=关键词.txt"
- md "%targetFolder%" 2>nul
- if exist "%targetFolder%\" (
- for /f "delims=" %%d in ("%cd%\%targetFolder%") do (
- for /f "useback delims=" %%a in ("%keywordsFile%") do (
- for /r %%i in ("*%%a*.mp3") do (
- if /i "%%~dpi" neq "%%~fd\%%a\" (
- md "%%~fd\%%a\" 2>nul
- if exist "%%~fd\%%a\" (
- move "%%i" "%%~fd\%%a\"
- ) else (
- echo [%%~fd\%%a\] 创建失败
- )
- )
- )
- )
- )
- ) else (
- echo [%targetFolder%\] 创建失败
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2024-8-19 08:46
本帖最后由 qixiaobin0715 于 2024-8-19 09:25 编辑
回复 4# zhengzhenhui
实际上代码第2、3行变量设置没必要,又不是很复杂,多此一举,直接写到下面代码里即可,又能减两行- @echo off
- set "KeyWordsFile=KeyWords.txt"
- set "TargetFolder=FavoriteSong"
- for /f "delims=" %%i in (%KeyWordsFile%) do (
- for /f "delims=" %%j in ('dir /s /b /a-d "*%%i*.mp3" 2^>nul') do (
- md "%TargetFolder%\%%i" 2>nul
- move "%%j" "%TargetFolder%\%%i\">nul
- )
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2024-8-19 13:36
这样也行:- @echo off
- for /f "delims=" %%i in (KeyWords.txt) do (
- if exist "*%%i*.mp3" (
- md "FavoriteSong\%%i" 2>nul
- move "*%%i*.mp3" "FavoriteSong\%%i\">nul
- )
- )
- for /f "delims=" %%i in ('dir /b /s /ad') do (
- for /f "delims=" %%j in (KeyWords.txt) do (
- if exist "%%i\*%%j*.mp3" (
- md "FavoriteSong\%%j" 2>nul
- move "%%i\*%%j*.mp3" "FavoriteSong\%%j\">nul
- )
- )
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2024-8-19 14:27
本帖最后由 qixiaobin0715 于 2024-8-19 14:38 编辑
回复 1# zhengzhenhui
自己用倒是无所谓,既然是【分享】,对于常见的情况楼主应当考虑的全面一些,比如出现重名文件,两个关键词有包含关系(周杰,周杰伦)等问题如何处理。
作者: zhengzhenhui 时间: 2024-8-19 16:09
回复 8# qixiaobin0715
哈哈,我这也是找资料硬凑优化的,长度有点罗里吧嗦的,想着不影响实际流程大家出手给简化一下也方便学习
作者: qixiaobin0715 时间: 2024-8-23 09:32
本帖最后由 qixiaobin0715 于 2024-8-23 15:37 编辑
回复 9# zhengzhenhui
分享应当是基于成熟的想法写出的代码,否则易对不明真相的人产生误导作用。
作者: aloha20200628 时间: 2024-8-23 11:40
回复 4# zhengzhenhui
用系统内置命令 forfiles 将4楼代码减肥到 ‘底’ ...
- @echo off &setlocal &set "targetFolder=%cd%\爱听的文件夹"
- for /f "delims=" %%w in (关键词.txt) do (
- md "%targetFolder%\%%~w" 2>nul
- forfiles /s /m "*%%~w*.mp3" /c "cmd /c move /y @path \"%targetFolder%\%%~w\\"" >nul 2>nul
- )
- endlocal &pause &exit/b
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |