/**
	Description: This lib works with filesystem.
	Link:
		http://ns7.webmasters.com/caspdoc/html/jscript_objects.htm
		http://msdn.microsoft.com/en-us/library/6tkce7xa(v=VS.85).aspx
	Author: Drachenko Viktor
*/

/**
	Returns the array of files objects.
	File object properties:
		Attributes, DateCreated, DateLastAccessed, DateLastModified, Drive, Name, ParentFolder, Path, ShortName, ShortPath, Size, Type

*/
function fsReadFolder(folderName,recursively) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		if ( ! fs.FolderExists(folderName) ) return null;
		var fsFiles = new Enumerator(fs.GetFolder(folderName).Files);
		var files = [];
		for(var i=0; !fsFiles.atEnd(); fsFiles.moveNext()) {
			var file = fsFiles.item();
			files.push(file);
		}
		if ( recursively !== false ) {
			var fsFolders = new Enumerator(fs.GetFolder(folderName).SubFolders);
			for(var i=0; !fsFolders.atEnd(); fsFolders.moveNext()) {
				var filesAdd = fsReadFolder(fsFolders.item());
				files = files.concat(filesAdd);
			}
		}
		return files;
	} catch (e) {
		return null;
	}
}
/**
	Deletes folder or file
*/
function fsDelete(path) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		if ( fs.FolderExists(path) )
			fs.DeleteFolder(path,true);
		else if ( fs.FileExists(path) )
			fs.DeleteFile(path,true);
	} catch(e) {
		return false;
	};
}
/**
	Copys folder to another folder
*/
function fsCopyFolder(fromFolderName,toFolderName) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		if ( ! fs.FolderExists(fromFolderName) ) return false;
		fromFolderName = fs.GetFolder(fromFolderName).Path;
		fs.CopyFolder(fromFolderName,toFolderName,true);
		return true;
	} catch(e) {
		return false;
	}
}
/**
	Copys a file
*/
function fsCopyFile(src,dest) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		if ( ! fs.FileExists(src) )
			return false;
		if ( fs.FileExists(dest) )
			fs.DeleteFile(dest,true);
		fs.CopyFile(src,dest,true);
		return true;
	} catch(e) {
		return false;
	}
}
/**
	Checks file exists
*/
function fsFileExists(file) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		return fs.FileExists(file);
	} catch(e) {
		return false;
	}
}
/**
	Recursively moves files from folder to another folder.
*/
function fsMergeFolders(fromFolderName,toFolderName) {
	try {
		var fs = new ActiveXObject("Scripting.FileSystemObject");
		if ( ! fs.FolderExists(fromFolderName) ) return false;
		fromFolderName = fs.GetFolder(fromFolderName).Path;
		if ( ! fs.FolderExists(toFolderName) ) {
			fs.CopyFolder(fromFolderName,toFolderName,true);
			fs.DeleteFolder(fromFolderName,true);
			return true;
		} else {
			toFolderName = fs.GetFolder(toFolderName).Path;
		}
		var files = fsReadFolder(fromFolderName);
		for(var i in files) {
			try {
				var file = files[i];
				var fileDir = file.Path.substring(fromFolderName.length+1);
				if ( fileDir.indexOf('\\') !== -1 ) {
					fileDir = fileDir.substring(0,fileDir.lastIndexOf('\\'));
					if ( ! fs.FolderExists(toFolderName+'\\'+fileDir) )
						fs.CreateFolder(toFolderName+'\\'+fileDir);
				} else {
					fileDir = '';
				}
				if ( fs.FileExists(toFolderName+'\\'+fileDir+'\\'+file.Name) )
					fs.DeleteFile(toFolderName+'\\'+fileDir+'\\'+file.Name,true);
				fs.CopyFile(file.Path,toFolderName+'\\'+fileDir+'\\'+file.Name,true);
				//fsDelete(file.Path);
			} catch(e) {
				
			};
		}
		return true;
	} catch(e) {
		return false;
	}
}
