蝎王府羊蝎子加盟:File Attributes: what are they and how to use them.

来源:百度文库 编辑:九乡新闻网 时间:2024/03/29 16:11:07
From:    Kan Yabumoto           tech@xxcopy.comTo:      XXCOPY userSubject: File Attributes: what are they and how to use them.Date:    2000-01-17===============================================================================Since XXCOPY has features which interact with the file attributes,a discussion of XXCOPY cannot be complete without touching the basicsof the file attributes.  This article presents the fundamentals ofthe file attributes and how XXCOPY manipulates them.  In addition,some common techniques with the file attributes are discussed.What are the file attributes?The operating systems (DOS, Win95/98, WinNT/2000/XP) maintain certainproperties associated with every file and directories in addition tothe file contents (data inside the file).  Such properties includefile size in bytesfile date/time (creation time, last-modify time, last-access time)file attributesarchive bit   (shows that the file has not been archived yet)read-only bit (write-protect the file)directory bit (distinguishes a directory from a file)hidden bit    (hides from an ordinary directory listing)system bit    (denotes a system file whatever that means)These properties are mostly maintained by various components ofthe operating system and they are handled automatically.  So,the users may not be concerned with them.  However,  manipulatingsome of the properties are not very difficult and it can even bequite useful at times.  This article shows some common techniquesassociated with the file attributes.The file size is always determined by the actual size of the fileand the only way to change it is to increase by appending morebytes to the existing file, or to decrease by overwriting with ashorter file.  The file date/time can be more easily changed.Since changing any of the three date/time values is supported byWin32 file I/O API, one can write a program to change them.  In fact,there are many utility programs available to do so.  Here, we willdiscuss the file attributes with greater details.What is the file attributes?Although one can say the file size and the file date/time are fileattributes (i.e., any properties associated with a file other thanthe file contents), with the more narrow definition and popularusage, the file attributes are collection of flags stored as a bitmapped 32-bit quantity which describes various aspects of the file.The original MS-DOS file attributes had only 8 bits in the fileattributes, the Win32 file attributes are stored as a 32-bit quantity.Although the exact bit positions of the file attributes are notofficially guaranteed to remain the same by Microsoft, they willprobably never be changed.Bit 0     Read-OnlyBit 1     HiddenBit 2     SystemBit 3     Volume LabelBit 4     DirectoryBit 5     ArchiveBit 3 (Volume Label) was used in MS-DOS to store the volume labeljust like an ordinary file in the directory.  Bit 4 (Directory)distinguish a file from a directory.  These two bits cannot bemodified easily.  But, the remaining four bits (bit 0, 1, 2, and 5)can be examined and/or modified by the ATTRIB.EXE utility which issupplied by DOS/Windows.The official Microsoft's way to manipulate the file attributes.Windows 95/98 provide a system utility program, ATTRIB.EXE whichis usually stored in the C:\Windows\command\ directory.Here is the ATTRIB utility's command syntaxATTRIB [+R | -R] [+A | -A] [+S | -S] [+H | -H] filespec [/S]+   Sets an attribute.-   Clears an attribute.R   Read-only file attribute.A   Archive file attribute.S   System file attribute.H   Hidden file attribute./S  Processes files in all directories in the specified path.For example if you want to clear the Archive attribute bit of allthe files in a directory, execute the following command line.ATTRIB  -A c:\mydir\*The Read-Only attribute.This bit is useful to make a file write-protected by software.For example, by setting the \AUTOEXEC.BAT file Read-only, you mayprotect the file from accidental deletion.  Or you may set afew files with the Read-only attributes and delete all the filesusing a "DEL *" (Delete all) command, the read only files willnot be deleted under normal circumstances.  Certain files in theWin95/98 system are kept as Read-only by default (e.g., the systemregistry files).Before you can delete or overwrite a Read-only file, you mustremove the Read-only file attribute bit.XXCOPY can force overwriting (or deletion) of a Read-only file bythe /R switch.The Hidden and System attribute.The purpose of the Hidden attribute bit is to make the fileinvisible in certain applications' file list display.  Since manyfile applications has the feature to ignore the Hidden attributebit, the file with the Hidden attribute bit is not always invisible.The System attribute bit is probably the least rigorouslydefined in its usage.  From the early days of MS-DOS, the Systemattribute bit has been used in inconsistent manners that the bitseldom has much meaning.  The DIR command treat the Systemattribute similarly to the Hidden bit for directory listing.But, the COPY command does not care whether a file has the Systemattribute or not for copying it (Interestingly the DIR and COPYcommands are both "built-in" commands which is implemented withinthe COMMAND.COM program).In most Win95/98 systems, you will find about twenty files in theroot directory which are marked both Hidden and System.  These twoattributes are often go hand in hand.  But, the choice seemsquite arbitrary.While the usage of the Hidden and the System attribute bits arenot well defined, the presence (or absence) of these attributebits seldom change the actions of most system utilities except forthe DIR and COPY commands, in most case, removing the Hidden andSystem attributes on most files do not cause any harm (exceptmaybe some layer of protection from accidental erasure).XXCOPY by default (/H0) ignores a file with either Hidden or Systemattribute.  With the /H switch, you may include files with theHidden or the System attributes in XXCOPY's copy action.The Archive attribute.The Archive attribute was first introduced by MS-DOS version 2.0when the XCOPY utility was also created.  Therefore, the Archiveattribute is probably best explained by how XCOPY handles theArchive attribute.  The purpose of the Archive attribute was clearlyto quickly determine whether a file requires a back up (archiving).The Archive attribute is set whenever an existing file is eitheroverwritten or modified (i.e., renamed or moved to another directory)by the file system.  A new file is usually created with the Archiveattribute set.  The idea is to have a utility or application programto take the responsibility of clearing the Archive bit when afile is successfully backed up.  The next system backup operationwill be made much more efficient by focusing only on the files withthe Archive attribute bit set which are either newly created ormodified in any way since all the Archive attributes are cleared thelast time (i.e., the last backup time).Incremental Backup using the Archive attribute.When XCOPY was the only "officially" designated system archive utilityin MS-DOS, the Archive attribute had its usefulness.  Or, if onlyone backup regime in the system manipulates the Archive attribute bitand no other programs modifies the bit, the scheme works well.However, there are many backup and archive utilities that arecapable of clearing the Archive attribute.Unfortunately, the operating systems do not enforce this"only-one-program-can-modify-the-Archive-bit" policy.  Because ofpossible interference with other applications which might clear theArchive attribute, we consider that the incremental backup schemebased upon the Archive attribute too risky to depend upon.Therefore, we do not recommend the use of the /M switch to performany system backup operation.  For an incremental backup,  XXCOPY's/BI switch performs similar function with more confidence bycomparing the files in the source and the destination with regardto the file size AND file date/time.  If either of the two does notmatch (or the file does not exist in the destination), the file willbe copied.  This method is nearly as efficient as looking at theArchive bit.Other usage of the Archive attribute.1.  You can determine which file(s) an application makes filechanges (creation and modification) in a directory.  First,clear the Archive attribute of all the files in the givendirectory and run the application.  Then, check to see whichfiles are marked by the Archive attribute.E.g.:  ATTRIB  -A c:\mydir\*       // clear the A bit firstRun your application nextXXCOPY  C:\mydir\*  /A/L    // list files with A bit set2.  Say, you have a directory with many files which you want tocopy to a set of diskettes (one diskette cannot hold all ofthe files).  You can first set all the files with Archiveattribute set first.  Then, run XXCOPY /M (or XCOPY /M).  Thecopy job will terminate as soon as the diskette becomes full.But the files which has already been copied are cleared of theArchive attribute.  So, run the same XXCOPY /M command againwith a new diskette.  This time, the files which has been copiedwill be skipped.  Just repeat this operation until the fileswith Archive attribute run out.E.g.:  ATTRIB +A C:\mydir\*        // set the A bit setXXCOPY C:\mydir\*  A:\ /M   // copy files with A bitXXCOPY C:\mydir\*  A:\ /M   // repeat for next diskette...                       // continue until all files...                       // are copied.Full Backup using the Archive attribute.We have stated that the use of the Archive attribute is not suitablefor a reliable incremental backup.   However, once we abandon thenotion that the Archive attribute serves as a persistent marker, itbecomes even more useful.  Yes, the Archive bit as a temporary markercan be quite convenient.  The example in the preceding section ofdirectory-copy to a set of diskette is a backup in a small scale.When you run Microsoft's ATTRIBUTE utility,ATTRIB  +A  C:\*  /SYou will encounter the following messages:Not resetting hidden file C:\WINDOWS\......Sharing violation reading drive CAbort, Retry, Fail?In this case, entering "F" should let you continue.  But, apparently,the ATTRIB utility treats the "Fail" option as "Abort".  This isprovably a bug in the Microsoft program.Our XXCOPY behaves more predictably.XXCOPY  C:\*  /S /AAThe /AA and /AZ switches make XXCOPY to perform the same functionsimilar to the ATTRIB utility does except it does a better job.XXCOPY's /AA switch sets (/AZ clears) the Archive attribute bitincluding the hidden files (it has automatically set the /H switchto include hidden files) and handles share-violation gracefully.If you need speed, you may suppress the console output by /Q or /Q2.XXCOPY  C:\*  /S /AA /Q    ;shows only the files which changesXXCOPY  C:\*  /S /AA /Q2   ;shows only statistics at the endAfter setting the Archive attribute bit of all the files in thevolume, you can start repeated backup using the /M switchXXCOPY  C:\*  T:\mybackup   /S /M /H ...(change the target media when it is full and try again)You may apply the same basic technique to a full-scale volume backupusing the Archive attribute.  This is quite useful when you arebacking up a large volume into smaller removable medium(e.g., CD-R, CD-RW, Tape-based file system, or even floppy disk).If you have an AutoLoader tape drive (e.g., HP SureStore 12000),a working batch file example based on this scheme is available athttp://www.datman.com/tbul/dmtb_038.htm.List of XXCOPY's file attributes related switches.Archive bit/A0   Cancels *ALL* /A, /M, /AT, and /AX switches./A    Excludes files without Archive bit set/AC   Ignores the Archive bit (always clears Archive bit)/M    Excludes files without Archive bit set (clears Archive bit)/AA   Sets   the src file archive bit (without actually copying)/AZ   Clears the src file archive bit (without actually copying)Note: Both /AA and /AZ implicitly set /H (can be overridden)Hidden and System bit/H0   Excludes files with Hidden or System Attributes bits (default)/Ho   Excludes files without Hidden or System Attributes bits./H    Ignores the Hidden or System Attributes bitsDestination Read Only bit/R0   Excludes a file when it exists in dst as read-only (default)/R    Allows overwrite/delete of a read-only file if necessaryDestination file attributes/K0   Keeps H-bit and S-bit, sets A-bit, clears R-bit (default)./KS   Keeps the source attributes including the read-only bit./KD   Keeps the attributes of destination (overwritten) file/KN   Sets the destination attributes to normal (only the A-bit)

© Copyright 2011 Pixelab, Inc. All rights reserved.