bat批处理文件夹下所有子文件夹下最后一个的文件重命名?

文件夹下的每个子文件夹最后一个的文件重命名为-00编号

第1个回答  2022-05-12
不清楚你的实际文件/情况,仅以问题中的说明及猜测为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的多个文件夹放一起双击运行<# :
cls&echo off&cd /d "%~dp0"&mode con lines=5000
rem 在当前目录下所有子文件夹里按顺序排在最后一个的文件名称末尾添加指定字符内容
set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
set "current=%cd%"
powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
echo;%#% +%$%%$%/%_% %z%
pause
exit
#>
$codes=@'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static class ExpDir
{
    [DllImport("Shlwapi.dll", CharSet=CharSet.Unicode)]
    public static extern int StrCmpLogicalW(string p1, string p2);
    public static string[] Sort(string[] f)
    {
        Array.Sort(f, StrCmpLogicalW);
        return f;
    }  
}
'@;
Add-Type -TypeDefinition $codes;

$b=[Convert]::FromBase64String("IC0tPiA=");
$c=[Text.Encoding]::Default.GetString($b);
$suffix='-00';
$current=$env:current;
$folders=@(dir -literal $current -recurse|?{$_ -is [System.IO.DirectoryInfo]});
for($i=0;$i -lt $folders.length;$i++){
    $files=@(dir -literal $folders[$i].FullName|?{$_ -is [System.IO.FileInfo]});
    if($files.length -ge 1){
        $arr=[ExpDir]::Sort($files);
        $oldfile=get-item -literal ($folders[$i].FullName+'\'+$arr[$arr.count-1]);
        $base=$oldfile.BaseName -replace ([regex]::Escape($suffix)+'$'),'';
        $oldfile.FullName.replace($current,'')+$c+$base+$suffix+$oldfile.Extension;
    }
}

相似回答