Solved

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

Posted on 2015-01-02
9
203 Views
Last Modified: 2015-01-02
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
0
Comment
Question by:chetan1981
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40528291
Is there more than one ABC file in the same folder? If so, they would need to be handled.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 40528295
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

0
 

Author Comment

by:chetan1981
ID: 40528301
Just one file...thanks
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 40528310
What do you mean by dynamic? On a scheduled basis?

If the name is always like ABC01012015 how about
ren ABC* ABC_main

Open in new window

0
 

Author Comment

by:chetan1981
ID: 40528314
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!
0
 

Author Comment

by:chetan1981
ID: 40528318
Hi NewVillageIT, the name is not constant all the time..thanks
0
 
LVL 24

Assisted Solution

by:NVIT
NVIT earned 500 total points
ID: 40528325
If that's the only 1 file, it would work. After the rename however, the file needs to be moved out somehow. So you can do the next rename at a later time, if needed.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 40528327
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

0
 

Author Comment

by:chetan1981
ID: 40528331
thanks, will try it out.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question