自删除 @echo off start if exist XX del XX goto start if not exist XX goto end end del %0 #include <windows.h> #include <shlobj.h> #include <fstream.h> #pragma comment( linker, "/subsystem:/"windows/" /entry:/"mainCRTStartup/"" ) char *pFileName(char *szFilePathName); int main() { HMODULE hCur = GetModuleHandle(NULL); char szFilePathName[MAX_PATH] = ""; if (0 == GetModuleFileName(hCur, szFilePathName, MAX_PATH)) { return -1; } fstream fw("./AutoDel.bat", ios::out); if (!fw) { return -1; } // char szDesktopPath[MAX_PATH] = ""; // if (TRUE == SHGetSpecialFolderPath(0, szDesktopPath, CSIDL_DESKTOPDIRECTORY, // FALSE)) // { // strcat(szDesktopPath, "//"); // strcat(szDesktopPath, pFileName(szFilePathName)); // } // if (TRUE == MoveFileEx(szFilePathName, szDesktopPath, // MOVEFILE_REPLACE_EXISTING)) // { fw << "@echo off" << '/n'; fw << ":start" << '/n'; fw << "/tif exist " << pFileName(szFilePathName) << " del /f/q " << pFileName(szFilePathName) << " goto start/n"; fw << "/tif not exist " << pFileName(szFilePathName) << " goto end/n"; fw << ":end/n"; fw << "/tdel /f/q %0"; fw.close(); // } // else // { // fw.close(); // return -1; // } ShellExecute(NULL, "open", "./AutoDel.bat", NULL, NULL, SW_HIDE); exit(0); return 0; } char *pFileName(char *szFilePathName) { if (szFilePathName == NULL) { return NULL; } for (int i=strlen(szFilePathName); i>=0; i--) { if (szFilePathName[i] == '//' || szFilePathName[i] == '/') { return szFilePathName + i + 1; } } return NULL; }