Solved

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

Posted on 2015-01-02
9
201 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 23

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 83

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
 
LVL 23

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 23

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now