1752568 Members
5368 Online
108788 Solutions
New Discussion юеВ

renaming files help

 
SOLVED
Go to solution
ben_moore34
Advisor

renaming files help

Hello.

I'm after some help in renaming files on a VMS/ VAX system.

The files are called
Benbad.310;1
Benbad.311;1
Benbad.312;1
Benbad.314;1
.....and so on
There are about 300 or so files.
I want to rename them to
Benin.010;1
Benin.011;1
Benin.012;1
Benin.014;1
....and so on (relace the 3 to a 0)

I have tried rename Benbad.3*;1 to Benin.0*;1
but it comes up with 'error opening file Benin.0*; as output

Anyone know of a way of renaming the whole lot of files easily? I don't really want to rename each one one at a time as it'll take ages.

Many thanks in advance

BM
17 REPLIES 17
Jan van den Ende
Honored Contributor
Solution

Re: renaming files help

Ben,

to begin with:
WELCOME to the VMS forum!

For your problem, I would create a small DCL command file.

$loop:
$ next = f$search("benbad.*")
$ if next .eqs. "" then goto done
$ ext = f$parse(next,,,"extention") - "."
$ if f$type(ext) .nes. "INTEGER" then goto done
$ if ext .lt. 300 then goto done
$ nwext = ext - 300
$ rename 'next' *.'nwext
$ goto loop
$done:


Explanation:
-find the next file called benbad -- any extension
-get its extension, but remove the "."
-test it for being a number
-if it is less than 300 you may have restarted (check this for validity in your environment; might need to change to GOTO LOOP)
- subtract 100
- rename
- repeat

hth

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Robert Gezelter
Honored Contributor

Re: renaming files help

Ben,

I would write a small DCL script to iterate through the directories using F$SEARCH, then F$PARSE to break the resulting filenames into their component pieces, then constructing the desired filename. Finally use the RENAME command to actually rename from the old file to the new file.

You may find my presentation on DCL Lexical functions from the Fall 1995 DECUS Symposium useful (the PDF of the slides can be found at http://www.rlgsc.com/decus/usf95/index.html ).

- Bob Gezelter, http://www.rlgsc.com
ben_moore34
Advisor

Re: renaming files help

Hello Jan

Many thanks for the quick reply. I'll give this a go and let you know how I get on.

Cheers

Ben
ben_moore34
Advisor

Re: renaming files help

Thanks Rob

I'll take a look at that.

Cheers guys

Ben
Hein van den Heuvel
Honored Contributor

Re: renaming files help

Well, there is no direct RENAME option to do this.

You can go the DCL route with an F$SEARCH in a loop, pick apart the filename (f$PARSE) and rename or... use PERL.

$! dir be*
Benbad.310;1 Benbad.311;1 Benbad.312;1 Benbad.314;1

$ ! Always do a dry-run:
$ perl -e "for () {$o = $_; s/\.3/.0/; print qq($o $_\n)}"
benbad.310 benbad.010
benbad.311 benbad.011
benbad.312 benbad.012
benbad.314 benbad.014

$ ! Now for real...

$ perl -e "for () {$o = $_; s/\.3/.0/; rename $o,$_ }"

$ dir be*
benbad.010;1 benbad.011;1 benbad.012;1 benbad.014;1

Hein.





Hein van den Heuvel
Honored Contributor

Re: renaming files help

btw...
For me this is one of the most use perl 'one-liner' in my personal repetoir :-).

For those who are not too familiar with perl but intersted some, here is that command in slow motion:

perl -e ! Command to follw in string
for () ! Loop over some array returning
! elements into default variable $_
! GLOB function returing arrya of matching file names
{ ! Block open
$o = $_; ! copy $_ into $o (old name)
s/\.3/.0/; ! Substitute .3 with a .0
! Escape the period as it is a wildcard.
rename $o,$_ ! Function to do the deed
} ! Block Done, loop around

Hein.




ben_moore34
Advisor

Re: renaming files help

Hello

I think I'm going to have to use the DCL script method here as PERL is not installed on our VAX.

You are going to have to forgive me a bit here as I am pretty new to VMS and programming. I'm understanding the general idea of how the f$search & parse are operating.

I have created the DCL script and called it rename.com. Whenever I try to run it I'm getting

@rename
%DCL-W-IVKEYW, unrecognized keyword - check validity and spelling
\EXTENSION\

I'm obviously doing something basic wrong here.

Thanks

Ben
Hein van den Heuvel
Honored Contributor

Re: renaming files help


Ben,

Check out: $help lexi f$parse arg

Jan wrote: $ ext = f$parse(next,,,"extention") - "."
Jan meant: $ ext = f$parse(next,,,"TYPE") - "."

Jan's solution uses integer calculation.
Thje description of your problem suggested STRING manipulation to me.

Suggested code ('works for me' :-)

$loop:
$ next = f$search("benbad.*")
$ if next .eqs. "" then goto done
$ ext = f$parse(next,,,"TYPE") - ".3"
$ rename/log 'next' .0'ext
$ goto loop
$done:

Hein.
Jan van den Ende
Honored Contributor

Re: renaming files help

Ben,

Hein (Hi Hein, still sorry for missing our bridge appointment because of medical reasons last year, all the more since bootcamp for 2009 is cancelled!) is right of course.
My bad, I did not check the docs.

As for Hein's variant:

- ANY extension starting with the digit 3 would also be done, such as .3ABC, any in the 30's, any 3000's, any 30000's.

- my solution will also rename 4nn to 1nn, 5nn to 2nn, etc.
Check which is relevant to you!!

hth

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.