Solved

Renaming Files with Batch file

Posted on 2001-06-14
19
575 Views
Last Modified: 2013-12-28
I need to rename a file in the formart 000001.dat and the next day it is 000002.dat etc.
I need to rename it to the format abcdefgh000001.dat

Any ideas? its simple enough i imagine but cant think :-)
0
Comment
Question by:sgriffin
  • 9
  • 6
  • 2
  • +2
19 Comments
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I don't think a batch file is the right thing here. If you could use one of the UNIX ports of a shell, they would allow you to do things like that. Another option would be a little C program, how about that?

======
Werner
0
 
LVL 2

Author Comment

by:sgriffin
Comment Utility
Hi, thats possible , thing is i know nothing about UNIX (onyl how to spell it:-)  and less about C , but i suppose i could get a start learning :-)
0
 
LVL 5

Expert Comment

by:vsamtani
Comment Utility
I'm not entirely sure exactly what you're trying to achieve. If you want to rename a single file each day, so that on day 1 it's called 001.dat, on day 2 it gets renamed to 002.dat, etc (are they daily backup files?), then your batch file could simply be:

ren 998.dat 999.dat
ren 997.dat 998.dat
ren 996.dat 997.dat
...
ren 001.dat 002.dat

If you're talking about naming a file so that its name reflects the date on which it was created, then you might want to investigate kixtart (www.kixtart.org). It's a scripting system which is essentially just a high-powered batch language.

Vijay


0
 
LVL 2

Author Comment

by:sgriffin
Comment Utility
Ill explain

the file is output from a system every day in the format 000001.dat next day 000002.dat

We have another system that accpets this for integration but at the moment , i manually rename the file to abcdefgh000001.dat(for example) , i need to keep the number at the end of the file for reference for the receiving system.

In Ms Access VBA it would be along the lines of

filename="00001.dat"

filename="abdefgh"+"filename
therefore  filename is now = abcdefgh000001.dat

but i want to do this with some sort of batch file ?

I hope this is a little bit clearer

thanks

Stephen
0
 
LVL 3

Expert Comment

by:hendrik999
Comment Utility
Try to use VBScript. The program language is simular to Visual Basic and you can run scripts by double-clicking on it or from the command line:
  cscript <scriptname>
Visual Basic Script is installed with Internet Explorer 5 and also available at Microsoft.
http://msdn.microsoft.com/scripting/

If you like this I can write a small example.
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
If you don't know anything about C it's not a good time to start with this project. How about hendrik's suggestion to use VBScript? Are you familiar with that?
You have to use some kind of utility, so let's find a tool you have experience with. (Did you ever do Basic?)

======
Werner
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
Since you need a batch to accomplish this (of course VB would be easier) I developed a script that can do this.

I'm testing it now -- I'll post back in a little while.
0
 
LVL 2

Author Comment

by:sgriffin
Comment Utility
I dont mind if its vb but as long a i can call it throug a sceduler thats fine !:-)

0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
This will accomplish what you're looking for.  Based on the info you provided, here's how it works.


1) The following script creates a temp file called "fileholder" which contains all of the "abcdefgh" file names in alphabetical order using the "dir /b/on" command.

2) We get the very last file name using the "For" statement and assign that file name to "hold" environment variable.  This gets us the last numerical file.

3) We then isolate the numerical end of the file name (i.e. 000001).

4) We then temporarily append "999" in front of it because when we use the set /a command to increment it by 1, it will remove any zero's that precede the number (i.e. 000001 would be incremented to 2 as opposed to 000002).

5) We then remove the 999 using the For statement.

6) We rename the file using the incremented variable

7) Lastly, we delete our temporary file "fileholder".




Rem  ~~~~~~~~~~~~~~~~~~~~~~~~  Rename.BAT   ~~~~~~~~~~~~~~~~~~~~~~~~  

@echo off
cls

dir abcdefgh*.dat /b/on >fileholder
for /f %%i in (fileholder) do set hold=%%i
set hold2=%hold%

