标题: [文本处理] 批处理将目录下txt文件移动到以txt文件第一个连续字母命名的文件夹中 [打印本页]
作者: cckkaa888 时间: 2024-6-10 19:47 标题: 批处理将目录下txt文件移动到以txt文件第一个连续字母命名的文件夹中
将目录下txt文件移动到该目录下子文件夹{以txt文件第一个连续字母命名的,连续字母不包含一个字母的}中,如果没有就创建这个文件夹并且移动。
例如
将"123_zhangsan456@张三abc.tx"t移动到"zhangsan"文件夹中,
将"h-123lisi@李四.txt"移动到"lisi"文件夹中
作者: 77七 时间: 2024-6-10 21:24
本帖最后由 77七 于 2024-6-11 12:34 编辑
- @echo off
- cd /d "%~dp0"
- set az=qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM
- for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
- set f=%%i
- call :1 "%%~ni"
- )
- pause
- exit
-
- :1
- set "str=/%~1\"
- for /f "tokens=1* delims=%az%" %%a in ("%str%") do (
- set str_s=%%a
- set str_e=%%b
- )
- call set "str_m=%%str:%str_s%=%%"
- if "%str_e%" neq "" (
- call set "str_m=%%str_m:%str_e%=%%"
- )
- if "%str_m%" neq "" (
- if "%str_m:~1%" equ "" (
- if "%str_e%" neq "" (
- call :1 "%str_e%"
- )
- ) else (
- md "%str_m%" 2>nul
- echo move "%f%" "%str_m%\"
- move "%f%" "%str_m%\"
- )
- )
- exit /b
复制代码
作者: aloha20200628 时间: 2024-6-10 22:07
本帖最后由 aloha20200628 于 2024-6-10 22:35 编辑
回复 1# cckkaa888
给一个直接用递归方法 ‘截取文件名第一个多字母字段’ 的版本
以下代码存为 test.bat 置于目标文件(*.txt)所在目录下运行...
- @echo off
- for /f "delims=" %%a in ('dir /b/a-d *.txt') do (
- set "s=%%a"&set "$a=%%a" &setlocal enabledelayedexpansion
- set "_s="&set "_k=" & (call :getstr)
- md "!_s!" 2>nul & move /y "!$a!" "!_s!\" & endlocal
- )
- pause&exit/b
- :getstr
- set "c=!s:~,1!" &if "!c!" geq "a" if "!c!" leq "Z" (
- set "s=!s:~1!"&set "_s=!_s!!c!"&set/a "_k+=1" & goto :getstr)
- if !_k! geq 2 (exit/b) else (set "s=!s:~1!"&set "_s="&set "_k=" & goto :getstr)
- exit/b
复制代码
作者: 77七 时间: 2024-6-11 15:09
回复 2# 77七
去除多余判断,优化下逻辑...- @echo off
- cd /d "%~dp0"
- set az=qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM
- for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
- set f=%%i
- call :1 "%%~ni"
- )
- pause
- exit
-
- :1
- set "str=/%~1\"
- for /f "tokens=1* delims=%az%" %%a in ("%str%") do (
- if "%%b" neq "" (
- call set "str=%%str:%%a=%%"
- call set "str=%%str:%%b=%%"
- setlocal enabledelayedexpansion
- if "!str:~1!" equ "" (
- endlocal
- call :1 "%%b"
- ) else (
- md "!str!" 2>nul
- echo move "!f!" "!str!\"
- move "!f!" "!str!\"
- endlocal
- )
- )
- )
- exit /b
复制代码
作者: newswan 时间: 2024-6-11 18:45
- echo "a123bcd4sd" | grep -Eo "[a-zA-Z]{2,}"
复制代码
作者: newswan 时间: 2024-6-11 18:50
本帖最后由 newswan 于 2024-6-12 09:36 编辑
Powershell- $sour = "D:\work"
- Push-Location -Paht $sour
- Get-ChildItem -Filter "*.txt" | ForEach-Object {
- $str = $_.BaseName -replace ".*?([a-zA-Z]{2,}).*",'$1'
- if ( -Not ( Test-Path $str ) ) {
- New-Item -Type Directory -Path $str
- }
- Move-Item -Path $_ -Destination $str
- }
- Pop-Location
复制代码
作者: aloha20200628 时间: 2024-6-12 11:17
本帖最后由 aloha20200628 于 2024-6-12 15:17 编辑
凡是提供正则匹配替换方法的脚本(如jscript/vbs/powershell/python/...)均可增援纯P代码以便其整个脚本代码轻量化...
以下代码调用jscript正则替换方法,存为test.bat,置于目标文件(*.txt)所在目录下运行...
- @set @x=1 /* &@echo off
- for /f "delims=" %%a in ('dir /b/a-d *.txt') do for /f "delims=" %%d in (
- ' cscript /nologo /e:jscript "%~f0" "%%a" ') do (md "%%d" 2>nul & move /y "%%a" "%%d\")
- pause&exit/b */
- v = WSH.arguments; WSH.echo(v(0).match(/[a-z]{2,}/i)[0]); WSH.quit();
复制代码
作者: WHY 时间: 2024-6-13 08:09
本帖最后由 WHY 于 2024-6-13 08:13 编辑
- PowerShell "dir *.txt -file | ?{$_.BaseName -match '[a-z]{2,}'} | forEach {$fd=$matches[0]; md $fd -ea SilentlyContinue; mv $_.FullName -Dest $fd}"
复制代码
- @if(0)==(0) echo off
- for /f "tokens=1*" %%i in ('dir /b /a-d *.txt ^| cscript -nologo -e:jscript "%~f0"') do (
- md %%i\ 2>nul
- move "%%j" %%i\
- )
- pause & exit/b
- @end
-
- while (!WSH.StdIn.AtEndOfStream) {
- var f = WSH.StdIn.ReadLine();
- var m = f.slice(0,-3).match(/[a-z]{2,}/i);
- if(m) WSH.Echo(m[0] + ' ' + f);
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |