18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
Folder and Files
Hai,
How to get the file name from a directory?
If a folder contain 5 files and I need to list the file name one by one..?
Please replay asap..
How to get the file name from a directory?
If a folder contain 5 files and I need to list the file name one by one..?
Please replay asap..
Best Answers
-
Hi,
you can access directorys this way:
At first, create an file object.
var $dir = new java.io.File($path);
Then you can use ".listFiles()" to create an array which contains
all files of the folder.
$fileArray = $dir.listFiles();
With ".isDirectory()" you can sort out the folders.
For example:
if($fileArray[$i].isDirectory == true )
{
//do something
} -
Hi,
you can access directorys this way:
At first, create an file object.
var $dir = new java.io.File($path);
Then you can use ".listFiles()" to create an array which contains
all files of the folder.
$fileArray = $dir.listFiles();
With ".isDirectory()" you can sort out the folders.
For example:
if($fileArray[$i].isDirectory == true )
{
//do something
}
Answers
I tried the below way..
The folder "userdata" contain 4 files.
var $dir = new java.io.File("C:/Users/vinil.p/sahinew/userdata");
$fileArray = $dir.listFiles();
for(var $i=0;$i<5;$i++)
{
_log($fileArray[$i]);
}
and the out put is...
_log(undefined);
_log(undefined);
_log(undefined);
_log(undefined);
_log(null);
--Stopped Playback: SUCCESS--
i forgot to mention that the array contains objects.
So, to get the names you can use "getName()"
--> _log($fileArray[$i].getName());
For other functions you can read the documentation of java file:
http://docs.oracle.com/javase/7/docs/api/java/io/File.html
Thanks a lot...
var $dir = new java.io.File("C:/Users/admin/sahinew/userdata/scripts");
var $fileArray = $dir.listFiles();
try
{
for(var $i=0;$i<10;$i++)
{
// 1st way
var $fp = $fileArray[$i].toString();
_log($fp);
var $fn=$fp.match(/[-_.\w]+[.][\w]+$/i)[0];
_log($fn);
//1st way end
//another way
_log($fileArray[$i].getName());
}
}
catch(e)
{
_log("completed");
}