📄 dir.c
字号:
}static INTConvertULargeInteger (ULARGE_INTEGER num, LPTSTR des, INT len){ TCHAR temp[32]; INT c = 0; INT n = 0; if (num.QuadPart == 0) { des[0] = _T('0'); des[1] = _T('\0'); n = 1; } else { temp[31] = 0; while (num.QuadPart > 0) { if (((c + 1) % (nNumberGroups + 1)) == 0) temp[30 - c++] = cThousandSeparator; temp[30 - c++] = (TCHAR)(num.QuadPart % 10) + _T('0'); num.QuadPart /= 10; } for (n = 0; n <= c; n++) des[n] = temp[31 - c + n]; } return n;}static VOIDPrintFileDateTime (LPSYSTEMTIME dt, DWORD dwFlags){ WORD wYear = (dwFlags & DIR_FOUR) ? dt->wYear : dt->wYear%100; switch (nDateFormat) { case 0: /* mmddyy */ default: ConOutPrintf (_T("%.2d%c%.2d%c%d"), dt->wMonth, cDateSeparator, dt->wDay, cDateSeparator, wYear); break; case 1: /* ddmmyy */ ConOutPrintf (_T("%.2d%c%.2d%c%d"), dt->wDay, cDateSeparator, dt->wMonth, cDateSeparator, wYear); break; case 2: /* yymmdd */ ConOutPrintf (_T("%d%c%.2d%c%.2d"), wYear, cDateSeparator, dt->wMonth, cDateSeparator, dt->wDay); break; }#if 0 switch (nTimeFormat) { case 0: /* 12 hour format */ default: ConOutPrintf (_T(" %2d%c%.2u%c"), (dt->wHour == 0 ? 12 : (dt->wHour <= 12 ? dt->wHour : dt->wHour - 12)), cTimeSeparator, dt->wMinute, (dt->wHour <= 11 ? 'a' : 'p')); break; case 1: /* 24 hour format */ ConOutPrintf (_T(" %2d%c%.2u"), dt->wHour, cTimeSeparator, dt->wMinute); break; }#endif}/* * print_summary: prints dir summary * Added by Rob Lake 06/17/98 to compact code * Just copied Tim's Code and patched it a bit * */static INTPrintSummary (LPTSTR szPath, ULONG ulFiles, ULONG ulDirs, ULARGE_INTEGER bytes, LPINT pLine, DWORD dwFlags){ TCHAR buffer[64]; if (dwFlags & DIR_BARE) return 0; /* print number of files and bytes */ ConvertULong (ulFiles, buffer, sizeof(buffer)); ConOutPrintf (_T(" %6s File%c"), buffer, ulFiles == 1 ? _T(' ') : _T('s')); ConvertULargeInteger (bytes, buffer, sizeof(buffer)); ConOutPrintf (_T(" %15s byte%c\n"), buffer, bytes.QuadPart == 1 ? _T(' ') : _T('s')); if (IncLine (pLine, dwFlags)) return 1; /* print number of dirs and bytes free */ ConvertULong (ulDirs, buffer, sizeof(buffer)); ConOutPrintf (_T(" %6s Dir%c"), buffer, ulDirs == 1 ? _T(' ') : _T('s')); if (!(dwFlags & DIR_RECURSE)) { ULARGE_INTEGER uliTotalAvailable, uliTotalBytes,uliTotalFree; TCHAR szRoot[] = _T("\\"); szRoot[0] = szPath[0]; GetDiskFreeSpaceEx (szRoot, &uliTotalAvailable, &uliTotalBytes, &uliTotalFree); ConvertULargeInteger (uliTotalFree, buffer, sizeof(buffer)); ConOutPrintf (_T(" %15s bytes free\n"), buffer); } if (IncLine (pLine, dwFlags)) return 1; return 0;}/* * dir_list * * list the files in the directory */static INTDirList (LPTSTR szPath, LPTSTR szFilespec, LPINT pLine, DWORD dwFlags){ TCHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA file; ULARGE_INTEGER bytecount; FILETIME ft; SYSTEMTIME dt; HANDLE hFile; TCHAR buffer[32]; ULONG filecount = 0; ULONG dircount = 0; INT count; CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo; WORD wAttribute; bytecount.QuadPart = 0; _tcscpy (szFullPath, szPath); if (szFullPath[_tcslen(szFullPath) - 1] != _T('\\')) _tcscat (szFullPath, _T("\\")); _tcscat (szFullPath, szFilespec); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ScreenBufferInfo); hFile = FindFirstFile (szFullPath, &file); if (hFile == INVALID_HANDLE_VALUE) { /* Don't want to print anything if scanning recursively * for a file. RL */ if ((dwFlags & DIR_RECURSE) == 0) { FindClose (hFile); error_file_not_found (); if (IncLine (pLine, dwFlags)) return 0; return 1; } FindClose (hFile); return 0; } /* moved down here because if we are recursively searching and * don't find any files, we don't want just to print * Directory of C:\SOMEDIR * with nothing else * Rob Lake 06/13/98 */ if ((dwFlags & DIR_BARE) == 0) { ConOutPrintf (_T(" Directory of %s\n"), szPath); if (IncLine (pLine, dwFlags)) return 1; ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags)) return 1; } /* For counting columns of output */ count = 0; do { /* next file, if user doesn't want all files */ if (!(dwFlags & DIR_ALL) && ((file.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) || (file.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))) continue; wAttribute = ScreenBufferInfo.wAttributes; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wAttribute); if (dwFlags & DIR_WIDE && (dwFlags & DIR_BARE) == 0) { ULARGE_INTEGER uliSize; if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _stprintf (buffer, _T("[%s]"), file.cFileName); dircount++; } else { _stprintf (buffer, _T("[%s]"), file.cFileName); filecount++; } ConOutPrintf (_T("%-15s"), buffer); count++; if (count == 5) { /* output 5 columns */ ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags)) return 1; count = 0; } uliSize.LowPart = file.nFileSizeLow; uliSize.HighPart = file.nFileSizeHigh; bytecount.QuadPart += uliSize.QuadPart; } else if (dwFlags & DIR_BARE) { ULARGE_INTEGER uliSize; if (_tcscmp (file.cFileName, _T(".")) == 0 || _tcscmp (file.cFileName, _T("..")) == 0) continue; if (dwFlags & DIR_RECURSE) { TCHAR dir[MAX_PATH]; _tcscpy (dir, szPath); _tcscat (dir, _T("\\")); if (dwFlags & DIR_LWR) _tcslwr (dir); ConOutPrintf (dir); } ConOutPrintf (_T("%-13s\n"), file.cFileName); if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) dircount++; else filecount++; if (IncLine (pLine, dwFlags)) return 1; uliSize.LowPart = file.nFileSizeLow; uliSize.HighPart = file.nFileSizeHigh; bytecount.QuadPart += uliSize.QuadPart; } else { if (dwFlags & DIR_NEW) { /* print file date and time */ if (FileTimeToLocalFileTime (&file.ftLastWriteTime, &ft)) { FileTimeToSystemTime (&ft, &dt); PrintFileDateTime (&dt, dwFlags); } /* print file size */ if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { wAttribute = (ScreenBufferInfo.wAttributes & 0xF0) | FOREGROUND_GREEN | FOREGROUND_INTENSITY; ConOutPrintf (_T(" <DIR> ")); dircount++; }else { ULARGE_INTEGER uliSize; if (file.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) { wAttribute = (ScreenBufferInfo.wAttributes & 0xF0) | FOREGROUND_BLUE | FOREGROUND_INTENSITY; }else { wAttribute = ScreenBufferInfo.wAttributes; } uliSize.LowPart = file.nFileSizeLow; uliSize.HighPart = file.nFileSizeHigh; ConvertULargeInteger (uliSize, buffer, sizeof(buffer)); ConOutPrintf (_T("%11s"), buffer); bytecount.QuadPart += uliSize.QuadPart; filecount++; } /* print long filename */ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wAttribute); ConOutPrintf (_T(" %s\n"), file.cFileName); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ScreenBufferInfo.wAttributes); } else { if (file.cFileName[0] == _T('.')) ConOutPrintf (_T("%-13s "), file.cFileName); else { TCHAR szShortName[13]; LPTSTR ext; _tcsncpy (szShortName, file.cFileName, 13); ext = _tcschr (szShortName, _T('.')); if (!ext) ext = _T(""); else *ext++ = _T('\0'); ConOutPrintf (_T("%-8s %-3s "), szShortName, ext); } /* print file size */ if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { ConOutPrintf (_T("%-14s"), _T("<DIR>")); dircount++; } else { ULARGE_INTEGER uliSize; uliSize.LowPart = file.nFileSizeLow; uliSize.HighPart = file.nFileSizeHigh; ConvertULargeInteger (uliSize, buffer, sizeof(buffer)); ConOutPrintf (_T(" %10s "), buffer); bytecount.QuadPart += uliSize.QuadPart; filecount++; } /* print file date and time */ if (FileTimeToLocalFileTime (&file.ftLastWriteTime, &ft)) { FileTimeToSystemTime (&ft, &dt); PrintFileDateTime (&dt, dwFlags); } /* print long filename */ ConOutPrintf (_T(" %s\n"), file.cFileName); } if (IncLine (pLine, dwFlags)) return 1; } } while (FindNextFile (hFile, &file)); FindClose (hFile); /* Rob Lake, need to make clean output */ /* JPP 07/08/1998 added check for count != 0 */ if ((dwFlags & DIR_WIDE) && (count != 0)) { ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags)) return 1; } if (filecount || dircount) { recurse_dir_cnt += dircount; recurse_file_cnt += filecount; recurse_bytes.QuadPart += bytecount.QuadPart; /* print_summary */ if (PrintSummary (szPath, filecount, dircount, bytecount, pLine, dwFlags)) return 1; } else { error_file_not_found (); // return 1; } return 0;}/* * _Read_Dir: Actual function that does recursive listing */static INTDirRead (LPTSTR szPath, LPTSTR szFilespec, LPINT pLine, DWORD dwFlags){ TCHAR szFullPath[MAX_PATH]; WIN32_FIND_DATA file; HANDLE hFile; _tcscpy (szFullPath, szPath); if (szFullPath[_tcslen (szFullPath) - 1] != _T('\\')) _tcscat (szFullPath, _T("\\")); _tcscat (szFullPath, szFilespec); hFile = FindFirstFile (szFullPath, &file); if (hFile == INVALID_HANDLE_VALUE) { return 0; } do { /* don't list "." and ".." */ if (_tcscmp (file.cFileName, _T(".")) == 0 || _tcscmp (file.cFileName, _T("..")) == 0) continue; if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _tcscpy (szFullPath, szPath); if (szFullPath[_tcslen (szFullPath) - 1] != _T('\\')) _tcscat (szFullPath, _T("\\")); _tcscat (szFullPath, file.cFileName); if (DirList (szFullPath, szFilespec, pLine, dwFlags)) { FindClose (hFile); return 1; } if ((dwFlags & DIR_BARE) == 0) { ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags) != 0) return 1; ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags) != 0) return 1; } if (DirRead (szFullPath, szFilespec, pLine, dwFlags) == 1) { FindClose (hFile); return 1; } } } while (FindNextFile (hFile, &file)); if (!FindClose (hFile)) return 1; return 0;}/* * do_recurse: Sets up for recursive directory listing */static INTDirRecurse (LPTSTR szPath, LPTSTR szSpec, LPINT pLine, DWORD dwFlags){ if (!PrintDirectoryHeader (szPath, pLine, dwFlags)) return 1; if (DirList (szPath, szSpec, pLine, dwFlags)) return 1; if ((dwFlags & DIR_BARE) == 0) { ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags)) return 1; } if (DirRead (szPath, szSpec, pLine, dwFlags)) return 1; if ((dwFlags & DIR_BARE) == 0) ConOutPrintf (_T("Total files listed:\n")); dwFlags &= ~DIR_RECURSE; if (PrintSummary (szPath, recurse_file_cnt, recurse_dir_cnt, recurse_bytes, pLine, dwFlags)) return 1; if ((dwFlags & DIR_BARE) == 0) { ConOutPrintf (_T("\n")); if (IncLine (pLine, dwFlags)) return 1; } return 0;}/* * dir * * internal dir command */INT CommandDir (LPTSTR first, LPTSTR rest){ DWORD dwFlags = DIR_NEW | DIR_FOUR; TCHAR dircmd[256]; TCHAR szPath[MAX_PATH]; TCHAR szFilespec[MAX_PATH]; LPTSTR param; INT nLine = 0; recurse_dir_cnt = 0L; recurse_file_cnt = 0L; recurse_bytes.QuadPart = 0; /* read the parameters from the DIRCMD environment variable */ if (GetEnvironmentVariable (_T("DIRCMD"), dircmd, 256)) { if (!DirReadParam (dircmd, ¶m, &dwFlags)) return 1; } /* read the parameters */ if (!DirReadParam (rest, ¶m, &dwFlags)) return 1; /* default to current directory */ if (!param) param = _T("."); /* parse the directory info */ if (DirParsePathspec (param, szPath, szFilespec)) return 1; if (dwFlags & DIR_RECURSE) { if (IncLine (&nLine, dwFlags)) return 0; if (DirRecurse (szPath, szFilespec, &nLine, dwFlags)) return 1; return 0; } /* print the header */ if (!PrintDirectoryHeader (szPath, &nLine, dwFlags)) return 1; if (DirList (szPath, szFilespec, &nLine, dwFlags)) return 1; return 0;}#endif/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -