使用工具获取文件名及时间属性到粘贴板—附带github源码

在工作中遇到一个小需求,当项目做完的时候,需要发布产物,每次都需要填写产物的名称和时间属性。比如需要发布文件 release.re, 那么就需要将“release.re 2020-6-27 9:39:10” 填写到发布文档中。通常的做法是,找到release.re文件,然后右键单击—>属性—>复制修改时间。当发布的文件比较多的时候,复制时间就有点麻烦,如果有一个工具,只需要找到这个文件,就可以粘贴所需信息,就省事多了。

根据上述需求,我开发了一个小工具来做这个事情。

修改时间的图片

如上图所示,我们打开这个软件后,只需要将D12.txt拖动到这个工具中,或者点击按钮“选择文件”将D12.txt选中。那么“D12.txt 2020-06-08 21:48:48”就会自动保存到粘贴板上了,这个时候只需要用Ctrl+V即可复制这个时间信息了。

上述时间是修改时间,也可以改源码,改为创建时间,访问时间,或者一次性三个时间都保存到粘贴板上。

 

源码介绍

文末有GitHub源码仓库的地址,这里介绍一下源码。源码比较简单,创建工程的步骤:VisualStudio2010->File->New Project->MFC Application->Application Type, Dialog based->Finish

思路主要包含三步

第一步:获取文件路径,有两个方式,一个是通过点击按钮的方式获取文件路径,另一个方式是通过拖动的方式获取文件路径。

//通过点击按钮的方式获取文件路径
void CgetFilePropertyMFCDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	try
	{ 
		int i=0;
		TCHAR szFilter[] = _T("所有文件(*.*)|*.*||");
		CFileDialog fileDlg(TRUE, _T("txt"), NULL, OFN_ALLOWMULTISELECT, szFilter, this);
 
		TCHAR *pBuffer = new TCHAR[MAX_PATH * 100];
		fileDlg.m_ofn.lpstrFile = pBuffer;
		fileDlg.m_ofn.nMaxFile = MAX_PATH * 100;
		fileDlg.m_ofn.lpstrFile[0] = '\0'; 
		
		CArray<CString, CString> array_filename;

		CString strFilePath; 
		CString strFileNameAndProperties;

		if (IDOK == fileDlg.DoModal())
		{
			POSITION pos_file;
			pos_file = fileDlg.GetStartPosition();
          	 
			while(pos_file != NULL)
			{
				strFilePath = fileDlg.GetNextPathName(pos_file);
				array_filename.Add(strFilePath);
				//SetDlgItemText(IDC_EDIT1,  strFilePath);				
				//SetOneFileNameAndProToClip(strFilePath);	
				 
				if(strFileNameAndProperties.IsEmpty())
				{
					strFileNameAndProperties+=(GetFileNameAndProperties(strFilePath, globalDateType));
				}
				else
				{
					strFileNameAndProperties+=(_T("\r\n")+GetFileNameAndProperties(strFilePath, globalDateType));
				}
			}//end while       
			 			
			SetDlgItemText(IDC_EDIT1, strFileNameAndProperties);	
			SetClipBoardData(strFileNameAndProperties);
		}
	}
	catch(...)
	{
		
	}
}
//通过拖动文件的方式获取文件路径
void CgetFilePropertyMFCDlg::OnDropFiles(HDROP hDropInfo)
{
	// TODO: Add your message handler code here and/or call default
	
	UINT nCount;
    TCHAR szPath[MAX_PATH];
 	CString strFileNameAndProperties;
    nCount = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
    if (nCount)
    {
        for (UINT nIndex = 0; nIndex < nCount; ++nIndex)
        {
            UINT pathlenght=DragQueryFile(hDropInfo, nIndex, szPath, _countof(szPath));
		
			CString filepath(szPath, pathlenght);//change the file path to CString format 
			if(strFileNameAndProperties.IsEmpty())
			{
				strFileNameAndProperties+=(GetFileNameAndProperties(filepath, globalDateType));
			}
			else
			{
				strFileNameAndProperties+=(_T("\r\n")+GetFileNameAndProperties(filepath, globalDateType));
			}
        }
    }
 
	SetDlgItemText(IDC_EDIT1, strFileNameAndProperties);	
	SetClipBoardData(strFileNameAndProperties);
    DragFinish(hDropInfo);
	
	CDialogEx::OnDropFiles(hDropInfo);
}

 