For /f "tokens=1,2* delims=abcdefgh" %%i in ('echo %hold%') do set hold=%%i
For /f "tokens=1,2* delims=." %%i in ('echo %hold%') do set hold=%%i

set hold=999%hold%
set /a hold=%hold% + 1

For /f "tokens=1,2* delims=999" %%i in ('echo %hold%') do set hold=%%i

Rem This is where we rename the file incrementing by 1
ren %hold2% abcdefgh%hold%.dat

echo y|del fileholder


Rem  ~~~~~~~~~~~~~~~~~~~~~~~~  Rename.BAT   ~~~~~~~~~~~~~~~~~~~~~~~~  
0
Wish Marketing would stop bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

 
LVL 2

Author Comment

by:sgriffin
Comment Utility
Hi , thats great , one thing though , the incrementing number 00001.dat,00002.dat etc is created by one application already , i just need to add the abdcefgh to the front of it each time but keep the number allocated without renaming and giving a new number. Also to let you know the file is actually called " IF051BG5.SQ " and then i need to have IF051BG5.SQ00001.dat in place of 00001.dat .

can your file do that?
0
 
LVL 2

Author Comment

by:sgriffin
Comment Utility
File Not Found
Invalid number.  Numeric contants are either decimal (17),
hexidecimal (0x11), binary (0b10001) or octal (021).
The syntax of the command is incorrect.

C:\>

this is the error received.

0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
I did everything in one script -- let me tweak it based on the new info you provided.....
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
The following will create "IF051BG5.SQ00001.dat" FROM "000001.dat"


@echo off
cls

dir *.dat /b/on >fileholder
for /f %%i in (fileholder) do set hold=%%i

ren %hold% IF051BG5.SQ%hold%
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
The following will rename a group of "dat" files all at once rather than one at a time (the "??????" represents how many digits are in these dat file names):



@echo off
cls

set hold=IF051BG5.SQ

dir ??????.dat /b/on >fileholder
for /f %%i in (fileholder) do ren %%i %hold%%%i
echo y|del fileholder



<eg>

000001.dat
000002.dat
000003.dat
000004.dat

becomes...

IF051BG5.SQ000001.DAT
IF051BG5.SQ000002.DAT
IF051BG5.SQ000003.DAT
IF051BG5.SQ000004.DAT
0
 
LVL 2

Author Comment

by:sgriffin
Comment Utility
ive run this and it tells me file not foud when the file is there funny huh?
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
The batch doesn't specify any particular sub-directory.  In it's current state, you need to run this from the directory containing the DAT files.  You can tweak to add the sub-dir so you can run it from anywhere (on the same drive).  Also, I have 6 "?" to represent how many digits are in the source DAT file.  Make sure you have the correct amount of "?".  In other words, if there 5 digits (00001) then use "?????" instead of "??????".
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
If you provide the *exact* drive and path (or UNC if it's a network share) where these files reside, I'll tweak the above batch to reflect it so that you can run from anywhere.
0
 
LVL 3

Accepted Solution

by:
czpczp earned 15 total points
Comment Utility
If the path for these DAT files were D:\LOG and the files have 5 digits in its' name (00001.dat), then you can do this to the last two scripts I gave you:




Rem This batch will get all the numerical dat files and rename it.

@echo off
cls

set hold=IF051BG5.SQ

dir D:\LOG\?????.dat /b/on >fileholder
for /f %%i in (fileholder) do ren D:\LOG\%%i %hold%%%i
echo y|del fileholder





Rem This batch will get the last numerical dat file and rename it.

@echo off
cls

dir D:\LOG\*.dat /b/on >fileholder
for /f %%i in (fileholder) do set hold=%%i

ren D:\LOG\%hold% IF051BG5.SQ%hold%
0
 
LVL 3

Expert Comment

by:czpczp
Comment Utility
Hi Steve.

Just curious to know if I can provide further help to your batch questions?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

763 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

9 Experts available now in Live!

Get 1:1 Help Now