如何读取文件名称的最后一个字符并通过运行批处理文件重命名它们

如何读取文件名称的最后一个字符并通过运行批处理文件重命名它们

我的文件夹中有 3 个文件,分别是:

XXX_a.txt
XXX_b.txt
XXX_c.txt

文件名可以是多变,仅 3 个文件的最后一个字符 abc没有改变

我需要将文件重命名如下:

XXX_a.txt to first.log
XXX_b.txt to second.log
XXX_c.txt to third.log

如何编写批处理文件来读取每个文件名称的最后一个字符,然后分别重命名?

非常感谢您的帮助,谢谢。

答案1

您可以使用通配符重命名:

rename *_a.txt first.log
rename *_b.txt second.log
rename *_c.txt third.log

假设只有一个 _a、_b 和 _c 文件。

答案2

在此处输入图片描述


  • 这个答案旨在展示如何使用批处理循环和控制台可执行文件重命名文件在运行时编译本身

  • 以编程方式重复命令将负责为已经包含一系列范围的文件分配名称*_[a-z-0-9]

  • 本例中提供了两个选项,请按基数或字面意思命名:
  • 还旨在防止当有多个文件具有相同字母时发生错误:

  • 如果有多个文件具有相同的字母,也可以防止发生错误:

XXX_a.txt First.log
YYY_a.txt Second.log
XXX_b.txt Third.log
YYY_b.txt Fourth.log
XXX_c.txt Fifth.log
YYY_c.txt Sixth.log
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe -w:0 -unsafe+ -codepage:1252 -t:exe -platform:anycpu -out:"Q1594189_Cardinal.exe" "Q1594189_Cardinal.cmd" -o -nologo

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe -w:0 -unsafe+ -codepage:1252 -t:exe -platform:anycpu -out:"Q1594189_Literal.exe" "Q1594189_Literal.cmd" -o -nologo
  • csc.exe使用/测试了以下编译器[Windows 10 pro 2004]:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
  • 您可以在命令行中使用以下命令检查可用的 csc.exe 版本:
>for /f tokens^=* %c in ('where /r %windir%\Microsoft.NET csc.exe^|findstr.exe /i k.*v')do @echo\%c

rem :: returns/outputs :: 
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
  • 此脚本会自动删除执行时生成的文件
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"
  • 文件"%tmp%\_bs.bin"是通过解码 base64 中的字符串生成的,删除了上面的 2 行,删除了用于标记注释的转义符
