Solved How To rename a text file using Batch

January 30, 2015 at 19:15:29
Specs: Windows 7
I am very new to batch files. Could someone help me in renaming a text file in a folder.

I have many text files in the folder D:\folders\files
the text files will be like textfile_yyyymmdd.txt
filename_yyyymmdd.txt, on running a batch file i should be able to rename the files to rename the files as textfile.txt filename.txt

I tried using the batch script

set FOLDER_PATH=D:\Folder\Data\Rename
for %%i in (*) do (set fname=%%i) & call:rename
goto :eof
set fname=%fname:~0,-9%
ren "%fname%" "%fname:~0,-9%"
goto :eof

but by running this script i am losing the file extension.could anyone please help me overcoming this error

See More: How To rename a text file using Batch

Report •

January 31, 2015 at 04:04:26
✔ Best Answer
If you want to strip out the string _yyyymmdd from the filename then

@echo off
set FOLDER_PATH=D:\Folder\Data\Rename
for /F "tokens=1* delims=_" %%a in ('dir /B *.txt') do ren "%%a_%%b" "%%a.txt"

Report •

January 31, 2015 at 05:11:16

As usual, yours is better than mine.

M2 Golden-Triangle

Report •

February 1, 2015 at 17:42:59

Thank you so much!! It worked out exactly the way i wanted it to work!!! thanks a tone!!!

Report •
Related Solutions

Ask Question