Rename Text Files Based On Content

December 6, 2010 at 10:34:39
Specs: Windows 7
I would like to rename text files based on their content e.g. file recu0021.txt starts like this:

Contact: +4XXXXXXXXXX59
Date: 11.09.2010 01:40


I want rename the file in "2010-11-09 01-40 +4XXXXXXXXXX59.txt"

This means the date first (Line 2, Position 7 to 16 exchanging the dots for "-") and adding the phone number (Line 1, Position 10 to 23).
Is it possible to do this with a batch file?

I'd appreciate any help!!
Thanks in advance!

See More: Rename Text Files Based On Content

Report •

December 6, 2010 at 17:00:12
Hi Joko

On my system also Windows 7 the Rename command did'nt like the ":" so the code
changes it to a "-"

@echo off
setlocal EnableDelayedExpansion

type nul>Name.bat
for %%a in (recu*.txt) do (
call :GetData %%a
set FileName=!FileName:^:=-!
echo ren "%%a" "!FileName!">>Name.bat
type Name.bat
rem Delete the "rem" below will the "type" line above if ok
rem Name.bat
exit /b

:GetData %1
for /f "tokens=1-6 delims=. " %%a in (%1) do (
if "%%a" EQU "Contact:" (Set FileName=%%b)
if "%%a" EQU "Date:" (set FileName=%%d-%%b-%%c %%e %%f !FileName!.txt& exit /b)

Report •

December 6, 2010 at 23:21:49
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (my.txt) do (
set /a N+=1 & if !N! equ 3 goto :done
set V!N!=%%a

set T=!V2:~12,4!-!V2:~6,2!-!V2:~9,2! !V2:~17,2!-!V2:~20,2!
set T=!T! !V1:~10!
echo ren my.txt "!T!.txt"

Life is too important to be taken seriously.


Report •

Related Solutions

Ask Question