/* 2>nul || @echo off

在此处输入图片描述

  • 观察:字符串来源@LotPings在此回答

  • 文件"!_path!\%~n0.exe"是一样的path\Q1594189_Cardinal.exe或者"path\Q1594189_Literal.exe"分别是运行时获得的可执行文件的名称,其中%~n0是所用球棒名称与.exe,形成my_file.bat==>>my_file.exe

  • 执行时生成的 exeucatel 文件接受 1 个数字参数(int)并分别返回相应的基数或文字字符串:

C:\>Q1594189_Cardinal.exe 11 
Eleventh

C:\>Q1594189_Literal.exe 11
Eleven
  • 要保留其中一个文件,只需从命令中将其删除del
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"

del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"
  • 要保留两者,只需删除命令del,并且一个或两个文件的存在不会干扰主要执行:
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"

  • 它也已经过测试并且仅适用于 3 个文件......

.\Q1594189_Literal.cmd :

/* 2>nul || @echo off

cd /d "%~dp0" && title <nul && title ...\%~nx0
call %:^) && cd /d "%~dp0" && setlocal EnableDelayedExpansion
<con: echo\ && set "_path=!cd!" && pushd %__APPDIR__% && color 0a

set "_flag_c#=-w:0 -unsafe+ -codepage:1252 -t:exe -platform:anycpu" 
set "_flag_c#=!_flag_c#! -out:"!_path!\%~n0.exe" "!_path!\%~nx0" -o -nologo"

for /f tokens^=* %%c in ('where /r ..\Microsoft.NET csc.exe^|findstr.exe /i k.v4
')do <con: cmd.exe /v:on /e:on /s /c ""%%~c" !_flag_c#!" && goto %:^] || goto %:^V

%:^V
timeout.exe /t -1 | echo\ Something's really wrong here^!!. && type "%tmp%\_bs.bin"
<con: ^< nul del/q /f "%tmp%\_bs.bin" "%~dpn0.exe" 2>nul & popd & endlocal & goto :EOF

%:^)
2>nul "%__APPDIR__%Certutil.exe" -decode -f "%~dpnx0" "%temp%\_bs.bin" >nul || goto %:^v
for /f tokens^=* %%i in ('type "%temp%\_bs.bin"')do set "_bs=%%~i" && echo\%%~i && exit /b
rem :: <-----BEGIN ...\_bs.bin ----- G1sxRhtbMEobWzFGG1swSg0K== -----END ...\_bs.bin -----> :: 

%:^]
set "_cnt=0" && set "_range=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9"
for %%A in (!_range!)do for /f eol^=^|delims^= %%G in ('2^>nul %__APPDIR__%where.exe "!_path!:*_%%~A.txt"
')do set /a "_cnt+=1+0" && for /f tokens^=* %%i in ('"!_path!\%~n0.exe" !_cnt!')do <con: rename "%%~G" "%%~i.log" 

timeout.exe -1|echo\Is done^^!!. & 2>nul del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe" & popd & endlocal & goto:EOF

rem :: --------------- This C# code is adapted from original RAFNAS TP post in c-sharpcorner.com ::   
rem :: http://www.c-sharpcorner.com/article/convert-numeric-value-into-words-currency-in-c-sharp :: */ 

using System;

class Program
{
    public static void Main(string[] args)
    {
        if (args.Length == 1)
        {
            string number = args[0].ToString();
            Console.WriteLine(String.Format(Program.Tens(number)));
        }
    }

    private static String Tens(String number)
    {
        int _number = Convert.ToInt32(number);
        String name = null;
        switch (_number)
        {
            case 1:  
                name = "One";  
                break;  
            case 2:  
                name = "Two";  
                break;  
            case 3:  
                name = "Three";  
                break;  
            case 4:  
                name = "Four";  
                break;  
            case 5:  
                name = "Five";  
                break;  
            case 6:  
                name = "Six";  
                break;  
            case 7:  
                name = "Seven";  
                break;  
            case 8:  
                name = "Eight";  
                break;  
            case 9:  
                name = "Nine";  
                break; 
            case 10:
                name = "Ten";
                break;
            case 11:
                name = "Eleven";
                break;
            case 12:
                name = "Twelve";
                break;
            case 13:
                name = "Thirteen";
                break;
            case 14:
                name = "Fourteen";
                break;
            case 15:
                name = "Fifteen";
                break;
            case 16:
                name = "Sixteen";
                break;
            case 17:
                name = "Seventeen";
                break;
            case 18:
                name = "Eighteen";
                break;
            case 19:
                name = "Nineteen";
                break;
            case 20:
                name = "Twenty";
                break;
            case 30:
                name = "Thirty";
                break;
            case 40:
                name = "Fourty";
                break;
            case 50:
                name = "Fifty";
                break;
            case 60:
                name = "Sixty";
                break;
            case 70:
                name = "Seventy";
                break;
            case 80:
                name = "Eighty";
                break;
            case 90:
                name = "Ninety";
                break;
                
            default:
                if (_number > 0)
                {
                    name = Tens(number.Substring(0, 1) + "0") + " " + Tens(number.Substring(1));
                }
                break;
        }
        return name;
    }        
}

.\Q1594189_Cardial.cmd :

