rename a file baesd in the 1 line of the file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

rename a file baesd in the 1 line of the file

#1 Post by RMSoares » 20 Aug 2012 04:59

Hi,
I need to rename a file with the following format MYFILE_<DATE>_<ID>, where the DATE and ID are obtained based on the 1st line of the file, the DATE being 8 characters (in format YYYYMMDD) start in position 10 and the ID (the number of processing) in the 7 characters that begin in 21 position.

Can anyone help me with a script to rename the file in this way?

thanks in advance

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file baesd in the 1 line of the file

#2 Post by foxidrive » 20 Aug 2012 05:21

So you want the files renamed, with the existing filenames and the date and ID added (from the first line as you mentioned).

Is that right?

Are the files text files? Or binary files?

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: rename a file baesd in the 1 line of the file

#3 Post by RMSoares » 20 Aug 2012 05:45

File text

ex:
1H00 20120820 1234567

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: rename a file baesd in the 1 line of the file

#4 Post by Squashman » 20 Aug 2012 05:49

RMSoares wrote:File text

ex:
1H00 20120820 1234567

You sure this data isn't tab delimited. The positions are not matching up to what you described in your first post.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file baesd in the 1 line of the file

#5 Post by foxidrive » 20 Aug 2012 06:59

While you are answering Squashman, please answer my question too.

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: rename a file baesd in the 1 line of the file

#6 Post by RMSoares » 20 Aug 2012 07:57

The text file must be rename, the file MYFILE.dat must be renamed to MYFILE_<DATE>_<ID>.dat

The text file is fixed delimited, in the example same sapces are missing

A new example, the file MYFILE.dat, with the first line :
1H0012345201208201231234567

must be rename to MYFILE_20120820_1234567.dat

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file baesd in the 1 line of the file

#7 Post by foxidrive » 20 Aug 2012 17:25

This works here:

Code: Select all

@echo off
set /p "data="<"myfile.dat" >nul
ren "myfile.dat" "myfile_%data:~9,8%_%data:~20,7%.dat"
pause


ren "myfile.dat" "myfile_20120820_1234567.dat"

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: rename a file baesd in the 1 line of the file

#8 Post by RMSoares » 22 Aug 2012 04:38

Hi,
a correction, the file that we will receive have in the first line will be:
1H001234520120820123123 XPTO

and must be rename to MYFILE_20120820_0000123.dat

The ID (the number of processing) correspondig to the 7 characters that begin in 21 position, must be format, removed the spaces in rigth and put '0' at left.
The value of ID ('123 ') must be convert to '0000123'

How can i do that ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file baesd in the 1 line of the file

#9 Post by foxidrive » 22 Aug 2012 05:00

Just for the record, it is bad form to change what you asked for after you have working code.

RMSoares wrote:The ID (the number of processing) correspondig to the 7 characters that begin in 21 position, must be format, removed the spaces in rigth and put '0' at left.


I don't follow what you are saying.

Give character positions and text as examples for what you want.

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: rename a file baesd in the 1 line of the file

#10 Post by RMSoares » 22 Aug 2012 07:25

Yes, it's very bad to changed the rules of the game when we are finishing

My file will have this 1º record
1H001234520120820123 XPTO

Base ond the 1º record i will renamed the file to MYFILE_<DATE>_<ID>
Where DATE will be the 8 position from 10 and ID (the number of processing) in the 7 characters that begin in 21 position.

In the case of ID i will get '123 ' (123 more four spaces) and in the name of the file it will be foamted to '0000123', align to rigth and put zeros at left.

DATE=20120820
ID=0000123

And name of the file will be: MYFILE_20120820_0000123.dat

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file baesd in the 1 line of the file

#11 Post by foxidrive » 22 Aug 2012 07:30

RMSoares wrote:My file will have this 1º record
1H001234520120820123 XPTO

Base ond the 1º record i will renamed the file to MYFILE_<DATE>_<ID>
Where DATE will be the 8 position from 10 and ID (the number of processing) in the 7 characters that begin in 21 position.


Can you show me the 7 characters that begin in 21 position?

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: rename a file baesd in the 1 line of the file

#12 Post by RMSoares » 22 Aug 2012 12:01

Big mistake, the correct record is
1H001234520120820XXX123 XPTO

where DATE=20120820
ID="123 " (without quotes)

Post Reply