Operating System - OpenVMS
1827752 Members
3280 Online
109969 Solutions
New Discussion

Re: SUbtracting in OpenVMS

 
SOLVED
Go to solution

SUbtracting in OpenVMS

Hi,

I am writing a COM to rename a group of files which satisfy the below condition. The File name has the current date and time
Example: 20100919103344
If the difference between the current date (20100920) and the file date (20100919) is greater than 1, then i need to do certain steps.
I tried this but it is not working as expected.

$file_name = "V2B_20100919103344.TXT"
$file_name = "V2B_20100919103344.TXT_TEMP"
$
$SH SYM file_name
FILE_NAME = "V2B_20100919103344.TXT_TEMP"
$file_date = f$extract(4,8,''file_name)
$SH SYM file_date
FILE_DATE = "20100919"
$CUR_DAT = F$CVTIME(,,"DATE") - "-" - "-"
$SH SYM CUR_DAT
CUR_DAT = "20100920"
$
$FINAL = (CUR_DAT - FILE_DATE)
$SH SYM FINAL
FINAL = "20100920"

Please advice.
34 REPLIES 34
Kris Clippeleyr
Honored Contributor
Solution

Re: SUbtracting in OpenVMS

You could use F$INTEGER on the strings CUR_DAT and FILE_DATE, and then do the subtraction.

Regards,
Kris (aka Qkcl)
I'm gonna hit the highway like a battering ram on a silver-black phantom bike...
abrsvc
Respected Contributor

Re: SUbtracting in OpenVMS

As stated above, use the f$integer lexical as shown in a modified version of your procedure listed bwloe.

Dan

============================================

$file_name = "V2B_20100919103344.TXT"
$file_name = "V2B_20100919103344.TXT_TEMP"
$!
$SH SYM file_name
$file_date = f$extract(4,8,''file_name)
$SH SYM file_date
$CUR_DAT = F$CVTIME(,,"DATE") - "-" - "-"
$SH SYM CUR_DAT
$!
$ c=f$integer("''cur_dat'")
$ f=f$integer("''file_date'")
$FINAL = (c - f)
$SH SYM FINAL
abrsvc
Respected Contributor

Re: SUbtracting in OpenVMS

Please note that the double and single quotes in the F$integer parameter are not required. I am a creature of habitand tend to include them, but they are not required in this context.

Dan
Hein van den Heuvel
Honored Contributor

Re: SUbtracting in OpenVMS



Why subtract? Your requirement
"If the difference between the current date"

_any_ difference will be 1 day or more.

So instead of :

- $FINAL = (CUR_DAT - FILE_DATE)

Use a string compare like:

- $IF CUR_DAT .GTS. FILE_DATE .......


Enjoy,
Hein
John Gillings
Honored Contributor

Re: SUbtracting in OpenVMS

Just to explain the behaviour... this is DCL doing clever things with the minus operator and polymorphic operands.

When DCL sees what appears to be an arithmetic expression containing only "+" and "-" it tries to work out if this is an INTEGER operation or a STRING operation, depending on the apparent types of the operands. In this case CUR_DATE is determined to be STRING, so the operation is STRING. Since the subtrahend is not a substring of the minuend, the result is the minuend.

You need to be very careful with these operators, to make sure DCL understands your intent. Sometimes you need to use F$INTEGER to force an operand to be interpreted as INTEGER or F$STRING to force it to be STRING.
In this case I'd go with Hein and use a comparison. Note that there you also need to be explicit about data type "GTS" for string and "GT" for integer.
A crucible of informative mistakes
John McL
Trusted Contributor

Re: SUbtracting in OpenVMS

Why not get yesterday's date in the same format as used in the filenames, then you can simply compare them.

$ yest_date = F$CVTIME("","COMPARISON","DATE") - "-" - "-"

