[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
回复 6# ziranww

a.bat
  1. @echo off
  2. set "dir=E:\test"
  3. powershell.exe -Command "(Select-String -Path '%dir%\*.*' -Pattern '(?<=model_name\x00)[^\x00]+(?=\x00.*?disp_out1_info)' -Encoding default)|Foreach-object{Rename-Item -LiteralPath $_.Path -NewName $_.Matches[0].Value}"
复制代码

TOP

好像没有规律可循,不好办;因为乱码部分是随机乱码。

TOP

本帖最后由 flashercs 于 2019-1-31 09:58 编辑

由NULxE0NUL判断
这是utf8编码打开npp
而utf8编码下,\xE0\x00不存在编码位置,故替换为\xFFFD;
其他乱码同理:
所以文本分隔符应该是控制字符的[\x00-0x1f]和替换符\xFFFD
  1. @echo off
  2. set "dir=E:\test"
  3. powershell.exe -Command "(Select-String -Path "%dir%\*.*" -Pattern '(?<=model_name[\x00-\x1F\uFFFD]+)[^\x00-\x1F\uFFFD]+(?=[\x00-\x1F\uFFFD]*disp_outl_info)' -Encoding utf8)|Foreach-object{Rename-Item -LiteralPath $_.Path -NewName ($_.Matches[0].Value+[System.IO.Path]::GetExtension($_.Path))}"
复制代码

TOP

14楼修改了

TOP

回复 19# ziranww
  1. @echo off
  2. set "dir=E:\test"
  3. powershell -Command "(Select-String -Path '%dir%\*.*' -Pattern '(?<=model_name[\x00-\x1F\uFFFD]+)[^\x00-\x1F\uFFFD]+(?=[\x00-\x1F\uFFFD]*disp_outl_info)' -Encoding utf8)|ForEach-Object {$baseName = $_.Matches[0].Value + [System.IO.Path]::GetExtension($_.Path); $i = 0; $newName = $baseName; while ($true) {try {Rename-Item -LiteralPath $_.Path -NewName $newName -ErrorAction Stop; break; }catch {$newName = $baseName + '.' + (++$i); }}}"
复制代码

TOP

本帖最后由 flashercs 于 2019-1-31 19:42 编辑

加个进度看看吧,不知处理到哪里了。
  1. @echo off
  2. REM UTF8编码,过滤控制字符和UTF8不能识别的字符后,选择字符串作为文件名并重命名文件。
  3. set "dir=%~dp0."
  4. powershell -Command "foreach ($item in (Get-ChildItem -LiteralPath \"%dir%\" -Filter *.* -File)) { if ([System.IO.File]::ReadAllText($item.FullName, [System.Text.Encoding]::UTF8) -match '(?<=model_name[\x00-\x1F\uFFFD]+)[^\x00-\x1F\uFFFD]+(?=[\x00-\x1F\uFFFD]*disp_outl_info)') { $baseName = $Matches[0] + $item.Extension; $i = 0; $newName = $baseName; while (Test-Path -LiteralPath (Join-Path -Path $item.DirectoryName -ChildPath $newName)) { $newName = $baseName + '.' + (++$i);} Write-Host 'Renaming file ' -NoNewline; Write-Host $item.FullName -NoNewline -ForegroundColor Green; Write-Host ' to ' -NoNewline; Write-Host $newName -ForegroundColor Green; Rename-Item -LiteralPath $item.FullName -NewName $newName; }}"
  5. pause
  6. exit /b
复制代码

TOP

回复 24# ziranww

                                      
已修改

TOP

回复 26# ziranww


    测试目录测试一下!
默认筛选的是 *.*

TOP

回复 29# ziranww

RenameFiles.bat
  1. @echo off
  2. REM 重命名文件为其扩展名序号+1
  3. set "dir=E:\Dest\dest3"
  4. Powershell -Command "(Get-ChildItem -LiteralPath \"%dir%\" -Filter '*.*' -File|ForEach-Object {$ext = $_.Extension.Substring(1); if ($ext -match '^\d+$') {$id = [int]$ext}else {$id = 0}[PSCustomObject]@{'id' = $id; 'fileInfo' = $_}})|Sort-Object -Descending -Property 'id'|ForEach-Object {if ($_.id -ne 0) {$newName = $_.fileInfo.BaseName}else {$newName = $_.fileInfo.Name}$newName = $newName + '.' + ($_.id + 1); Write-Host ('Rename File \"' + $_.fileInfo.FullName + '\" to \"' + $newName + '\"'); Rename-Item -LiteralPath $_.fileInfo.FullName -NewName $newName}"
  5. pause
  6. exit /b
复制代码

TOP

返回列表