Search code examples
stringbatch-rename

Batch add string before filename


I'm working on a Windows batch file and I need to change name of files in current direcotry.

I have these files:

file1.txt
file2.txt
file3.txt

and I need to add string "REG~" before each filename like this

REG~file1.txt
REG~file2.txt
REG~file3.txt

Thank you.


Solution

  • @echo off    
    SETLOCAL ENABLEDELAYEDEXPANSION    
    SET old=file  
    SET new=REG~file   
    for /f "tokens=*" %%f in ('dir /b *.txt') do (    
        SET newname=%%f    
        SET newname=!newname:%old%=%new%!    
    move "%%f" "!newname!"    
    )
    

    What this does is it loops over all .txt files in the folder where the batch file is located and replaces the file with December inside the filenames.