Solved

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

Posted on 2015-01-02
9
199 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
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 …
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)

705 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

19 Experts available now in Live!

Get 1:1 Help Now