65.9K
CodeProject 正在变化。 阅读更多。
Home

更改文件和文件夹日期

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.53/5 (9投票s)

2004年12月12日

1分钟阅读

viewsIcon

63110

downloadIcon

3442

这是一个简单的工具,用于更改任何文件/文件夹的创建、修改和访问日期。

Sample Image - FileDate.jpg

引言

有很多工具可以获取文件日期,但此工具将设置您指定的任何日期到任何文件或文件夹。 通过研究此工具,您将学习到

  • 操作文件/系统日期时间
  • 递归查找文件
  • 在编辑框中实现拖放功能

设置日期和时间

当您单击应用时,您指定的新日期和时间将设置为所选文件或文件夹。 使用 SetFileTime API 将日期和时间设置为文件/文件夹。 设置文件夹及其内部文件的日期和时间是一件棘手的事情。 在这里,我使用了 FindFirstFileFindNextFile 来递归查找所有文件夹和文件,同时为它们设置新的日期和时间。 以下是代码:

// its an independent function, will set the date and time
///   its a core function in this tool
int SetFileDateTime(char *szFilePath, 
            FILETIME ftCreated, 
            FILETIME ftModified, 
            FILETIME ftAccessed)
{
  DWORD dwAttr;
  dwAttr = GetFileAttributes(szFilePath);
    
  if(g_bIgnoreAttrAndApply)
  {//to ignore mean resets the file/folder attributes to normal
   //         and again it will be set back

      if(FILE_ATTRIBUTE_HIDDEN&dwAttr)
          if(!g_bAttrHidden)
              return 0;
      if(FILE_ATTRIBUTE_READONLY&dwAttr)
          if(!g_bAttrReadOnly)
              return 0;
      if(FILE_ATTRIBUTE_SYSTEM&dwAttr)
          if(!g_bAttrSystem)
              return 0;
      if(FILE_ATTRIBUTE_COMPRESSED&dwAttr)
          if(!g_bAttrCompressed)
              return 0;
      if(FILE_ATTRIBUTE_ENCRYPTED&dwAttr)
          if(!g_bAttrEncrypted)
              return 0;

      SetFileAttributes(szFilePath,FILE_ATTRIBUTE_NORMAL);
  }
  else
  {//if not to ignore, then the following 
   //attributed file/folder can not be set to new file x date time

      if(FILE_ATTRIBUTE_HIDDEN&dwAttr)
          return 0;
      if(FILE_ATTRIBUTE_READONLY&dwAttr)
          return 0;
      if(FILE_ATTRIBUTE_SYSTEM&dwAttr)
          return 0;
      if(FILE_ATTRIBUTE_COMPRESSED&dwAttr)
          return 0;
      if(FILE_ATTRIBUTE_ENCRYPTED&dwAttr)
          return 0;
  }

  // open the file/folder
  HANDLE hFile = CreateFile(szFilePath,
        GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS,
        NULL);

  if(hFile == INVALID_HANDLE_VALUE)
      return 1;

    //set date time
    BOOL bVal = 
         SetFileTime(hFile,&ftCreated,&ftAccessed,&ftModified);
    if(!bVal)
    {
      char *szBuff = (char*)malloc(sizeof(char)*1024);
      LPVOID lpMsgBuf;
      int err = GetLastError();

      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
              NULL, 
              err, 
              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
              (LPTSTR) &lpMsgBuf, 
              0, 
              NULL);

      sprintf(szBuff,"Unable to set date to \n  %s \n\nReason:%s \nError No:%d",
              szFilePath,lpMsgBuf,err),

      MessageBox(NULL,(LPTSTR)lpMsgBuf, "GetLastError", MB_OK|MB_ICONERROR);
              LocalFree(lpMsgBuf);
    }

  // close the handle
      CloseHandle(hFile);
    
  // if it is ignore then it should be set back to its original attribute
  if(g_bIgnoreAttrAndApply)
      SetFileAttributes(szFilePath,dwAttr);
    
  if(!bVal)
      return 2;
  return 0;
}

// it is a recursive function will 
//                go through the files and folders within any folder
int SetDateTimeToFolderFiles(HANDLE hSearchedFile, 
             char *szPathName, 
             FILETIME ftCreated, 
             FILETIME ftModified, 
             FILETIME ftAccessed)
{
    WIN32_FIND_DATA stFindData;
    char *szCurFilename = (char*)malloc(sizeof(char)*_MAX_PATH);
    char *szCurPathname = (char*)malloc(sizeof(char)*_MAX_PATH);

    strcpy(szCurFilename,szPathName);
    strcpy(szCurPathname,szPathName);

    if((hSearchedFile == NULL)||(hSearchedFile ==INVALID_HANDLE_VALUE))
    {// start the search
        sprintf(szCurFilename,"%s\\*.*",szPathName);

    hSearchedFile = FindFirstFile(szCurFilename,&stFindData);
    if(hSearchedFile == INVALID_HANDLE_VALUE)
        return 0;
    }
    else
    {
        if(!FindNextFile(hSearchedFile,&stFindData))
        {// if no more file exists close it
            FindClose(hSearchedFile);
            return 0;
        }
    }

    strcat(szCurPathname,"\\");
    strcat(szCurPathname,stFindData.cFileName);

    if(stFindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
    {// if it is a folder then ....
        if((!stricmp(stFindData.cFileName,"."))||
            (!stricmp(stFindData.cFileName,".."))||
            (!stricmp(stFindData.cFileName,""))||
            (!stricmp(stFindData.cFileName,"")))
        {
            // call it recursively
            return SetDateTimeToFolderFiles(hSearchedFile, 
                            szPathName, 
                            ftCreated,
                            ftModified, 
                            ftAccessed);
        }
        
        // call the set function for the found folder
        SetFileDateTime(szCurPathname, ftCreated, ftModified, ftAccessed);

        // call it recursively
        SetDateTimeToFolderFiles(    NULL, 
                    szCurPathname, 
                    ftCreated, 
                    ftModified,
                    ftAccessed);
    }

    // control will come here only if the found one is a file


    //  set the date time to the file
    SetFileDateTime(szCurPathname, ftCreated, ftModified, ftAccessed);

    // call it recursively
    return SetDateTimeToFolderFiles(hSearchedFile, 
                    szPathName, 
                    ftCreated,
                    ftModified, 
                    ftAccessed);
}

具有特殊属性的文件无法设置为新的日期和时间。 应该将其重置为 FILE_ATTRIBUTE_NORMAL 才能设置新的日期和时间。 如果用户未选中复选框也应用于具有特殊属性的文件,则在处理时将忽略这些具有特殊属性的文件。 这为该工具带来了新功能。 用户可以在进行批量数据和时间设置时忽略特殊的系统文件。

获取日期和时间

当您单击刷新时,将检索所选文件夹或文件的日期和时间并填充。 以下代码将执行此操作...

// refresh will get date and time from folder/file and
//   CdateTimeCtrl will be populated with it
void CFileDateDlg::OnBtnRefresh() 
{
    CString cszPath;
    m_edtPath.GetWindowText(cszPath);

    FILETIME ftCreated, ftModified, ftAccessed;
    BOOL bRet = GetFileDateTime(cszPath.GetBuffer(0),
                    ftCreated, 
                    ftModified, 
                    ftAccessed);
    cszPath.ReleaseBuffer();

    ((CDateTimeCtrl*)GetDlgItem(IDC_DATECREATED))->SetTime(ftCreated);
    ((CDateTimeCtrl*)GetDlgItem(IDC_DATEMODIFIED))->SetTime(ftModified);
    ((CDateTimeCtrl*)GetDlgItem(IDC_DATEACCESSED))->SetTime(ftAccessed);

    ((CDateTimeCtrl*)GetDlgItem(IDC_TIMECREATED))->SetTime(ftCreated);
    ((CDateTimeCtrl*)GetDlgItem(IDC_TIMEMODIFIED))->SetTime(ftModified);
    ((CDateTimeCtrl*)GetDlgItem(IDC_TIMEACCESSED))->SetTime(ftAccessed);

}

结论

希望您觉得这很有用。 欢迎随时修改此工具。 我还要提醒您,我对因该工具造成的任何损害概不负责。 谢谢。

© . All rights reserved.