回复 13# myh17909
按资源管理器排序是个问题,与批处理排序规则不同,很麻烦的... 尤其是你这种要求,按序号排序.- <#*,:&cls
- @echo off
- REM 这里是指目录下所有文件全部重命名.新文件名与旧文件名是一一对应的,顺序不能改变.
- REM 旧文件名集合是按照for列出的顺序.新文件名集合是自定义数组.
- REM 此法重命名次数最多
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
- set 001=语文
- set 002=数学
- set 003=英语
- set 004=科学
- for /d %%A in (*) do (
- if defined %%~nxA call :rentemp "%%~fA" "!%%~nxA!"
- )
-
- endlocal
- pause
- exit /b
-
- :rentemp
- setlocal enabledelayedexpansion
- pushd "%~1"|| goto end
- set n=0
- @REM 按Explorer排序规则排序
- for /f "delims=" %%A in ('dir /ad /b^|powershell -c "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String))) $input"') do (
- REM 必须一一对应,如果新文件名已存在,则必须重命名为临时文件名,并保存到临时列表,数据格式:oldname/tempname/targetname
- set /a n+=1
- set "newname=%~2 (!n!)"
- if not "!newname!"=="%%A" (
- if exist "!newname!" (
- call :getTempName
- @REM echo ren "%%A" "!tempname!"
- >nul 2>nul ren "%%A" "!tempname!" && set "templist[!n!]=%%A/!tempname!/!newname!"
- ) else (
- echo ren "%%A" "!newname!"
- ren "%%A" "!newname!"
- )
- )
- )
- REM 重命名临时列表
- for /f "tokens=1* delims==" %%A in ('2^>nul set templist[') do (
- for /f "tokens=1-3 delims=/" %%C in ("%%B") do (
- echo ren "%%C" "%%E"
- ren "%%D" "%%E"
- )
- )
- popd
- :end
- endlocal
- exit /b
-
- :getTempName
- :loop
- set tempname=~%random%~
- if exist "%tempname%" goto loop
- @REM echo tempname=%tempname%
- exit /b
- #>
- param([psobject]$InputObject)
-
- Add-Type -TypeDefinition @'
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
-
- namespace MyCode
- {
- public class ExplorerViewComparer : Comparer<string>
- {
- [DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogical", CharSet = CharSet.Auto)]
- internal static extern int StrCmpLogical(string p1, string p2);
-
- public override int Compare(string x, string y)
- {
- return StrCmpLogical(x, y);
- }
- }
- }
- '@
- # create comparer object
- $ExpComp = New-Object MyCode.ExplorerViewComparer
- $arrstr = [string[]]@($InputObject)
- [Array]::Sort($arrstr, $ExpComp)
- $arrstr
- trap {continue}
复制代码
|