Javascript FileSystemObject (FSO) provides the ParentFolder property that can be accessed through the file object or folder object returned by the GetFile or GetFolder method. ParentFolder property returns the folder object associated with the specified file or folder in which it is located. Folder object returned by the ParentFolder property can further access the method and properties associated with folder object e.g: DateCreated, DataLastModified, Move etc.
Syntax for Javascript FSO ParentFolder property
fileObj.ParentFolder // or folderObj.ParentFolder
Both above syntaxes show the way of using the file object and folder object to access the ParentFolder property. GetFile method returns the file object and GetFolder method returns the folder object that can be used to access the ParentFolder property associated with the specified file or folder. In simple words you can say that ParentFolder property returns the path of the folder in which the specified file or folder resides. This property also works as folder object.
Example 1
File object to get the value of ParentFolder property
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// object created for file object returned by GetFile method
var fileObj = fso.GetFile("C:\\Temp\\textFile.txt");
document.write("<b>File Name: </b>" + fileObj.Name);
document.write("<br />");
// Output:
// ParentFolder : C:\Temp
</script>
Example 2
Folder object to get the value of ParentFolder property
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// object created for folder object returned by GetFolder method
var folderObj = fso.GetFolder("C:\\Temp\\myFolder");
document.write("<b>Folder Name: </b>" + folderObj.Name);
document.write("<br />");
// Output:
// ParentFolder : C:\Temp
</script>
Continue to next tutorial: Javascript FSO Path Property to learn how to get the full path of the specified file or folder.