⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydir.c

📁 以前写的一个类似于DOS Dir命令的程序
💻 C
字号:
/**************************************************************************
*                                                                         *
* MyDir.c                                                                 *
*                                                                         *
* Copyright 1999 by Inventec, all rights reserved.                        *
*                                                                         *
* Author:           Mike Jing                                             *
*                                                                         *
* Date:             1999.11.01                                            *
*                                                                         *
* Description:      A simplified version of the DOS internal command DIR. *
*                   Demonstrating how to access DOS files and how to use  *
*                   the wildcard characters.                              *
*                                                                         *
* Write for:        My dear friend, Gao ZhenTing.                         *
*                                                                         *
*-------------------------------------------------------------------------*
*                                                                         *
* Input:    The drive and directory and filename which you want to see.   *
*           [Drive:][\Path\][FileName.Ext]                                *
*           You can specify the parameters in detail, or part of them.    *
*           And only /? option can be recognized by this program.         *
*                                                                         *
*           There are some examples of using this command:                *
*           MyDir /?    <ENTER>  Get the help.                            *
*           MyDir       <ENTER>  List the contents of working directory.  *
*           MyDir C:    <ENTER>  List working directory of drive C:.      *
*           MyDir C:\   <ENTER>  List the root directory of drive C:.     *
*           MyDir *.*   <ENTER>  List all the files of working directory. *
*           MyDir GZT   <ENTER>  List files of GZT.* in working directory.*
*           MyDir .h    <ENTER>  List *.h of your working directory.      *
*                                                                         *
*           There is one difference point from DIR:                       *
*               This program can't deal with directories very well.       *
*               If you type [MyDir sendto], it will not find the sub-dir  *
*               [sendto] in your working directory, it will only look for *
*               files named like [sendto.*].
*                                                                         *
* Output:   Directory list and return value.                              *
*                                                                         *
*           The program will display the files found on the screen and    *
*           pause for every page.                                         *
*                                                                         *
*           Return value of 0 indicates normal end of the program;        *
*           Return value of 1 indicates the program is cancled            *
*           by user with the CTRL_C function key.                         *
*                                                                         *
**************************************************************************/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dir.h>

#define CTRL_C 0x03

int main(int argc, char *argv[])
/*The first parameter [argc] will tell you how many strings is entered in
the command line, and the second parameter will tell you what strings is
entered in the command line.*/

/*For example, if you type the command line like this:
MyDir ABC DEF
and the [argc] will be 3, indicates 3 strings in total, including the
command itself, then [argv] will be a string array the same like this,
char *argv[3]={"MyDir", "ABC", "DEF"};*/

