Page 1 of 1

how to rename unknown source file names to fixed file names

Posted: 19 Mar 2016 11:42
by mimi
There is a folder of files, the batch needs to change all the file names to filenameA, filenameB, filenameC ....

Re: how to rename unknown source file names to fixed file names

Posted: 19 Mar 2016 14:22
by foxidrive
26 files? A to Z is 26 letters.

Re: how to rename unknown source file names to fixed file names

Posted: 23 Mar 2016 06:58
by [newbie]
Hi, maybe that is what you are looking

Code: Select all

@echo off & setlocal enabledelayedexpansion
cls

set "type=.txt"
set /a Nr=100
set /a Nr+=1

pushd "%~dp0"
REM first loop
for /f "delims=" %%i in ('dir /b /a-d /on "*%type%" 2^>nul') do (
    ren %%i "%%~ni-!Nr:~-2!%type%"
    set /a Nr+=1
)
REM second loop
for /f "delims=" %%j in ('dir /b "*%type%" 2^>nul') do (
    set str=%%~nj
    set file=!str:~0,-2!
    set str=!str:~-2!
    set str1=!str!
    for %%a in ("01=A" "02=B" "03=C" "04=D" "05=E" "06=F" "07=G" "08=H" "09=I" "10=J" "11=K" "12=L" "13=M" "14=N" "15=O" "16=P" "17=Q" "18=R" "19=S" "20=T" "21=U" "22=V" "23=W" "24=X" "25=Y" "26=Z") do (
    set "str1=!str1:%%~a!"
    )
    set str=!str:~0,-2!!str1!
    ren %%j !file!!str!!type!
)
popd
pause

In the first loop a number is on each file inserted
hello_world.txt > hallo_wold-1.txt
...
The second loop changes the numbers in letters
hallo_wold-1.txt > hallo_wold-A.txt
...

undo with... remove last 2 signs...

Code: Select all

cmd /v:on /c "for /f %i in ('dir /b /a-d "*.txt"') do @(set "fl=%~ni" &set "fl=!fl:~0,-2!" &ren %i !fl!%~xi)"

Re: how to rename unknown source file names to fixed file names

Posted: 23 Mar 2016 07:39
by Aacini
This method allows to select the character set used in the new names, and insert one or two characters of the set in the new names, as needed.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Define the character set used in the new names
set "charSet=AEIOU"
set "setSize=5"

set "first="
set /A "i=0, j=0"
for /F "delims=" %%a in ('dir /B /A-D *.*') do (

   rem Rename this file with new name
   for %%j in (!j!) do ECHO ren "%%a" "filename!first!!charSet:~%%j,1!%%~Xa"

   rem Pass to next name
   set /A "j=(j+1)%%setSize"

   if !j! equ 0 (
      rem Increment the first char
      for %%i in (!i!) do set "first=!charSet:~%%i,1!"
      set /A "i=(i+1)%%setSize"
   )

)

If the char set would be the 26 letters, this method support 26*26+26 new names. If this number is not enough, a third character can be inserted in a very simple way...

Note: if the "filename" part needs to be the original name of the file, just change it by "%%~Na".

Antonio