Operating System - OpenVMS
1827807 Members
2645 Online
109969 Solutions
New Discussion

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

 
SOLVED
Go to solution

To delete a file if the sie of the file is less than 1 blocks in OpenVms

Hi Al,

I need to delete files which get created with 0 blocks. please help me.
i need a command to delete files which are less than 1 block in VMS.
15 REPLIES 15
Craig A
Valued Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

Hi!

This will generate a list of zero block files:

$direct/sel=size=max=1/col=1/nohead/notrail

You can then open up this file, read each record in and issue a $DELETE command.

HTH

Craig
Craig A
Valued Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

Whoops!

And you also need:

/size=all on the $direct command line.

Craig

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

I might have 100's of files in a directory.
i want to delete those automatially when i run a COM.
that COM should automatically get all those which has size of 0 blocks and delete. I do not want to manually do it daily.
Craig A
Valued Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

You need:

HELP OPEN
HELP READ
HELP CLOSE

Craig
RBrown_1
Trusted Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms


For an alternate method, try F$SEARCH, F$FILE_ATTRIBUTES and DELETE.
The Brit
Honored Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

In particular, you should look at lexical f$file_attributes, argument=EOF. This returns number of blocks used, zero-size file will return EOF=0

As suggested above, combine with an f$search and a delete command, in a loop.

DAve
Hein van den Heuvel
Honored Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

So you write a little DCL script around the produced file.

Or pipe it into a tool, like perl, which can take the list and turn it into a command.

For example:

$ pipe dir/sel=size=max=1/size=allo/nohead/notrail l*.*;* | perl -ne "chomp; unlink if /;/"

Or just let perl directly do the directory walk with a glob and do the size matching:

$ perl -e "for () {unlink unless -s}"

Too bad that DCL/DELETE does not take @ the way one would expect. It only takes the first line as filespec, the rest as commands.

Too bad $ DELETE does not take more 'common qualifiers'. It takes /since and /before but not /select.

Too bad DCL pipe does not come with an Unix XARGS like option.

You may also want to check out DFU HELP SEARC/FORMAT, but it's likely overkill.

( http://digiater.nl/dfu )

hth,
Hein

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

HI All,

thanks for your quick response. will try tis out tomorrow.
John Gillings
Honored Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

BE CAREFUL!...

realise that a file which is being written, open for exclusive access by another process, will be reported by F$FILE, DIRECTORY and DFU as size=0, even though it may contain large volumes of data. If you have sufficient privilege, you WILL be able to delete such a file. It will just be marked DELETE PENDING and be deleted when closed. If you're not careful you could delete real data, rather than just clean up a bunch of empty files.

You may want to check before blindly deleting such a file. You can try opening it for READ access:

$ SET NOON
$ OPEN/READ test candidate-file
$ IF $STATUS THEN CLOSE/DISPOSE=DELETE test

another option is to look at creation and modification dates (CDT and RDT). If exactly equal, the file is probably open.


Stepping back though... what is creating these files, and why do you get so many of them that you care enough to delete them?

Is there a way to detect them at the point they're created, rather than go hunting? For example:

$ out=some-file-name
$ do something which may generate output in 'out'
$
$ f=F$SEARCH(out)
$ IF f.NES.""
$ THEN
$ IF F$FILE(f,"EOF").LE.0 THEN DELETE 'f'
$ ENDIF

This way you KNOW the file is OK to be deleted.

If you must search whole disks, you may find DFU is quicker than DIRECTORY, especially on large volumes:

$ MCR DFU SEARCH/SIZE=MAX=0 device

A crucible of informative mistakes
John McL
Trusted Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

John G's question is very valid - why are these files being created? It may be a lot easier to prevent them being created than deleting them afterwards, and it will save the resources used when creating the file.

If you have no choice but to clean them up afterwards then maybe they have a naming convention that will help identify them. Use something like

$ WILD_FILE_NAME= "ABC*.*"
$ LOOP:
$ THIS_FILE = F$SEARCH(WILD_FILE_NAME)

This might avoid deleting the wrong file.
Shriniketan Bhagwat
Trusted Contributor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

Hi,

Who creates these files? Is it your application, when executed? If there is any specific pattern in the file name, then you can use the pattern to search the files using F$SERCH lexical as provided by the John Gillings. Be careful while deleting the files (you may not want to delete impotent files accidentally). As John Mcl said, it is good to prevent creation of such unwanted files than deleting then later. Prevention will save your precious time every time.

Regards,
Ketan

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

Hi All,

Thanks for your comments.
Zeni B. Schleter
Regular Advisor

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

My two cents... If this is a one shot effort, taking the output of the directory listing mentioned and editing it to do the delete is fairly quick. If this is repetitive then you can also use the f$file_attribute lexical function to check on creation dates or modified dates as well as the size of the file.

I would still use the Open/Close check if unsure whether the file is in use or not.
Paul Jerrom
Valued Contributor
Solution

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

What's the real problem? Loads of files in a directory? If they all have the same name and there are multiple versions being created and you are SURE you don't want to see the contents then you can set a file version limit - "$ SET FILE/VERSION_LIMIT=10 tmp.txt" will limit the number of occurances of tmp.txt to 10 (automatically deleting previous versions when needed). Although you'll have to purge the files down to the required number first ("$ PURGE/KEEP=10 tmp.txt").
But you'll need eventually to tackle the problem at source - find out what is creating the files and stop it!
Have fun,

Peejay
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If it can't be done with a VT220, who needs it?

Re: To delete a file if the sie of the file is less than 1 blocks in OpenVms

Hi all,

i have found out the issue why it was creating multiple files with 0 blocks. thanks a lot for your help. i have made the changes in the com.

Thanks to all once again.