1

I have 3 files in a folder which are:

XXX_a.txt
XXX_b.txt
XXX_c.txt

The filename can be varied, only the last character of the 3 files a, b and c are not changed

I need to rename the files as below:

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

How to write a batch file to read each file's name last character and then rename it respectively?

Your help are greatly appreciated, thank you.

Io-oI
  • 9,237
newbie
  • 13
  • 3

2 Answers2

0

You can rename using wildcards:

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

This assumes there's only a single _a, _b and _c file.

Berend
  • 3,192
0

enter image description here


  • This answer is intended to show how you can rename your files using batch loops and console executable in compiled at run time by itself

  • Where programmatically the repetition of the command will be in charge of assigning the names to your files already comprising a range of scope *_[a-z-0-9]

  • Two options are provided in this example, name them cardinally or literally:
  • Also designed to prevent errors from occurring if there is more than one file with the same letter:

  • Also preventing the occurrence of errors if there is more than one file with the same letter:

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
  • The code is adapted from this post by RAFNAS TP

  • The command line used to compile with csc.exe:

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

  • The following csc.exe compilers were used/tested [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

  • You can check for available csc.exe version in your command line using:
>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

  • This script automatically deletes the files generated on execution
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"
  • File "%tmp%\_bs.bin" is generated by decoding strings in base64 to delete 2 lines above, removing the escaping used to mark comments in
/* 2>nul || @echo off

enter image description here

  • Obs.: String source from @LotPings in this answer

  • File "!_path!\%~n0.exe" is the same path\Q1594189_Cardinal.exe or "path\Q1594189_Literal.exe" is respectively the name of the executable, obtained at run time, where %~n0 is the name of the used bat name concatenated with .exe, forming my_file.bat ==>> my_file.exe

  • The exeucatel file generated on execution accepts 1 numeric argument (int) and returns the corresponding cardinal or literal string respectively:

C:\>Q1594189_Cardinal.exe 11 
Eleventh

C:>Q1594189_Literal.exe 11 Eleven

  • To keep one of these files just remove it from the del command:
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"

del/q /f "%tmp%_bs.bin" "!_path!%~n0.exe"

  • To keep both, just remove the del command, and the existence of one or both files does not interfere with the main execution:
del/q /f "%tmp%\_bs.bin" "!_path!\%~n0.exe"

  • It has also been tested and works with only 3 files...

.\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 &#62; 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 &#62; 0)
            {
                name = Tens(number.Substring(0, 1) + "0") + " " + Tens(number.Substring(1));
            }
            break;
    }
    return name;
}        

}


Io-oI
  • 9,237