如果文件夾不存在則創建
應該是文件夾不存在就創建的吧。
File headPath = new File(uploadPath);//獲取文件夾路徑模拆
if(!headPath.exists()){//判斷文團碼棗件夾是否創建,塌拆沒有創建則創建新文件夾
headPath.mkdirs();
}
2. php判斷文件夾是否存在不存在則創建
php判斷文件夾是否存在用到的工具:notepad++,代碼如下:
functionmkdirs($a1,$mode=0777)
{
if(is_dir($a1)||@mkdir($al,$mode))returnTRUE;
if(!mkdirs(dirname($a1),$mode))returnFALSE;
return@mkdir($a1,$mode);
}
mkdirs("a1");
說明:程序判斷a1這個目錄是否存在,如果存在就返回真,如果不存在就創建a1目錄,默認給與讀寫和執行的許可權。
注意事項:777許可權適用於linux環境。
3. C#判斷文件和文件夾是否存在 不存在則創建
if (Directory.Exists(Server.MapPath(~/upimg/hufu)) == false)//如果不存在就創建file文件夾{ Directory.CreateDirectory(Server.MapPath(~/upimg/hufu));} //Directory.Delete(Server.MapPath(~/upimg/hufu), true);//刪除文件夾以及文件夾中的子目錄,文件 //判斷文件的存在 if (File.Exists(Server.MapPath(~/upimg/Data.html))){ Response.Write(Yes);//存在旅銀文件}else{ Response.Write(No); //不存在文件 File.Create(MapPath(~/upimg/Data.html));//創建該文件} string name = GetFiles.FileName;//獲取已上傳明卜文件的名字 string size = GetFiles.PostedFile.ContentLength.ToString();//獲取已上傳文件的大小 string type = GetFiles.PostedFile.ContentType;//獲取已上傳文件的MIME string postfix = name.Substring(name.LastIndexOf(.) + 1);//獲取已上傳文件的後綴 string ipath = Server.MapPath(upimg) +\\+ name;//獲取文件的實際路徑 string fpath = Server.MapPath(upfile) + \\ + name; string dpath = upimg\\ + name;//判斷寫入資料庫的虛擬路徑 ShowPic.Visible = true;//激活 ShowText.Visible = true;//激活 //判斷文件格式 if (name == ) { Response.Write(<scriptalert('上傳文件不能為空')</script);}else{ if (postfix == jpg || postfix == gif || postfix == bmp || postfix == png){ GetFiles.SaveAs(ipath); ShowPic.ImageUrl = dpath; ShowText.Text = 你上傳的圖片名稱是: + name + + 文件大小: + size + KB + + 文件類型: + type + + 存放的實際路徑為: + ipath;}else{ ShowPic.Visible = false;//隱藏圖片 GetFiles.SaveAs(fpath);//由於不是圖片文件,因此轉存在upfile這個文件夾拆槐宴 ShowText.Text = 你上傳的文件名稱是: + name + + 文件大小: + size + KB + + 文件類型: + type + + 存放的實際路徑為: + fpath;}
4. php判斷文件夾是否存在不存在則創建
//直接這樣即可:
$dir='./test/test';
is_dir($dir)ORmkdir($dir,0777,true);//如果文件夾不存在,將以遞歸方式創建該文件夾
5. 批處理 判斷一個文件夾是否存在,不存在則創建,求大神指導。。謝謝
用IF NOT EXIST "G:\%DATE:~0,10%tst" MD "G:\%DATE:~0,10%tst"
例如:
IF EXIST C:DATE (
del filename.
) ELSE (
echo filename. missing.
)
(5)如果文件夾不存在則創建擴展閱讀:
注意事項
@echo off
@title 批處理判斷文件夾是否存在
if exist folder1 (
echo "已銷告笑經存在文件夾"
) else (
md folder1
)
if not exist folder2 md folder2
pause
命令中首先判斷當前目錄友岩中是否存在folder1,如虧含果存在,列印「已經存在文件夾」如果不存在就用md命令建立文件夾。
6. php判斷文件夾是否存在不存在則創建
if(file_exists($file)) 存在;
else 不存在;
if(is_dir($dir)) 存在;
else 不存在;
mkdir($dir); //創建文件夾
file_put_contents('文件路徑', '文件內容');//創建文件
7. 在C++中如何判斷文件夾是否存在,不存在的話創建文件夾
參考代碼如下:
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
#include <memory>
//檢查文件夾是否存在,不存在則創建之
//文件夾存在返回 0
//文件夾創建失敗返回-1
//文件夾創建失敗返回1
int CheckDir(char* Dir)
{
FILE *fp = NULL;
char TempDir[200];
memset(TempDir,'\0',sizeof(TempDir));
sprintf(TempDir,Dir);
strcat(TempDir,"\\");
strcat(TempDir,".temp.fortest");
fp = fopen(TempDir,"w");
if (!fp)
{
if(_mkdir(Dir)==0)
{
return 1;//文件夾創建成功
}
else
{
return -1;//can not make a dir;
}
}
else
{
fclose(fp);
}
return 0;
}
8. c++ 判斷文件夾是否存在,不存在則創建
c++中,<io.h>中的_access可以判斷文件是否存在,<direct.h>中的_mkdir可以創建文件。
---------------------------------------------
建單級目錄:
#include <io.h>
#include <direct.h>
#include <string>
int main()
{
std::string prefix = "G:/test/";
if (_access(prefix.c_str(), 0) == -1) //如果文件夾不存在
_mkdir(prefix.c_str()); //岩鬧尺則創建
}
----------------------------------------------------
建多級目錄:
最後一個如果是文件粗高夾的話,需要加上 '\\' 或者 '/'
#include <io.h>
#include <direct.h>
#include <string>
int createDirectory(std::string path)
{
int len = path.length();
char tmpDirPath[256] = { 0 };
for (int i = 0; i < len; i++)
{
tmpDirPath[i] = path[i];
if (tmpDirPath[i] == '\\'彎蘆 || tmpDirPath[i] == '/')
{
if (_access(tmpDirPath, 0) == -1)
{
int ret = _mkdir(tmpDirPath);
if (ret == -1) return ret;
}
}
}
return 0;
}
9. Java判斷文件夾是否存在,不存在就創建
方法如下:
public static void judeDirExists(File file)
if (file.exists()) if (file.isDirectory())
System.out.println("dir exists"); }
else System.out.println("the same name file exists, can not create dir"); }41
else System.out.println("dir not exists, create it ..."); 、
file.mkdir();