/* 2>nul || @echo off

cd /d "%~dp0" && title <nul && title ...\%~nx0
call %:^) && cd /d "%~dp0" && setlocal EnableDelayedExpansion
<con: echo\ && set "_path=!cd!" && pushd %__APPDIR__% && color 0a

set "_flag_c#=-w:0 -unsafe+ -codepage:1252 -t:exe -platform:anycpu" 
set "_flag_c#=!_flag_c#! -out:"!_path!\%~n0.exe" "!_path!\%~nx0" -o -nologo"

for /f tokens^=* %%c in ('where /r ..\Microsoft.NET csc.exe^|findstr.exe /i k.v4
')do <con: cmd.exe /v:on /e:on /s /c ""%%~c" !_flag_c#!" && goto %:^] || goto %:^V

%:^V
timeout.exe /t -1 | echo\ Something's really wrong here^!!. && type "%tmp%\_bs.bin"
<con: ^< nul del/q /f "%tmp%\_bs.bin" "%~dpn0.exe" 2>nul & popd & endlocal & goto :EOF

%:^)
2>nul "%__APPDIR__%Certutil.exe" -decode -f "%~dpnx0" "%temp%\_bs.bin" >nul || goto %:^v
for /f tokens^=* %%i in ('type "%temp%\_bs.bin"')do set "_bs=%%~i" && echo\%%~i && exit /b
rem :: <-----BEGIN ...\_bs.bin ----- G1sxRhtbMEobWzFGG1swSg0K== -----END ...\_bs.bin -----> :: 

%:^]
set "_cnt=0" && set "_range=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9"
for %%A in (!_range!)do for /f eol^=^|delims^= %%G in ('2^>nul %__APPDIR__%where.exe "!_path!:*_%%~A.txt"
')do set /a "_cnt+=1+0" && for /f tokens^=* %%i in ('"!_path!\%~n0.exe" !_cnt!')do "<con: rename "%%~G" "%%~i.log" 

timeout.exe -1|echo\Is done^^!!. & 2>nul del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe" & popd & endlocal & goto:EOF

rem :: --------------- This C# code is adapted from original RAFNAS TP post in c-sharpcorner.com ::   
rem :: http://www.c-sharpcorner.com/article/convert-numeric-value-into-words-currency-in-c-sharp :: */ 
 
using System;

class Program
{
    public static void Main(string[] args)
    {
        if (args.Length == 1)
        {
            string number = args[0].ToString();
            Console.WriteLine(String.Format(Program.Tens(number)));
        }
    }

    private static String Tens(String number)
    {
        int _number = Convert.ToInt32(number);
        String name = null;
        switch (_number)
        {
                case 1:
                name = "First";
                break;
                case 2:
                name = "Second";
                break;
                case 3:
                name = "Third";
                break;
                case 4:
                name = "Fourth";
                break;
                case 5:
                name = "Fifth";
                break;
                case 6:
                name = "Sixth";
                break;
                case 7:
                name = "Seventh";
                break;
                case 8:
                name = "Eighth";
                break;
                case 9:
                name = "Ninth";
                break;
                case 10:
                name = "Tenth";
                break;
                case 11:
                name = "Eleventh";
                break;
                case 12:
                name = "Twelfth";
                break;
                case 13:
                name = "Thirteenth";
                break;
                case 14:
                name = "Fourteenth";
                break;
                case 15:
                name = "Fifteenth";
                break;
                case 16:
                name = "Sixteenth";
                break;
                case 17:
                name = "Seventeenth";
                break;
                case 18:
                name = "Eighteenth";
                break;
                case 19:
                name = "Nineteenth";
                break;
                case 20:
                name = "Twentieth";
                break;
                case 30:
                name = "Fhirtieth";
                break;
                case 40:
                name = "Fortieth";
                break;
                case 50:
                name = "Fiftieth";
                break;
                case 60:
                name = "Sixtieth";
                break;
                case 70:
                name = "Seventieth";
                break;
                case 80:
                name = "Eightieth";
                break;
                case 90:
                name = "Ninetieth";
                break;
                
                default:
                if (_number > 0)
                {
                    name = Tens(number.Substring(0, 1) + "0") + " " + Tens(number.Substring(1));
                }
                break;
        }
        return name;
    }        
}

相关内容