programming4us
programming4us
ENTERPRISE

Windows System Programming : Example: Listing File Attributes & Setting File Times

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/28/2011 3:43:23 PM

Example: Listing File Attributes

It is now time to illustrate the file and directory management functions. Program 1, lsW, shows a limited version of the UNIX ls directory listing command, which is similar to the Windows DIR command. lsW can show file modification times and the file size, although this version gives only the low order of the file size.

The program scans the directory for files that satisfy the search pattern. For each file located, the program shows the file name and, if the -l option is specified, the file attributes. This program illustrates many, but not all, Windows directory management functions.

The bulk of Program 1 is concerned with directory traversal. Notice that each directory is traversed twice—once to process files and again to process subdirectories—in order to support the -R recursive option.

Program 1, as listed here, will properly carry out a command with a relative pathname such as:

lsW -R include\*.h

It will not work properly, however, with an absolute pathname such as:

lsW -R C:\Projects\ls\Debug\*.obj

because the program, as listed, depends on setting the directory relative to the current directory. The complete solution (in Examples) analyzes pathnames and will also carry out the second command.

An exercise suggests modifying this program to remove the SetCurrentDirectory calls so as to avoid the risk of program failures leaving you in an unexpected state.

Run 1. lsW: Listing Files and Directories


Example: Setting File Times

Program 2 implements the UNIX touch command, which changes file access and modifies times to the current value of the system time. 

Program 2. touch: Setting File Times
/* touch [options] files */
#include "Everything.h"

int _tmain(int argc, LPTSTR argv[])
{
FILETIME newFileTime;
LPFILETIME pAccessTime = NULL, pModifyTime = NULL;
HANDLE hFile;
BOOL setAccessTime, setModTime, NotCreateNew, maFlag;
DWORD createFlag;
int i, fileIndex;

fileIndex = Options(argc, argv, _T("amc"),
&setAccessTime, &setModTime, &NotCreateNew, NULL);

maFlag = setAccessTime || setModTime;
createFlag = NotCreateNew ? OPEN_EXISTING : OPEN_ALWAYS;

for (i = fileIndex; i < argc; i++) {
hFile = CreateFile(argv[i], GENERIC_READ | GENERIC_WRITE, 0,
NULL, createFlag, FILE_ATTRIBUTE_NORMAL, NULL);
/* Get current system time and convert to a file time.
Do not change the create time. */
GetSystemTimeAsFileTime(&newFileTime);
if (setAccessTime || !maFlag) pAccessTime = &newFileTime;
if (setModTime || !maFlag) pModifyTime = &newFileTime;
SetFileTime(hFile, NULL, pAccessTime, pModifyTime);
CloseHandle(hFile);
}
return 0;
}


The program uses GetSystemTimeAsFileTime, which is more convenient than calling GetSystemTime  followed by SystemTimeToFileTime. See MSDN for more information, although these functions are straightforward.

Run 2 shows touch operation, changing the time of an existing file and creating a new file.

Run 2. touch: Changing File Time and Creating New Files
Other  
  •  Windows System Programming : File Attributes and Directory Processing
  •  Windows System Programming : File Pointers & Getting the File Size
  •  SharePoint 2010 : Business Intelligence - Excel Services (part 2) - Accessing Excel Services Over SOAP
  •  SharePoint 2010 : Business Intelligence - Excel Services (part 1) - Accessing Excel Services Over REST
  •  SharePoint 2010 : Business Intelligence - Visio Services
  •  Exchange Server 2010 : Perform Essential Database Management (part 3) - Manage Database Settings
  •  Exchange Server 2010 : Perform Essential Database Management (part 2) - Manage the Transaction Log Files
  •  Exchange Server 2010 : Perform Essential Database Management (part 1) - Manage the Database Files
  •  Architecting Applications for the Enterprise : UML Diagrams (part 3) - Sequence Diagrams
  •  Architecting Applications for the Enterprise : UML Diagrams (part 2) - Class Diagrams
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    programming4us
     
     
    programming4us