WHY 当前离线
上校
@if(0)==(0) echo off pushd "D:\Test" for /f "tokens=1*delims=|" %%i in ('dir /b *_*.pdf ^| cscript -nologo -e:jscript "%~f0"')do ( echo ren "%%i" "%%j" ) pause & exit @end var max = 0, arr = []; var reg = /_([0-9]+)(?=\....$)/; while (!WSH.StdIn.AtEndOfStream) { var file = WSH.StdIn.ReadLine(); var m = file.match(reg); if (!m) continue; if (max < m[1].length) max = m[1].length; arr.push(file); } var zero = new Array(max).join('0'); var reg1 = new RegExp('_[0-9]{' + max + '}(?=\....$)'); for (var i = 0; i < arr.length; i++) { if (arr[i].match(reg1)) continue; var newFile = arr[i].replace( reg, function(s0,s1){return '_' + (zero + s1).slice(-max)} ) WSH.Echo(arr[i] + '|' + newFile); }复制代码
TOP
buyiyang 当前离线
中尉
(echo,a&echo,b)>1.txt 61 0D 0A 62 0D 0A复制代码
(echo,a & echo,b ) > 1.txt复制代码
@echo off >&3 3>1.txt echo,a&echo,b复制代码
示例: (echo test)|findstr /c:" " 效果:显示test,findstr认为前面的命令输出含有空格 注释:CMD的预处理在分析语句时,会在)和|这些有特殊意义的转义字符前后插入空格,同时剪除多余的空格。 而当)和|一同出现时,CMD的预处理没有把这些空格从命令行中全部过滤掉,而会遗漏一个空格到echo命令中。 而echo命令则把这个空格连通之前文本一同输出到管道后命令findstr中,所以导致findstr匹配出含空格行。 链接:http://www.bathome.net/viewthrea ... amp;page=3#pid50356 http://www.bathome.net/viewthrea ... amp;page=4#pid28166
评分人数
qixiaobin0715 当前离线
大校
@echo off set n=0 setlocal enabledelayedexpansion for /f "tokens=2 delims=_" %%i in ('dir /b /a-d *_*.pdf') do ( if %%~ni gtr !n! set n=%%~ni ) for /l %%i in (1,1,9) do set n=!n:%%i=0! set n=1!n! for /f "tokens=1* delims=_" %%i in ('dir /b /a-d *_*.pdf') do ( set m=%%~nj set /a m+=n ren "%%i_%%j" "%%i_!m:~1!%%~xj" ) pause复制代码
hfxiang 当前离线
上尉
@rem 拆分文件补全序号位数,位数修改(-v"N=03")即可,如4位则改为(-v"N=04"),类推... @echo off&cd /d "%~dp0" dir /b *.pdf|gawk -v"N=03" -F"[_.]" "{o=$0;$2=sprintf(\"%%\"N\"d\",$2);print \"move /Y \\\"\" o \"\\\" \\\"\" $1 \"_\" $2 \".\" $3 \"\\\"\"}"|cmd.exe复制代码
qd2024 当前离线
三级士官
newswan 当前离线
$folder = "1" $file = Get-ChildItem -path $folder -Filter "*.pdf" | Sort-Object { ($_.BaseName -replace "^.+_") -as [int] } $lenMax = ( ( $file | ForEach-Object { ( ($_.BaseName -replace "^.+_").tostring() ).length } ) | Sort-Object )[-1] $file | ForEach-Object { $arr = $_.basename -split "_" if ( $arr[1].length -lt $lenMax ) { $newName = $arr[0] + "_" + ( "{0:d$lenmax}" -f [int]$arr[1] ) + $_.extension Rename-Item $_.fullname -newname $newName } }复制代码
aloha20200628 当前在线
@echo off &setlocal enabledelayedexpansion dir /b/s/a-d "%~1\*.mp4">0.0 & (call :getMax 0.0 m) (endlocal & set "max=%m%") & setlocal enabledelayedexpansion for /L %%n in (1,1,%max%) do set "p=0!p!" for /f "delims=" %%F in (0.0) do ( set "xF=%%~xF" & for /f "tokens=1-2 delims=." %%1 in ("%%~nF") do ( (call :cutP "%%~2" %max% s1 s2) & set "q=!p!!s1!" & ren "%%~F" "!q:~-%max%!!s2!!xF!") ) del 0.0 & endlocal & exit/b :getMax // %1=listF %2=digits :[loop] set/a "k+=1" & set "n=!n![0-9]" & findstr "\.!n![^0-9]" "%~1">nul && goto[loop] || (set/a "%~2=k" & exit/b) :cutP // %1=nameF %2=max %3=s1 %4=s2 (set "s=%~1" & for /L %%n in (%~2,-1,0) do if "!s:~%%n,1!" geq "0" if "!s:~%%n,1!" leq "9" ( set/a "k=%%n+1" & for %%k in (!k!) do (set "%~3=!s:~,%%k!" & set "%~4=!s:~%%k!") & exit/b)) exit/b复制代码
$srcPath = 'D:\Test\'; $max = 0; $files = dir -Path $srcPath -Filter *_*.pdf; for ($i = 30; $i -ge 1; $i--) { $reg = '_[0-9]{' + $i + '}$'; if (($files.BaseName -match $reg).Count -gt 0) {$max = $i; break;} } $reg = '_[0-9]{1,' + ($max-1) + '}$'; $files | ?{$_.BaseName -match $reg} | ren -NewName { $arr = $_.BaseName.Split('_'); $arr[-1] = $arr[-1].PadLeft($max, '0'); ($arr -join '_') + $_.Extension; } -whatIf pause复制代码
ppll2030 当前离线
四级士官
@echo off setlocal enabledelayedexpansion for %%i in (1 10 100 1000 10000) do if exist %%i.*.pdf set n=%%i echo %n% set /a n*=10 for /f "tokens=1-3 delims=." %%i in ('dir /b /a-d *.*.pdf') do ( set m=%%i set str=%%j&set str=!str:*%%i=! set /a m+=n REM 执行更名请去除echo echo ren "%%i.%%j.%%k" "!m:~1!!str!.%%k" ) pause&exit复制代码
set str=!str:%%i=!复制代码
set str=!str:*%%i=!复制代码