VC中用命令行创建文件夹的方法

来源:互联网 发布:淘宝晒图怎么删除 编辑:程序博客网 时间:2024/05/01 21:24

用命令行创建

在VC中嵌套dos指令的方法来实现,调用system函数

可以使用system("mkdir \"foldername\"");

或者使用system("md \"foldername\"");

或者干脆写一段命令行的小代码,然后在这里调用,使用此种方法就得注意了,程序在执行时会弹出一个黑白窗口,一闪而过。这种代码实现非常灵

#include "stdafx.h"#include "afx.h"  //在共享 DLL 中使用 MFC#include#include#include#include// 设置入口地址,在此实现隐藏控制台#pragma comment( linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") int main(int argc, _TCHAR* argv[])   //此处使用main(){  CString szPath="d:\\2012\\12\\18";CString strDir(szPath);//存放要创建的目录字符串    //确保以'\'结尾以创建最后一个目录    if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))    {        strDir.AppendChar(_T('\\'));    }    std::vector vPath;//存放每一层目录字符串    CString strTemp;//一个临时变量,存放目录字符串    bool bSuccess = false;//成功标志    //遍历要创建的字符串    for (int i=0;i    {        if (strDir.GetAt(i) != _T('\\'))         {//如果当前字符不是'\\'            strTemp.AppendChar(strDir.GetAt(i));        }        else         {//如果当前字符是'\\'            vPath.push_back(strTemp);//将当前层的字符串添加到数组中            strTemp.AppendChar(_T('\\'));        }    }    //遍历存放目录的数组,创建每层目录    std::vector::const_iterator vIter;    for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)     {        //如果CreateDirectory执行成功,返回true,否则返回false        bSuccess = CreateDirectory(*vIter, NULL) ? true : false;        }if(bSuccess==true){::MessageBox(NULL,_T("成功创建多级文件夹!"),_T("创建多级文件夹"),MB_OK);}else{::MessageBox(NULL,_T("所需多级文件夹已存在!"),_T("创建多级文件夹"),MB_OK);}return 0;}

在实际应用中,我们有时候需要创建多级文件夹,来进行保存数据,但是CreateDirectory()只能创建单级文件夹,故在此提供一种创建多级文件夹的方法:(以创建D:\2012\12\18为例进行说明


0 0