?
Solved

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

Posted on 2015-01-02
9
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

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 85

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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 25

Accepted Solution

by:
NVIT earned 2000 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 25

Assisted Solution

by:NVIT
NVIT earned 2000 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 85

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

752 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