Solved Rename Multiple Text Files Based on Contants of Line 1

August 15, 2016 at 05:09:39
Specs: Windows 8.1
Hi

I have numerous text files with random names that reside in a common folder. I am looking for a batch file that will rename each file based on the contents of the first line in each text file.

For example, Line 1 in each text file has the format:

Now: 17/09/2015 02:06:49

I would like the file to be renamed to:

20150917-020649.txt

The batch file would be executed from the folder containing the text files.

Appreciate any assistance anyone can offer.

Best Regards

message edited by WestCoaster


See More: Rename Multiple Text Files Based on Contants of Line 1

Report •

#1
August 15, 2016 at 06:12:40
✔ Best Answer
::====== script starts here ===============
::
:: west.bat 2016-08-15 19:39:39.79
@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
call :sub1 %%a
echo ren %%a !D!-!T!.TXT
)
goto :eof

:sub1
for /f "tokens=* delims= " %%i in (%1) do (
set F=%%i
goto :gotline
)
:gotline
set F=!F:Now: =!
set D=!F:~6,4!!F:~3,2!!F:~0,2!
set T=!F:~11,2!!F:~14,2!!F:~17,2!
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
August 15, 2016 at 06:33:37
Awesome, works a treat. Thanks very much for your help.

Report •
Related Solutions


Ask Question