第二步:通过文件路径获取文件名与时间属性


//获取文件名以及时间属性,第二个参数dateType是表示日期格式的,比如年-月-日,日-月-年等
CString CgetFilePropertyMFCDlg::GetFileNameAndProperties(CString strFilePath, int dateType)
{
	FILETIME fileCreateDate, fileModifyDate, fileAccessDate;
	CString strModifyTime;
	SYSTEMTIME systemTimeLocal;
	CString firstFileName;
	CString finalResult;

	CString strDateFormat=_T("%04d-%02d-%02d %02d:%02d:%02d");

	firstFileName = strFilePath.Right(strFilePath.GetLength()-strFilePath.ReverseFind('\\')-1); 
	
	/***************get file date start************************************/
	HANDLE hFile = CreateFile(strFilePath, GENERIC_READ,          // open for reading
	FILE_SHARE_READ,       // share for reading
	NULL,                            // default security
	OPEN_EXISTING,          // existing file only
	FILE_FLAG_BACKUP_SEMANTICS , // normal file
	NULL);
	if (!GetFileTime(hFile, &fileCreateDate, &fileAccessDate, &fileModifyDate))
	{
		return _T("");
	}

	ZeroMemory(&systemTimeLocal, sizeof(SYSTEMTIME));
	FileTimeToSystemTime(&fileModifyDate, &systemTimeLocal);
	
	switch(dateType)
	{
		case YEAR_MONTH_DAY:
			strDateFormat=_T("%04d-%02d-%02d %02d:%02d:%02d");
			strModifyTime.Format(strDateFormat, systemTimeLocal.wYear, systemTimeLocal.wMonth, systemTimeLocal.wDay,  systemTimeLocal.wHour+8, systemTimeLocal.wMinute, systemTimeLocal.wSecond); 
			break;

		case MONTH_DAY_YEAR:
			strDateFormat=_T("%02d-%02d-%04d %02d:%02d:%02d");
			strModifyTime.Format(strDateFormat,  systemTimeLocal.wMonth, systemTimeLocal.wDay, systemTimeLocal.wYear, systemTimeLocal.wHour+8, systemTimeLocal.wMinute, systemTimeLocal.wSecond); 
			break;

		case DAY_MONTH_YEAR:
			strDateFormat=_T("%02d-%02d-%04d %02d:%02d:%02d");
			strModifyTime.Format(strDateFormat, systemTimeLocal.wDay, systemTimeLocal.wMonth, systemTimeLocal.wYear, systemTimeLocal.wHour+8, systemTimeLocal.wMinute, systemTimeLocal.wSecond); 
			break;

		default:
			break;
	}	

	finalResult=firstFileName+_T(" ")+strModifyTime;
	return finalResult;
}

第三步:将获取到的文件名与时间属性存储到粘贴板


void  CgetFilePropertyMFCDlg::SetClipBoardData(CString cStringText)
{
    if (::OpenClipboard(m_hWnd) &&::EmptyClipboard())
    {
        size_t cStringTextLength = (cStringText.GetLength() + 1) * sizeof(TCHAR);

        HGLOBAL allocHeapMemory = GlobalAlloc(GMEM_MOVEABLE, cStringTextLength);

        if (allocHeapMemory == NULL)
        {
            CloseClipboard();
            return;
        }

        LPTSTR lptstrDst = (LPTSTR)GlobalLock(allocHeapMemory);

        memcpy_s(lptstrDst, cStringTextLength, cStringText.LockBuffer(), cStringTextLength);
        cStringText.UnlockBuffer();

        GlobalUnlock(allocHeapMemory);
 
        UINT uintFormat = (sizeof(TCHAR) == sizeof(WCHAR))?CF_UNICODETEXT:CF_TEXT;
        
        if(SetClipboardData(uintFormat, allocHeapMemory) == NULL)
        {
            CloseClipboard();
            return;
        }

        CloseClipboard();
    }
}

 

工具下载地址:https://download.csdn.net/download/qingtianjushi/12554974

SourceCode: https://github.com/ensky16/getFileProperty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ensky.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值