CppFiles.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:5k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::IO;
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(int argc, char* argv[])
- #endif
- {
- // Check for required arguments
- if (argc < 2)
- {
- Console::WriteLine(S"Usage: CppFiles [options] [path]");
- return 0;
- }
- String* options = 0;
- String* path = 0;
- bool bGotOptions = false;
- // Split out the arguments
- if (argc == 3)
- {
- bGotOptions = true;
- options = new String(argv[1]);
- path = new String(argv[2]);
- }
- else if (argc == 2)
- path = new String(argv[1]);
- bool bSize = false;
- bool bDate = false;
- bool bAtts = false;
- // If we have options, check them. The default is to list the name only
- // Possible options are:
- // v verbose listing, gives name, size & access time
- // s list size
- // d list last access date
- // a list attributes
- if (bGotOptions)
- {
- options = options->ToLower();
- if (options->IndexOf('v') != -1)
- {
- bSize = true;
- bDate = true;
- bAtts = true;
- }
- else
- {
- if (options->IndexOf('s') != -1) bSize = true;
- if (options->IndexOf('d') != -1) bDate = true;
- if (options->IndexOf('a') != -1) bAtts = true;
- }
- }
- bool bItsAFile = false;
- bool bItsADirectory = false;
- FileInfo* fi = new FileInfo(path);
- DirectoryInfo* di = new DirectoryInfo(path);
- if (fi->Exists)
- bItsAFile = true;
- else if (di->Exists)
- bItsADirectory = true;
- if (!bItsAFile && !bItsADirectory)
- {
- Console::WriteLine(S"No such file or directory");
- return(-1);
- }
- if (bItsAFile)
- {
- Console::Write(fi->Name);
- if (bSize) Console::Write(" {0}", __box(fi->Length));
- if (bDate) Console::Write(" {0}",
- File::GetLastAccessTime(fi->ToString()).ToString());
- if (bAtts)
- {
- FileAttributes fa = File::GetAttributes(fi->ToString());
- Console::Write(" ");
- if (fa & FileAttributes::Normal)
- Console::Write("<normal>");
- else
- {
- if (fa & FileAttributes::Archive) Console::Write("a");
- if (fa & FileAttributes::Hidden) Console::Write("h");
- if (fa & FileAttributes::System) Console::Write("s");
- if (fa & FileAttributes::ReadOnly) Console::Write("r");
- }
- }
- Console::WriteLine();
- }
- else if (bItsADirectory)
- {
- // List the directory contents - subdirs first, then files
- String* dirs[] = Directory::GetDirectories(di->ToString());
- for(int i=0; i<dirs->Count; i++)
- {
- DirectoryInfo* sdi =
- new DirectoryInfo(dirs->get_Item(i)->ToString());
- // Directories list in upper case
- String* dirName = sdi->Name->ToUpper();
- Console::Write("{0,30}",dirName);
- // no size, so put a few blanks
- String* ss = S"--";
- Console::Write("{0,12}",ss);
- // last mod date is OK
- if (bDate) Console::Write(" {0}",
- Directory::GetLastAccessTime(sdi->ToString()).ToString());
- // no attributes, either
- // finish the line
- Console::WriteLine();
- }
- // Now do the files
- String* files[] = Directory::GetFiles(di->ToString());
- for(i=0; i<files->Count; i++)
- {
- FileInfo* fci = new FileInfo(files->get_Item(i)->ToString());
- // Files list in lower case
- String* fileName = fci->Name->ToLower();
- Console::Write("{0,30}",fileName);
- if (bSize) Console::Write("{0,12}", __box(fci->Length));
- if (bDate) Console::Write(" {0}",
- File::GetLastAccessTime(fci->ToString()).ToString());
- // Attributes
- if (bAtts)
- {
- FileAttributes fa = File::GetAttributes(fci->ToString());
- Console::Write(" ");
- if (fa & FileAttributes::Normal)
- Console::Write("<normal>");
- else
- {
- if (fa & FileAttributes::Archive) Console::Write("a");
- if (fa & FileAttributes::Hidden) Console::Write("h");
- if (fa & FileAttributes::System) Console::Write("s");
- if (fa & FileAttributes::ReadOnly) Console::Write("r");
- }
- }
- // finish the line
- Console::WriteLine();
- }
- }
- return 0;
- }