Solved

Renaming Files with Batch file

Posted on 2001-06-14
19
583 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
ID: 6193666
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
ID: 6194312
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
ID: 6194784
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Author Comment

by:sgriffin
ID: 6194824
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
ID: 6194870
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
ID: 6195032
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
ID: 6195188
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
ID: 6195274
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
ID: 6195296
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
 
LVL 2

Author Comment

by:sgriffin
ID: 6195371
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
ID: 6195382
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
ID: 6195398
I did everything in one script -- let me tweak it based on the new info you provided.....
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6195421
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
ID: 6195491
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
ID: 6195525
ive run this and it tells me file not foud when the file is there funny huh?
0
 
LVL 3

Expert Comment

by:czpczp
ID: 6195546
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
ID: 6195573
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
ID: 6196159
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
ID: 6214851
Hi Steve.

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

785 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