1748144 Members
3633 Online
108758 Solutions
New Discussion юеВ

Re: file renaming

 
SOLVED
Go to solution
nipun_2
Regular Advisor

file renaming

Hi,
I am new to OpenVMS and wanted to know if there is a simple way to rename files. I am aware of rename command however I have filenames increasing sequentially but in different folders

e.g dir1.subdir1.filename
dir2.subdir2.filename
dir3.subdir3.filename

Can anyone please let me know how to rename the filenames with a simple script

Thanks in advance
10 REPLIES 10
Ian Miller.
Honored Contributor

Re: file renaming

Can you give an example?

you can do things with wildcarded renames otherwise a simpile loop in DCL
$C=1
$L1:
$ IF C .LT. 10 THEN GOTO END
$ RENAME [DIR'C'.SUBDIR'C']FILENAME NEWNAME
$ C = C+ 1
$ GOTO L1
$END:

I expect Hein will come up with a solution in perl :-)
____________________
Purely Personal Opinion
Jan van den Ende
Honored Contributor

Re: file renaming

Nipus,

some simple examples:

rename all files from [DIR.SUB] to [DIR]
(one level up)
$ RENA [DIR.SUB]*.*.* [DIR]
rename the highest version of each file:
$ RENA [DIR2.XYZ]*.*;0 [DIR3.ABC.DEF]
Renane alle files except those created todat:
$ RENA [X.Y]*.*.* [A.B.C]/BEFORE=TODAY
rename all files modified the last week:
$ RENA [A.B]*.*.* [C.D.E.F]/MODIFIED/SINC="TODAY-7-"
Rename all (but only the) .XYZ files:
$ RENA [P]*.XYZ.* [Q.R]
Rename all files with PQR somewhere in the filename:
RENA [X]*PQR*.*;* [Y.Z]

If you have more complex needs, then in a DCL script just about anything can be accompliced. Tell us your need, and we will come up with an example solution.

You can work out your own needs.

Beware:
unlike UNIX, in VMS you can ONLY rename files to a location on THE SAME PHYSICAL DISK DRIVE!

hth

Proost.

Have one on me.

Jan
Don't rust yours pelled jacker to fine doll missed aches.
Jan van den Ende
Honored Contributor

Re: file renaming

Ian,

your script will not do much!!

C=1

If C is less than 10 go to end.....
which will very quickly be over, and nothing gets done :-(

This sure makes it easy for Hein to come up with something better!

I guess you meant
$ IF C . GT. 10 THEN ...

Proost.

Have one on me.

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

Re: file renaming

Jan, extra Duvel for you :-)
Just a typo.
____________________
Purely Personal Opinion
nipun_2
Regular Advisor

Re: file renaming

Thanks a lot for the replies. Giving you details is even better for me.

I have two objectives 1) Rename files (*.ABC)
2) Move (*.TXT) files

In both the scenario the directory setup is
as shown below. Right now I will go with first object, 2nd one I will post as new thread if required

Please Note: these are two separate files in each directory with .GOBJ extension

[00000760.00000427]C0000385.GOBJ
[00000760.00000427]C0000385_TRAB.GOBJ

[00000761.00000428]C0000386.GOBJ
[00000761.00000428]C0000386_TRAB.GOBJ

[00000763.00000429]C0000387.GOBJ
[00000763.00000429]C0000387_TRAB.GOBJ.
.
.
.

Please observe the difference in numbers between 2nd and 3rd DIRECTORIES....


The following is the renaming
$RENAME C0000385.GOBJ C0000385_full.GOBJ
$RENAME C0000385_TRAB.GOBJ C0000385.GOBJ

The following things are constant
1) No change in strings
2) The files are always located two subdirectories below as shown in the above example
3) File numbers can range from 0000000 to 999999 (right now we have files till 00008000)with a string before e.g C0000499.GOBJ

My point is I am looking for 2 options in script that works like this
Option 1: Sequential Increase

Enter your starting file number: 385 (this is directly the .Gobj file number shown above)
Enter you ending file number: 402

Option2: Non sequential increase

In this scenarion I am thinking of preparing a txt file or editing the COM file and writing down each *.GOBJ number eg from which it can be read.

$!enter the file names beow
335
345
402
454
$End


let me know your suggestions.
Hein van den Heuvel
Honored Contributor
Solution

Re: file renaming

The DCL RENAME command can not directly do what you want.
It can not morph a file name or extension.

