filename change and extraxct 5 char from name

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

filename change and extraxct 5 char from name

Post by worldwide » Tue Jul 24, 2007 12:30 pm

Hello

new topic .... and im gettin or better to say im already addicted to MS

I have a file, it has a filename like asdf_xxxx_assd.DAT
And the x can change from 4 till 8 times x, like : asdf_xxxxxxxx_assd.DAT

I have another file, this one i want to rename into xref_xxxxx.DAT, where the xxxxx is the one from the otherfile.

Is this possible ?

I checked out Position, Length and so on, but since the amount of x changes im stuck at the moment.

Any help or tipps ?

Thanx in advance

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 24, 2007 12:38 pm

This is the long-winded, but probably easier to understand, method:

Code: Select all

//play around with different values between the underscores
Let>fname=asdf_123456_assd.DAT

//extract the part between the underscores (_)
Position>_,fname,1,start
Let>start=start+1
Length>fname,lf
MidStr>fname,start,lf,temp
Position>_,temp,1,end
Let>end=end-1
MidStr>temp,1,end,midPart

Let>newfile=xref_%midPart%.DAT
MessageModal>newfile

//RenameFile>sourcefile,newfile
Change the value of fname and it will always extract the part between the underscores.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Tue Jul 24, 2007 1:15 pm

:D GREAT !!!! Im just working on it !!

If its ready ill post it here for others !!

Thanx !!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jul 24, 2007 1:28 pm

If it's guaranteed that those are the only two underscores in the filename, then it's simpler to use Separate>

Let>fname=asdf_123456_assd.DAT
Separate>fname,_,parts
Let>newfile=xref_%parts_2%.DAT
MessageModal>newfile

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Tue Jul 24, 2007 1:30 pm

This is what i finished, the only thing is maybe that if there are more DAT files in the directory, but it still works :-) ;-)


---------------------------------------------------------------------------------
---------------------------------------------------------------------------------

GetFileList>c:\directory\*.DAT,aantal,;

Let>fname=%aantal%

//extract the part between the underscores (_)
Position>_,fname,1,start
Let>start=start+1
Length>fname,lf
MidStr>fname,start,lf,temp
Position>.,temp,1,end
Let>end=end-1
MidStr>temp,1,end,midPart

Let>newfile=XREF_%midPart%.DAT

RenameFile>c:\directory\XREF.DAT,c:\directory\%newfile%
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------


Thanx again for the help :-) ;-)

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Tue Jul 24, 2007 1:33 pm

@Me_again

im sorry the file is like : xxxx_1_xxxxx.DAT

I tried also to seperate it, but i couldnt figure out how

But i previous wrote my final version, maybe you got any better changes for it ? Would be appriciated, even that it works now.

Thanx also to you ofcourse

c.u.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jul 24, 2007 1:36 pm

worldwide wrote:@Me_again

im sorry the file is like : xxxx_1_xxxxx.DAT
I'm confused, which part do you want to re-use, 'xxxx', 1, or 'xxxxx' ?

Those would be parts_1, parts_2, but then you would need to Separate> again on '.' to get the 'xxxxx' without the .DAT

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Tue Jul 24, 2007 2:07 pm

sorry my mistake :-)

The last part till .DAT, so 1234_xxxxxx.Dat where the xxxxxx ist the part which i need to reuse.

Thanx !!! ;-)

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Tue Jul 24, 2007 2:44 pm

Ok,

now it doesnt work so well as i thought......

this is the error...

i have 5 files with different start names but the same ID(but the ID ispossible from 3 till 8)
Like :

abc_ID1_ID2.dat
def_ID1_ID2.dat
ghi_ID1_ID2.dat
jkl_ID1_ID2.dat
mno_ID1_ID2.dat
XREF.dat

I want to rename that XREF.dat into XREF_ID1_ID2.dat

What i tried was to get the first file, takeout the ID1 and ID2.
I did that with :
---------------------------------------------------------------------------------

GetFileList>c:\directory\*.DAT,aantal,;
Let>fname=%aantal%
Position>_,fname,1,start
Let>start=start+1
Length>fname,lf
MidStr>fname,start,lf,temp
Position>.,temp,1,end
Let>end=end-1
MidStr>temp,1,end,midPart
Let>newfile=XREF_%midPart%.DAT
RenameFile>c:\directory\XREF.DAT,c:\directory\%newfile%
-----------------------------------------------------------------------------
But ... as MS it works somehow, but if i compile it into an exe it gives an error

As you maybe can think im a beginner, so my "coding" if you can speak of it is more the less very basic, but im triing ;-)

Maybe some thoughts ?

worldwide
Junior Coder
Posts: 31
Joined: Mon Jul 09, 2007 11:34 am

Post by worldwide » Wed Jul 25, 2007 8:05 am

Ok, now i got a solution, but also a problem :

I did it now different, i did a readout of the file and the proper position.
I got the info i needed.
--------------------------------------------------------------------
--------------------------------------------------------------------
ReadLn>C:\directory\XREF.DAT,3,text
MidStr>text,69,11,show
Let>rest=show
//MessageModal>show
Position>\,rest,1,start
Let>start=start+1
Length>rest,lf
MidStr>rest,start,lf,temp
Position>.,temp,1,end
Let>end=end-1
MidStr>temp,1,end,midPart
Let>newfile=%midPart%
//MessageModal>newfile
RenameFile>C:\directory\XREF.DAT,C:\directory\XREF_1_%newfile%.DAT
---------------------------------------------------------------------
---------------------------------------------------------------------

As a macro i works, but as soon as i convert it into an exe-file it doesnt work, could someone help me why this is ?
Aspecially the rename function doesnt work !!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts