Link to home
Start Free TrialLog in
Avatar of chetan1981
chetan1981

asked on

DOS rename file that has timestamp as part of filename to a standard name

Hi,

I have a prcess that takes incoming files and loads them to a DB. The files have unique names on a daily as it includes the date\time stamp. I need to rename these files to a uniform name that the process can read.

For instance ABC01012015 needs to be ABC_main

Can someone please help on how to handle these renames dynamically?

Thanks,
Chetan
Avatar of NVIT
NVIT
Flag of United States of America image

Is there more than one ABC file in the same folder? If so, they would need to be handled.
Avatar of oBdA
oBdA

There are several ways to deal with this; this is not necessarily the easiest, but probably the most versatile (and you can reuse the time extraction part for other scripts; unlike the %date% or %time% variables, it is not dependent on the OS version and the user's regional settings).
Check the time stamp setting in line 20; it's not clear from your example whether you need day-month-year or month-day-year.
The script is currently in test mode and will only display the copy/delete commands it would normally run; remove the uppercase ECHOs in lines 21 and 23 to run it for real:
@echo off
setlocal enabledelayedexpansion
set IncomingFolder=C:\Temp
set IncomingPrefix=ABC
set Extension=
set UniformName=ABC_main
echo Getting time ...
set DoW=Sunday Monday Tuesday Wednesday Thursday Friday Saturday
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set /a DoWIndex = DayOfWeek + 1
for /f "tokens=%DoWIndex%" %%a in ("%DoW%") do set DayOfWeekName=%%a
REM *** At this point, the variables DayOfWeekName, Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeros if less than 10.
set TimeStamp=%Day%%Month%%Year%
ECHO copy "%IncomingFolder%\%IncomingPrefix%%TimeStamp%%Extension%" "%IncomingFolder%\%UniformName%%Extension%"
if not errorlevel 1 (
	ECHO del "%IncomingFolder%\%IncomingPrefix%%TimeStamp%%Extension%"
)

Open in new window

Avatar of chetan1981

ASKER

Just one file...thanks
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi oBda, this would work if the timestamp is always today's date\time. Unfortunately this is not always so. What I need is something that renames ABC%%%%% to ABC.

Thanks!
Hi NewVillageIT, the name is not constant all the time..thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assuming you don't need a backup of the old file, this should do it then; it's in test mode again, remove the uppercase ECHO in line 11 to run it for real:
@echo off
setlocal enabledelayedexpansion
set IncomingFolder=C:\Temp
set IncomingPrefix=ABC
set Extension=
set UniformName=ABC_main
if not exist "%IncomingFolder%\%IncomingPrefix%*%Extension%" (
	echo Source file not found.&exit /b 1
)
if exist "%IncomingFolder%\%UniformName%%Extension%" del "%IncomingFolder%\%UniformName%%Extension%"
ECHO ren "%IncomingPrefix%*%Extension%" "%UniformName%%Extension%"

Open in new window

thanks, will try it out.