Here is a coarse outline with examples.
First in PERL, then in DCL.

First create some test files:

$ cr/dir [.tmp.a]
$ cr/dir [.tmp.b]
$ cr/dir [.tmp.c]
$ cop nl: [.tmp.a]001.tmp
$ cop nl: [.tmp.a]002.tmp
$ cop nl: [.tmp.a]003.tmp
$ cop nl: [.tmp.b]003.tmp
$ cop nl: [.tmp.b]004.tmp
$ cop nl: [.tmp.b]005.tmp
$ cop nl: [.tmp.c]006.tmp

List them in perl with a one-liner:
$ perl -e "print ""$_\n"" foreach (<[.tmp.*]*.tmp>)"
u$1:[hein.tmp.a]001.tmp
u$1:[hein.tmp.a]002.tmp
u$1:[hein.tmp.a]003.tmp
u$1:[hein.tmp.b]003.tmp
u$1:[hein.tmp.b]004.tmp
u$1:[hein.tmp.b]005.tmp
u$1:[hein.tmp.c]006.tmp

Here is a perl one-liner for rename:

perl -e "foreach (<[.tmp.*]*.tmp>){ if (/(.*)\.(.*)/) { rename $_,$1.""_xxx."".$2 }}"

- the same 'glob' operation to find target files.
- then a regular expression to look for anytinng it can up to the last period (the * is greedy) remember that (in $1), and remember everything after that last period in $2.
- call rename with the file glob line ($_) as first argument, and a constructed second argument.

You could readily tweak the glob file spec, or the regexpr to match exactly those files needed.


Now a DCL loop to get the files:

$ cre tmp.com
$loop:
$file = f$search ("[.tmp.*]*.tmp")
$if file .eqs. "" then goto done
$write sys$output file
$goto loop
$done:
Exit
$ @tmp
U$1:[HEIN.TMP.A]001_XXX.TMP;1
U$1:[HEIN.TMP.A]002_XXX.TMP;1
U$1:[HEIN.TMP.A]003_XXX.TMP;1
U$1:[HEIN.TMP.B]003_XXX.TMP;1
U$1:[HEIN.TMP.B]004_XXX.TMP;1
U$1:[HEIN.TMP.B]005_XXX.TMP;1
U$1:[HEIN.TMP.C]006_XXX.TMP;1

And a DCL loop to do similar renames:

$ cre tmp.com
$loop:
$file = f$search ("[.tmp.*]*.tmp")
$if file .eqs. "" then goto done
$new_name = f$parse(file,,,"name") + "_yyy"
$rename/log 'file 'new_name
$goto loop
$done:
Exit
$ @tmp
%RENAME-I-RENAMED, U$1:[HEIN.TMP.A]001_XXX.TMP;1 renamed to U$1:[HEIN.TMP.A]001_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.A]002_XXX.TMP;1 renamed to U$1:[HEIN.TMP.A]002_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.A]003_XXX.TMP;1 renamed to U$1:[HEIN.TMP.A]003_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.B]003_XXX.TMP;1 renamed to U$1:[HEIN.TMP.B]003_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.B]004_XXX.TMP;1 renamed to U$1:[HEIN.TMP.B]004_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.B]005_XXX.TMP;1 renamed to U$1:[HEIN.TMP.B]005_XXX_YYY.TMP;1
%RENAME-I-RENAMED, U$1:[HEIN.TMP.C]006_XXX.TMP;1 renamed to U$1:[HEIN.TMP.C]006_XXX_YYY.TMP;1


Enjoy!
Hein.
Karl Rohwedder
Honored Contributor

Re: file renaming

Hi,

I once wrote a little utility for 'enhanced' renaming, Heins example would be:

$ xren <.%>%%%.tmp ()_YYY

The help (XREN ?) is in german, but in the beginning are some english comments.

mfg Kalle

P.S. I renamed it to .COM_TXT.
nipun_2
Regular Advisor

Re: file renaming

Hein,
That was awesome. I got things running pretty well. Ofcourse, there are some modifications I still have to do. But that script pretty much did everything I need to know.

The script by Ian was also very helpful for file renaming.

nipun_2
Regular Advisor

Re: file renaming

Thank you for the utility Karl,
I have already developed the file as per Hein's script and thereby also got better understanding of DCL programming. Your utility file will definetely be useful as a learning experience.

nipun