Create Complete Directory Path
Audience Level
Beginner and above.
Introduction
A JScript function that takes a filepath input string and creates all of the directories in the path. Directories that already exist in the path are skipped over.
Source Code
/*
Function: doCreateDirectoryPath()
Description: Build a hierarchy of directories up to the last directory in a path. Ignores folders that already exist.
Returns:
History:
20040815 1920BST v1 Andrew Urquhart Created
*/
function doCreateDirectoryPath(strFilepath) {
try {
if (!strFilepath) {
throw new Error(1, "Required parameter \"strFilepath\" was not defined");
}
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
if (!objFSO) {
throw new Error(2, "Failed to create \"Scripting.FileSystemObject\" object");
}
var dirs = strFilepath.replace(/\//g, "\\").replace(/(\w:)\\?/, "").split("\\");
var dirPathStack = RegExp.$1 + "\\";
for (var d=0; d<dirs.length - 1; d++) {
if (dirs[d]) {
dirPathStack += dirs[d] + "\\";
try {
if (!objFSO.FolderExists(dirPathStack)) {
// Folder doesn't exist, so create it
try {
objFSO.CreateFolder(dirPathStack);
}
catch (err) {
throw new Error(err.number, "CreateFolder() failed with argument \"" + dirPathStack + "\". Message: " + err.description);
}
}
else {
// Folder already exists, skip it
continue;
}
}
catch (err) {
// Ignore 'permission denied' and 'file exists' errors
if (err.number == -2146828218 || err.number == -2146828230) {
continue; // Try to build the path all the way to the last directory - we may be being denied only because we're outsite the servable root and we may eventually pop back into it
}
else {
throw err;
}
}
}
}
// Return a Boolean as to whether directory path exists
return objFSO.FolderExists(dirPathStack);
}
catch (err) {
throw new Error(err.number, "Function doCreateDirectoryPath() failed with parameters strFilepath=\"" + strFilepath + "\". Message=\r\n" + err.description);
}
}
Function: doCreateDirectoryPath()
Description: Build a hierarchy of directories up to the last directory in a path. Ignores folders that already exist.
Returns:
History:
20040815 1920BST v1 Andrew Urquhart Created
*/
function doCreateDirectoryPath(strFilepath) {
try {
if (!strFilepath) {
throw new Error(1, "Required parameter \"strFilepath\" was not defined");
}
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
if (!objFSO) {
throw new Error(2, "Failed to create \"Scripting.FileSystemObject\" object");
}
var dirs = strFilepath.replace(/\//g, "\\").replace(/(\w:)\\?/, "").split("\\");
var dirPathStack = RegExp.$1 + "\\";
for (var d=0; d<dirs.length - 1; d++) {
if (dirs[d]) {
dirPathStack += dirs[d] + "\\";
try {
if (!objFSO.FolderExists(dirPathStack)) {
// Folder doesn't exist, so create it
try {
objFSO.CreateFolder(dirPathStack);
}
catch (err) {
throw new Error(err.number, "CreateFolder() failed with argument \"" + dirPathStack + "\". Message: " + err.description);
}
}
else {
// Folder already exists, skip it
continue;
}
}
catch (err) {
// Ignore 'permission denied' and 'file exists' errors
if (err.number == -2146828218 || err.number == -2146828230) {
continue; // Try to build the path all the way to the last directory - we may be being denied only because we're outsite the servable root and we may eventually pop back into it
}
else {
throw err;
}
}
}
}
// Return a Boolean as to whether directory path exists
return objFSO.FolderExists(dirPathStack);
}
catch (err) {
throw new Error(err.number, "Function doCreateDirectoryPath() failed with parameters strFilepath=\"" + strFilepath + "\". Message=\r\n" + err.description);
}
}
Download
Example Usage
Here I'm using the doSaveFile()
function described elsewhere to save a file, but beforehand I want to ensure use that the directories
in the “strFilepath” exist:
var strFilePath = "E:\\websites\\username\\www\\css\\main.css";
var strContent = "/* main.css */";
// Either function may throw an exception, so ensure we can catch any
// ==================================================================
try {
// Ensure directories exist before attempting to save a file
// =========================================================
doCreateDirectoryPath(strFilepath);
// Save the file - this is a function described in another page
// ============================================================
doSaveFile(strFilePath, "UTF-8", strContent, true);
}
catch (err) {
Response.Write("The file could not be saved to path \"" + Server.HTMLEncode(strFilePath) + "\" because error \"" + err.number + "\" occurred with message: " + err.description);
}
var strContent = "/* main.css */";
// Either function may throw an exception, so ensure we can catch any
// ==================================================================
try {
// Ensure directories exist before attempting to save a file
// =========================================================
doCreateDirectoryPath(strFilepath);
// Save the file - this is a function described in another page
// ============================================================
doSaveFile(strFilePath, "UTF-8", strContent, true);
}
catch (err) {
Response.Write("The file could not be saved to path \"" + Server.HTMLEncode(strFilePath) + "\" because error \"" + err.number + "\" occurred with message: " + err.description);
}