/*So you can get the command line parameters by this way.*/
{
   struct ffblk my_ffblk;
      /*File control block, used for findfirst and findnext function.*/
   int fDone;
      /*If all the files in the directory you specified is listed.*/
   int nScreenCounter;
      /*A coounter indicates how many lines is displayed on the screen.*/
   int nFileCounter;
      /*A coounter indicates how many files is found.*/
   int nKey;
      /*The key user had entered, CTRL_C to abort, others to continue.*/
   int nReturnValue;
      /*Returned value of fnsplit, tells which component is presented
      for the file path -- drive, directory, filename, extension.*/
   int nDrive;
      /*Drive number.*/
   int nTemp, i;
      /*Temporary variables.*/
   char sWorkingPath[MAXPATH];
      /*A string stores the path of your working directory.*/
   char sPath[MAXPATH];
      /*A path generated by fnmerge, the files specified by user.*/
   char sTemp[MAXPATH];
      /*Temporary variables.*/
   char sDrive[MAXDRIVE], sDir[MAXDIR], sFileName[MAXFILE], sExt[MAXEXT];
      /*Split part of a path -- drive, directory, filename, extension.*/
   char sDate[11], sTime[9], sSize[12];
      /*Strings to store the date, time and size information.*/

   /*One parameter means the command line has only the command -- MyDir.*/
   /*So the contents of current working directory will be listed.*/
   /*Suppose your working directory is c:\windows.*/
   if (argc == 1)
   {
      nDrive = getdisk();
         /*Get current disk number, 0 - A, 1 - B, 2 - C, ...*/
      strcpy(sWorkingPath, "X:\\");
      sWorkingPath[0] = 'A' + nDrive;
         /*Now the string is like [C:\].*/
      getcurdir(nDrive + 1, sWorkingPath + 3);
         /*Get the directory of the specified drive, 0 - Current drive, 1 - A.*/
         /*And we can get a string like [C:\WINDOWS].*/
      sWorkingPath[strlen(sWorkingPath)] = '\\';
      sWorkingPath[strlen(sWorkingPath)] = NULL;
         /*Now we have got a string like [C:\WINDOWS\].*/
      fnsplit(sWorkingPath, sDrive, sDir, sFileName, sExt);
         /*Split the path we have got into parts.*/
      fnmerge(sPath, sDrive, sDir, "*", ".*");
         /*And merge them together with the wildcard characters.*/
         /*So the string now is like [C:\WINDOWS\*.*].*/
   }

   /*Two parameters means the command line has the command and an option.*/
   /*So the specified files will be listed.*/
   /*Suppose your you have type a command line like [MyDir D:*.asm].*/
   /*And the working directory of drive D: is [\Working\obj]*/
   if (argc == 2)
   {
      if (((argv[1][0] == '/') || (argv[1][0] == '-'))&&((argv[1][1] == '?') || (argv[1][1] == 'h') || (argv[1][1] == 'H')))
         /*If the user want some help.*/
      {
         fnsplit(argv[0], sDrive, sDir, sFileName, sExt);
         fnmerge(sPath, NULL, NULL, sFileName, sExt);
         printf("%s, write by Mike Jing, 1999.11.1. All rights reserved.\n", sPath);
         printf("Usage:\t%s [Drive:][\\Path\\][FileName.Ext].\n\n", sPath);
         printf("Thank you for use.\n");
         return 0;
      }
      else
      {
         if (argv[1][1] == ':')
            /*If the command line specified a disk drive, turn it into number.*/
            nDrive = (argv[1][0] & 0x1f) - 1;
            /*Study the ASCII table, you will see it. a,A -> 0, b,B -> 1, ...*/
         else
            /*Else get the current disk drive.*/
            nDrive = getdisk();
         strcpy(sWorkingPath, "X:\\");
            /*[X:\].*/
         sWorkingPath[0] = 'A' + nDrive;
            /*[D:\].*/
         getcurdir(nDrive + 1, sWorkingPath + 3);
            /*[D:\WORKING]*/
         nTemp = strlen(sWorkingPath);
         sWorkingPath[nTemp] = '\\';
         sWorkingPath[nTemp + 1] = NULL;
            /*[D:\WORKING\]*/
         nReturnValue = fnsplit(argv[1], sDrive, sDir, sFileName, sExt);
            /*Split the path and see which componet it has.*/
         if ((nReturnValue & EXTENSION) == 0)
            /*If there is not an extension in the path, add a [.*].*/
            strcpy(sExt, ".*");
         if ((nReturnValue & FILENAME) == 0)
            /*If there is not a filename in the path, add a [*].*/
            strcpy(sFileName, "*");
         if ((nReturnValue & DIRECTORY) == 0)
            /*If the user didn't specify the directory, add it with the working directory.*/
            fnsplit(sWorkingPath, sTemp, sDir, sTemp, sTemp);
         if ((nReturnValue & DRIVE) == 0)
            /*If the user didn't specify drive, add it with the working drive.*/
            fnsplit(sWorkingPath, sDrive, sTemp, sTemp, sTemp);
         fnmerge(sPath, sDrive, sDir, sFileName, sExt);
            /*Merge them together, so you get a string like [D:\WORKING\OBJ\*.asm].*/
      }
   }
   nTemp = strlen(sPath);
   for (i = 0; i < nTemp; i++)
      sPath[i] = toupper(sPath[i]);
      /*Turn the characters to uppercase, so you get [D:\WORKING\OBJ\*.ASM].*/
   printf("Directory listing of [%s].\n", sPath);
   fDone = findfirst(sPath,&my_ffblk,0);
      /*Look for the first file like what is specified by user.*/
   /*Initialize the vaariables.*/
   nKey = 0;
   nFileCounter = 0;
   nScreenCounter = 0;
   while ((fDone == 0) && (nKey != CTRL_C))
      /*If the user haven't press CTRL_C and the search is not ended, continue.*/
   {
      sprintf(sDate, "%4d-%02d-%02d", (my_ffblk.ff_fdate >> 9) + 1980, (my_ffblk.ff_fdate >> 5) & 0x0f, (my_ffblk.ff_fdate >> 0) & 0x1f);
         /*Get the date information from file control block and convert it to string.*/
      sprintf(sTime, "%02d:%02d:%02d", (my_ffblk.ff_ftime >> 11), (my_ffblk.ff_ftime >> 5) & 0x3f, (my_ffblk.ff_ftime & 0x1f) * 2);
         /*Get the time information from file control block and convert it to string.*/
      sprintf(sSize, "%3d,%03d,%03d", (int)(my_ffblk.ff_fsize / 1000000) % 1000, (int)(my_ffblk.ff_fsize / 1000) % 1000, (int)my_ffblk.ff_fsize % 1000);
         /*Get the size information from file control block and convert it to string.*/
      printf("%-12s\t%12s\t%s\t%s\n", my_ffblk.ff_name, sSize, sDate, sTime);
      nFileCounter++;
      fDone = findnext(&my_ffblk);
         /*Look for the next file.*/
      if (nScreenCounter++ == 21)
         /*If the screen is full, wait for a key.*/
      {
        nScreenCounter = 0;
        printf("Press any key to continue...\n");
        nKey = getch();
        printf("(Continuing [%s]...)\n", sPath);
      }
   }
   if (nKey == CTRL_C)
      /*If the program is cancled by user, return 1.*/
   {
      printf("^C\n");
      return 1;
   }
   else
   {
      printf("\t%d file(s).", nFileCounter);
         /*Print the number of files found.*/
      return 0;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -