• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

Renaming Files with Batch file

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
sgriffin
Asked:
sgriffin
  • 9
  • 6
  • 2
  • +2
1 Solution
 
griesshCommented:
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
 
sgriffinAuthor Commented:
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
 
vsamtaniCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sgriffinAuthor Commented:
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
 
hendrik999Commented:
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
 
griesshCommented:
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
 
czpczpCommented:
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
 
sgriffinAuthor Commented:
I dont mind if its vb but as long a i can call it throug a sceduler thats fine !:-)

0
 
czpczpCommented:
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
 
sgriffinAuthor Commented:
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
 
sgriffinAuthor Commented:
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
 
czpczpCommented:
I did everything in one script -- let me tweak it based on the new info you provided.....
0
 
czpczpCommented:
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
 
czpczpCommented:
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
 
sgriffinAuthor Commented:
ive run this and it tells me file not foud when the file is there funny huh?
0
 
czpczpCommented:
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
 
czpczpCommented:
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
 
czpczpCommented:
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
 
czpczpCommented:
Hi Steve.

Just curious to know if I can provide further help to your batch questions?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now