(The above gives 20100921 when I run it because here it's already 21 Sep here.)

Just compare yest_date to the first 8 characters of your filename string.

It's not clear if you want to rename the files but this would be the simplest because you wouldn't need to check if the files were created earlier than yesterday.

Re: SUbtracting in OpenVMS

Hi All,

Thanks a lot for your replies.
I will be trying today with all of your valuable inputs..

Really thanks a lot..

Re: SUbtracting in OpenVMS

Hi All,

I have a problem now.
Consider this Example:
The current date is 20101001(October 1st 2010). The File date is 20100930.
I went with subtracting them as an integer. But i need the value to be 1. If i subtract, it gives the putput as 71.

Please Help..
The Brit
Honored Contributor

Re: SUbtracting in OpenVMS

Can I ask why you are messing with this subtraction stuff at all.

Is there no way you can do this using the f$delta_time lexical.

F$DELTA_TIME

Returns the time difference between a given start and end time.
The end time must be the same as or later than the start time.

Format

F$DELTA_TIME(start-time,end-time)

Times are supplied as "Absolute", so use f$cvtime to convert FILE_DATE back to absolute, Set CUR_DATE to f$cvtime(,"ABSOLUTE","DATE").

f$delta will return the difference as a Delta time. From which you extract the "day" component to test.

It certainly avoids having to worry about days-in-month.

Dave.

Joseph Huber_1
Honored Contributor

Re: SUbtracting in OpenVMS

f$delta_time is the correct way to get a time difference.

Even if You take f$integer() of the dates, don't think the difference of the two integers is the number of days between two dates!

Example:
20101001-20100901 = 100, not 30 !
http://www.mpp.mpg.de/~huber

Re: SUbtracting in OpenVMS

HI,

I have the file_date like this 20100930.
How will i convert it to absolute time ??

Re: SUbtracting in OpenVMS

What i want to do:
I need to find out all the files from a certain logical, which are older than 1 day from the current date and rename them.
The file Format which we have is %%_yyyymmddhhmmss.%%%

I get the FILE_DATE as "20100930"

How can i convert it into absoulte date ?

I tried the below:

$CUR_DATE = f$cvtime(,"ABSOLUTE","DATE")
$
$YEST = f$cvtime("YESTERDAY","ABSOLUTE","DATE")
$
$
$SH SYM CUR_DATE
CUR_DATE = "22-SEP-2010"
$
$SH SYM YEST
YEST = "21-SEP-2010"
$
$DAYS = F$DELTA_TIME (YEST,CUR_DATE)
$
$SH SYM DAYS
DAYS = " 1 00:00:00.00"
$

How can i extract DAY component from the variable DAYS ?


Please help.
abrsvc
Respected Contributor

Re: SUbtracting in OpenVMS

How about taking a different approach which will avoid all of this? Use the directory command as follows to generate a list of the files. Use the /output qualifier to send them to a file and process the file. This method may be easier to use in the long run. Let the system worry about the month transitions.

Dan

==================================
$ a=f$cvtime("yesterday")
$ yyyy=f$extract(0,4,a)
$ mm=f$extract(5,2,a)
$ dd=f$extract(8,2,a)
$ srch_string=yyyy+mm+dd
$ dir/before=yesterday *'string'*

===================================
Joseph Huber_1
Honored Contributor

Re: SUbtracting in OpenVMS


I have the file_date like this 20100930.
How will i convert it to absolute time ?


Try this .COM file:
$!Convert compact date (20100922) to VMS absolute
$ cdt=p1
$ y=f$extract(0,4,cdt)
$ m=f$extract(4,2,cdt)
$ d=f$extract(6,2,cdt)
$mn="NULL,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,"
$ nm=f$integer(m)
$! if (m.klt.1).or.(M>12) THEN GOTO ERR
$ mon=f$element(nm,",",mn)
$ vmsdate=d+"-"+mon+"-"+y
$ show symbol vmsdate
$ exit

http://www.mpp.mpg.de/~huber
Jan van den Ende
Honored Contributor

Re: SUbtracting in OpenVMS

CHANDRASEKARAN,

As usual, "What problem are you trying to solve" is important info.

>>>
I need to find out all the files from a certain logical, which are older than 1 day from the current date and rename them.
<<<

In VMS, this info is directly available:

$ Credat = f$file_attribute(filename,"CDT")
... and do the date-handling from there.

But even more directly if you want to rename them:

$ RENAME filename newname /BEFORE=YESTERDAY.
... if you need to retain (parts of) the file spec, then there are really nice tricks with wildcarding, or if you need more sofisticated manipulations, with f$parse.

But YOU have to tell us WHAT you need.

hth

Proost.

Have one on me.

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

Re: SUbtracting in OpenVMS

PLease note the following correction in my previous posting:

Original:

$ srch_string=yyyy+mm+dd
$ dir/before=yesterday *'string'*

Corrected:

$ srch_string=yyyy+mm+dd
$ dir/before=yesterday *'srch_string'*

Hein van den Heuvel
Honored Contributor

Re: SUbtracting in OpenVMS

>> I have the file_date like this 20100930.
>> How will i convert it to absolute time ??

Just like you always when you chewed of more than you can handle: A couple of bytes at a time

Joseph give a fine bit of parsing and reconstructing using F$ELEMENT. I like and often use that. But then you have to glue in the dashes.

So for this case I prefer F$EXTRACT and ditch the intermediate variable... those are for wimps!


$ vmsdate = f$extract(6,2,cdt) + -
f$extract( 4 * f$extract(4,2,cdt), 5, "NULL-JAN-FEB-MAR-APR-MAY-JUN-JUL-AUG-SEP-OCT-NOV-DEC-") + -
f$extract(0,4,cdt)
$
$ write sys$output cdt, " = ", vmsdate, " = ", f$cvtime(vmsdate,,"DATE")


>> I need to find out all the files from a certain logical, which are older than 1 day from the current date and rename them.

Ah! Here is what we really want to to (Jan!)
Is that a simple rename like directory, extension or name only, or complex replacing pieces of string in the name?

If it is complex rename, then you may want to consider a perl 'one-liner'

Just for fun....

First you test with 'print qq'

$ perl -le "for () { if ( -M > 1 ) { $old = $_; s/V2B/done/; print qq($old, $_ ) }}"

for () ... GLOB returning matching file names in $_

if ( -M > 1 ) ... Modification data older than 1.0 days?

$old = $_; ... Remember the old name

s/V2B/done/; ... Substitute a piece of string in $_

print qq($old, $_ ) ... print what rename details.


Now make it real by just replacing "print qq" with "rename"

$ perl -le "for () { if ( -M > 1 ) { $old = $_; s/V2B/done/; rename ($old, $_ ) }}"


This assumes that the file modification date matched the file name, which may be a stretch

And it assumes that you really wanted more than 24 hours = 1.0 days old, not just 'yesterday'.
If you want yesterday use DIR/BEF=YES.

Perl can of course also suck on that piece of time string in the filename and turn it into a real time.

Just for your entertainment....

One of many ways to pick apart the string:

$ perl -le "($y,$m,$d) = unpack(q(a4a2a2),q(20100930)); print qq(y=$y m=$m d=$d)"
y=2010 m=09 d=30

And now turn that into a time (seconds since 1-jan-1970, 86400 per day) and back:

$ perl -le "use Time::Local; ($y,$m,$d) = unpack q(a4a2a2),q(20100930); print scalar localtime timelocal(0,0,0,$d,$m-1,$y)"
Thu Sep 30 00:00:00 2010

Hope this helps some,
Hein



Re: SUbtracting in OpenVMS

Hi All,

I achieved the conversion of absoulte date from the string by using the below command:

$file_date="20100922"
$ADATE = F$CVTIME(f$extract(0,4,file_date)-f$extract(5,2,file_date)-f$extract(7,2,file_date), "ABSOLUTE", "DATE")
$sh sym adate
ADATE = "22-SEP-2010"
$
$

Anyway thanks for all your comments..
Hein van den Heuvel
Honored Contributor

Re: SUbtracting in OpenVMS

Sorry buddy, but that did not work at all.

Try 20100830.

You ended up substracting pieces of string from each other resulting in '010'.
That was used as the HOUR in the time defaulting the rest to "TODAY" and the time being discarded by the explicit "DATE" request.


Watch:

$ write sys$output f$extract(0,4,file_date)-f$extract(5,2,file_date)-f$extract(7,2,file_date)
010

$ write sys$output F$CVTIME(010,"ABSOLUTE")
22-SEP-2010 10:00:00.00


Cheers,
Hein
Joseph Huber_1
Honored Contributor

Re: SUbtracting in OpenVMS

>> I achieved the conversion of absoulte date from the string by using the below

Fine if You are happy.
In fact what it gives You is the date of today 10 O'Clock.
http://www.mpp.mpg.de/~huber

Re: SUbtracting in OpenVMS

Hi Hein,

Thanks...
I am trying it for other dates.
Joseph,

I am not able to seee the query u used.... :)
Jan van den Ende
Honored Contributor

Re: SUbtracting in OpenVMS

Please review my answer of 13:10:39 GMT!!

TRY it.

And then, if anything still is unclear, detail that.

Proost.

Have one on me.

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

Re: SUbtracting in OpenVMS

Hi Jan,

sorry... was lil busy...
actually in your idea, it is taking the created date :-( so, here the jobs run for around 8 hours continuously.
So, the file date can be 20100920120023
but the create date will be after 20th..

So, that will not workout for me...
I need the file date and not the file create date ...
Jan van den Ende
Honored Contributor

Re: SUbtracting in OpenVMS

CHANDRASEKARAN,

>>>
So, the file date can be 20100920120023
but the create date will be after 20th..

So, that will not workout for me...
I need the file date and not the file create date ...
<<<

Please be clear about that!

The moment you initiate a file (at the start of the job I assume?) is the CreateDateTime (CDT). The moment you close such file (near the end of the job?) will be RevisionDateTime (RDT) -- but that gets updated every time you change ANYTHING in the file OR ITS DESCRIPTION.

If it is more complicated than that, please be clearer.

Proost.

Have one on me.

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