批量文件名加前缀

来源:互联网 发布:知乎 好奇心诊所 编辑:程序博客网 时间:2024/06/05 01:04

前言

今天开始看新的资料了, 文件夹里一坨文件, 不到500个.
因为不是一天的事情, 很有可能以后再继续学习时, 找不到下一个该学习哪个.
据我自己的经验, 先要给这些文件, 加一个数字前缀. 看完一个, 就记录我看完这个了. 等下回, 我就找下一个序号来学习.
我就习惯性的开始给文件改名, 改了几十个,突然意识到,不能手工这么玩…
去codeproject上找文件改名的工程, 很多, 很先进(支持正则表达式).
但是我不需要那么先进的功能, 只需要给文件改名, 加一个数字前缀而以.
用到的API不多 : FindFirstFile, MoveFile, FindNextFile, FindClose.
就自己写了一个, 吃完晚饭, 边写边调试, 一个小时完成.
写的工具支持拖拽(文件或文件夹), 只给文件加前缀序号, 文件夹不动, 一键搞定.
自己写几句代码做重复的事情, 还是有点幸福的感觉:)

工程下载点

srcFileNameAddSn.zip
编译环境 : vc6sp6

UI

这里写图片描述

记录

// hw.cpp : Defines the entry point for the application.//#include "stdafx.h"#include <windows.h>#include <tchar.h>#include <stdio.h>#pragma comment(lib, "user32.lib")#include <string>#include <Shellapi.h>#pragma comment(lib, "Shell32.lib")#include "resource.h"#include "constDefine.h"HINSTANCE g_hInstance = NULL;HWND g_hMainWnd = NULL;char g_szSrcFile[MAXBYTE] = {'\0'};LRESULT CALLBACK    MainDlgProc(HWND, UINT, WPARAM, LPARAM);void fnCenterWindow(HWND hWnd);void fnSetWndText(HWND hWnd, const TCHAR* pcTip);void fnSetCtrlText(int iCtrlId, const TCHAR* pcTip);BOOL fnIsFileExist(const char* pcFilePathName);BOOL fnProcFile(const char* pcFilePathName, int& iFileIndex);BOOL fnSplitFilePathName(    const char* pcPathName,    std::string& strDriver,    std::string& strDir,    std::string& strFileNamePrefix,    std::string& strFileNamePostfix);LRESULT OnInitDialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);LRESULT OnClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);LRESULT OnCancel(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);LRESULT OnOk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);LRESULT OnDropFiles(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);// 做一些测试任务, 发行时,里面的内容会被关掉void fnTest_On_WinMain();int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow){    g_hInstance = hInstance;    fnTest_On_WinMain();    return DialogBox(hInstance, (LPCTSTR)IDD_MAIN_DLG, GetDesktopWindow(), (DLGPROC)MainDlgProc);}LRESULT CALLBACK MainDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    BOOL bRc = TRUE;    WORD wCtrlId = LOWORD(wParam);    WORD wNotifyCode = HIWORD(wParam);    HWND hSubWnd = (HWND)lParam;    switch (message) {        case WM_INITDIALOG: {                OnInitDialog(hWnd, message, wParam, lParam);            }            break;        case WM_COMMAND:            switch (wCtrlId) {                case IDCANCEL:                    OnCancel(hWnd, message, wParam, lParam);                    break;                case IDOK:                    OnOk(hWnd, message, wParam, lParam);                    break;                default:                    break;            }            break;        case WM_CLOSE:            OnClose(hWnd, message, wParam, lParam);            break;        case WM_DROPFILES:            OnDropFiles(hWnd, message, wParam, lParam);            break;        default:            bRc = FALSE;            break;    }    return bRc;}void fnCenterWindow(HWND hWnd){    HWND hwndOwner;    RECT rc;    RECT rcDlg;    RECT rcOwner;    hwndOwner = GetParent(hWnd);    if (NULL == hwndOwner) {        hwndOwner = GetDesktopWindow();    }    GetWindowRect(hwndOwner, &rcOwner);    GetWindowRect(hWnd, &rcDlg);    CopyRect(&rc, &rcOwner);    OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);    OffsetRect(&rc, -rc.left, -rc.top);    OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);    SetWindowPos(hWnd,                 HWND_TOP,                 rcOwner.left + (rc.right / 2),                 rcOwner.top + (rc.bottom / 2),                 0,                 0,                 SWP_NOSIZE);}void fnSetCtrlText(int iCtrlId, const TCHAR* pcTip){    HWND hWnd = NULL;    if (NULL != g_hMainWnd) {        hWnd = GetDlgItem(g_hMainWnd, iCtrlId);        fnSetWndText(hWnd, pcTip);    }}void fnSetWndText(HWND hWnd, const TCHAR* pcTip){    if (NULL != hWnd) {        SetWindowText(hWnd, (NULL != pcTip) ? pcTip : _T(""));    }}LRESULT OnInitDialog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    g_hMainWnd = hWnd;    fnSetWndText(g_hMainWnd, G_STR_PROG_NAME);    fnCenterWindow(hWnd);    return S_OK;}LRESULT OnClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    EndDialog(hWnd, LOWORD(wParam));    return S_OK;}LRESULT OnCancel(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    EndDialog(hWnd, LOWORD(wParam));    return S_OK;}LRESULT OnOk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    int iFileIndex = 0;    HWND hEdit = NULL;    char szBuf[0x100] = {'\0'};    fnSetCtrlText(IDC_STATIC_TIP, "...");    hEdit = ::GetDlgItem(g_hMainWnd, IDC_EDIT);    ::GetWindowText(hEdit, g_szSrcFile, sizeof(g_szSrcFile));    if (fnProcFile(g_szSrcFile, iFileIndex)) {        sprintf(szBuf, "成功, (%d)个文件前缀操作完成:)", iFileIndex);        fnSetCtrlText(IDC_STATIC_TIP, szBuf);    } else {        fnSetCtrlText(IDC_STATIC_TIP, "文件改名失败...");    }    return S_OK;}BOOL fnProcFile(const char* pcFilePathName, int& iFileIndex){    HANDLE hFind = INVALID_HANDLE_VALUE;    WIN32_FIND_DATA FindData;    char szOrgFilePathName[_MAX_PATH * 2] = {'\0'};    char szNewFilePathName[_MAX_PATH * 2] = {'\0'};    char szBuf[_MAX_PATH * 2] = {'\0'};    std::string strDriver;    std::string strDir;    std::string strFileNamePrefix;    std::string strFileNamePostfix;    if (NULL == pcFilePathName) {        return FALSE;    }    hFind = FindFirstFile(pcFilePathName, &FindData);    if (INVALID_HANDLE_VALUE != hFind) {        fnSplitFilePathName(            pcFilePathName,            strDriver,            strDir,            strFileNamePrefix,            strFileNamePostfix);        do {            // FindData.dwFileAttributes是位值的组合            if ((FILE_ATTRIBUTE_DIRECTORY & FindData.dwFileAttributes) > 0) {                if ((0 != strcmp(FindData.cFileName, "."))                        && (0 != strcmp(FindData.cFileName, ".."))) {                    strcpy(szNewFilePathName, strDriver.c_str()); // strDriver like "C:"                    strcat(szNewFilePathName, strDir.c_str()); // strDir like "\\dir1\\"                    strcat(szNewFilePathName, FindData.cFileName);                    strcat(szNewFilePathName, "\\*.*");                    fnProcFile(szNewFilePathName, iFileIndex);                }            } else {                strcpy(szOrgFilePathName, strDriver.c_str()); // strDriver like "C:"                strcat(szOrgFilePathName, strDir.c_str()); // strDir like "\\dir1\\"                strcpy(szNewFilePathName, szOrgFilePathName);                sprintf(szBuf, "%3.3d_%s", iFileIndex++, FindData.cFileName);                strcat(szNewFilePathName, szBuf);                strcat(szOrgFilePathName, FindData.cFileName);                ::MoveFile(szOrgFilePathName, szNewFilePathName);            }        } while (FindNextFile(hFind, &FindData));        FindClose(hFind);        hFind = NULL;    }    return TRUE;}LRESULT OnDropFiles(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    POINT pt;    UINT uFilesCnt = 0;    UINT uIndex = 0;    HDROP hDropFile = (HDROP)wParam;    DragQueryPoint(hDropFile, &pt);    uFilesCnt = DragQueryFile(hDropFile, -1, (LPSTR) NULL, 0);    for (uIndex = 0; uIndex < uFilesCnt; uIndex++) {        DragQueryFile(hDropFile, uIndex, g_szSrcFile, sizeof(g_szSrcFile));        fnSetCtrlText(IDC_EDIT, g_szSrcFile);        fnSetCtrlText(IDC_STATIC_TIP, "请点击按钮\"加前缀\"");    }    DragFinish(hDropFile);    return S_OK;}BOOL fnIsFileExist(const char* pcFilePathName){    BOOL bRc = FALSE;    HANDLE hFile = INVALID_HANDLE_VALUE;    do {        if (NULL == pcFilePathName) {            break;        }        hFile = CreateFile(pcFilePathName,  // file name                           GENERIC_READ,                   // open for reading                           0,                              // do not share                           NULL,                           // default security                           OPEN_EXISTING,                  // existing file only                           FILE_ATTRIBUTE_NORMAL,   // normal file                           NULL);                          // no template        if ((NULL == hFile) || (INVALID_HANDLE_VALUE == hFile)) {            break;        }        CloseHandle(hFile);        hFile = INVALID_HANDLE_VALUE;        bRc = TRUE;    } while (0);    return bRc;}void fnTest_On_WinMain(){}BOOL fnSplitFilePathName(    const char* pcPathName,    std::string& strDriver,    std::string& strDir,    std::string& strFileNamePrefix,    std::string& strFileNamePostfix){    char szPathName[MAX_PATH] = {'\0'};    char szDriver[MAX_PATH] = {'\0'}; /// 这个缓冲区要整大点, 要不执行结果不对.    char szDir[MAX_PATH] = {'\0'};    char szFileNamePrefix[MAX_PATH] = {'\0'};    char szFileNamePostfix[MAX_PATH] = {'\0'};    if (NULL == pcPathName) {        return FALSE;    }    strcpy(szPathName, pcPathName);    // _splitpath 的反函数_makepath    _tsplitpath(szPathName,                szDriver,                szDir,                szFileNamePrefix,                szFileNamePostfix);    strDriver = szDriver;    strDir = szDir;    strFileNamePrefix = szFileNamePrefix;    strFileNamePostfix = szFileNamePostfix;    /** note    * csPathName = c:\subDir1\subDir2\subDir3\test1.dat  * csDriver = c:    * csDir = \subDir1\subDir2\subDir3\    * csFileNamePrefix = test1    * csFileNamePostfix = .dat    */    return TRUE;}
0 0
原创粉丝点击