Highlighted

aFile.remove() not working as expected

New Here ,
Feb 25, 2016

Copy link to clipboard

Copied

Trying to find out why the code below is not deleting all files from the target folder.

After some test it appears that only graphic files (jpg, png, gif) are being removed, skipping pdf, txt, eps, doc, docx, etc.

Have you seen this happening before? Thanks,

#target framemaker

var aGraphicFiles = [];

//Check for target folder existance, if not, create.

var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

if (!checkFolderExists(myTargetFolder)){ //target folder does not exist

    myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

    myTargetFolder.create();

}

//Check if folder DeleteMe is empty

myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

aGraphicFiles = myTargetFolder.getFiles();

if (aGraphicFiles.length != 0){

    for (i=0; i<aGraphicFiles.length; i++){ //scan array

        var sourceChild = aGraphicFiles;

        if (sourceChild instanceof File){

            sourceChild.remove();

            }

        if (sourceChild instanceof Folder){

            alert("I found a folder");

        }

    }

}

function checkFolderExists(myObj){

    var flagEx = false;

    if (myObj.exists){

        flagEx = true; //alert("myTargetFolder exists");

     } else {

        flagEx = false; //alert("myTargetFolder does NOT exist");

     }

     return flagEx;

}

Hi funsekon,

here an example, that works:

Unfortunately the editor doesn't accept code at the moment, so I have to put it here this way:

Just some notes to your code:

It's easier to check, if a file exists:

if (objfile.exists).......

The reason your code doesn't work

you have to define a File Object to delete/remove it. : sourceChild = new File(aGraphicFiles)

sourceChild.remove()

Your Line 3 :

var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

  should be

var myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe"); 

BTW: BE CARFUL WITH THIS SCRIPT ---  IT WILL ULTIMATELY DELETE ALL FILES !!!!!!!!!!!!!

var oFolder = new Folder("C:\\test\\deleteme");

var oStartFolder = oFolder.selectDlg ("Select Folder");

 

if (oStartFolder != null)
{
DeleteFilesInFolder(oStartFolder);
}

function DeleteFilesInFolder(foFolderName)

{

var locError = false;

 

var DeleteFolder = new Folder(foFolderName)

 

var foFolderContent = DeleteFolder.getFiles ("");

for (var i = 0; i < foFolderContent.length; i++)
{
if (foFolderContent  instanceof File)
{
var FileToDelete = new File (foFolderContent);
FileToDelete.remove();
if (FileToDelete.error > "")
{
locError = true;
alert("Error deleting File: ") + FileToDelete.fsName;
}
}
}

}

TOPICS
Scripting

Views

577

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

aFile.remove() not working as expected

New Here ,
Feb 25, 2016

Copy link to clipboard

Copied

Trying to find out why the code below is not deleting all files from the target folder.

After some test it appears that only graphic files (jpg, png, gif) are being removed, skipping pdf, txt, eps, doc, docx, etc.

Have you seen this happening before? Thanks,

#target framemaker

var aGraphicFiles = [];

//Check for target folder existance, if not, create.

var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

if (!checkFolderExists(myTargetFolder)){ //target folder does not exist

    myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

    myTargetFolder.create();

}

//Check if folder DeleteMe is empty

myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

aGraphicFiles = myTargetFolder.getFiles();

if (aGraphicFiles.length != 0){

    for (i=0; i<aGraphicFiles.length; i++){ //scan array

        var sourceChild = aGraphicFiles;

        if (sourceChild instanceof File){

            sourceChild.remove();

            }

        if (sourceChild instanceof Folder){

            alert("I found a folder");

        }

    }

}

function checkFolderExists(myObj){

    var flagEx = false;

    if (myObj.exists){

        flagEx = true; //alert("myTargetFolder exists");

     } else {

        flagEx = false; //alert("myTargetFolder does NOT exist");

     }

     return flagEx;

}

Hi funsekon,

here an example, that works:

Unfortunately the editor doesn't accept code at the moment, so I have to put it here this way:

Just some notes to your code:

It's easier to check, if a file exists:

if (objfile.exists).......

The reason your code doesn't work

you have to define a File Object to delete/remove it. : sourceChild = new File(aGraphicFiles)

sourceChild.remove()

Your Line 3 :

var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

  should be

var myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe"); 

BTW: BE CARFUL WITH THIS SCRIPT ---  IT WILL ULTIMATELY DELETE ALL FILES !!!!!!!!!!!!!

var oFolder = new Folder("C:\\test\\deleteme");

var oStartFolder = oFolder.selectDlg ("Select Folder");

 

if (oStartFolder != null)
{
DeleteFilesInFolder(oStartFolder);
}

function DeleteFilesInFolder(foFolderName)

{

var locError = false;

 

var DeleteFolder = new Folder(foFolderName)

 

var foFolderContent = DeleteFolder.getFiles ("");

for (var i = 0; i < foFolderContent.length; i++)
{
if (foFolderContent  instanceof File)
{
var FileToDelete = new File (foFolderContent);
FileToDelete.remove();
if (FileToDelete.error > "")
{
locError = true;
alert("Error deleting File: ") + FileToDelete.fsName;
}
}
}

}

TOPICS
Scripting

Views

578

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 25, 2016 0
Enthusiast ,
Feb 25, 2016

Copy link to clipboard

Copied

Hi funsekon,

here an example, that works:

Unfortunately the editor doesn't accept code at the moment, so I have to put it here this way:

Just some notes to your code:

It's easier to check, if a file exists:

if (objfile.exists).......

The reason your code doesn't work

you have to define a File Object to delete/remove it. : sourceChild = new File(aGraphicFiles)

sourceChild.remove()

Your Line 3 :

var myTargetFolder = new File("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe");

  should be

var myTargetFolder = new Folder("C:\\Basket2\\XML Tests\\BookFolder\\Resources\\DeleteMe"); 

BTW: BE CARFUL WITH THIS SCRIPT ---  IT WILL ULTIMATELY DELETE ALL FILES !!!!!!!!!!!!!

var oFolder = new Folder("C:\\test\\deleteme");

var oStartFolder = oFolder.selectDlg ("Select Folder");

 

if (oStartFolder != null)
{
DeleteFilesInFolder(oStartFolder);
}

function DeleteFilesInFolder(foFolderName)

{

var locError = false;

 

var DeleteFolder = new Folder(foFolderName)

 

var foFolderContent = DeleteFolder.getFiles ("");

for (var i = 0; i < foFolderContent.length; i++)
{
if (foFolderContent  instanceof File)
{
var FileToDelete = new File (foFolderContent);
FileToDelete.remove();
if (FileToDelete.error > "")
{
locError = true;
alert("Error deleting File: ") + FileToDelete.fsName;
}
}
}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 25, 2016 0
funsekon LATEST
New Here ,
Mar 01, 2016

Copy link to clipboard

Copied

Thanks Klaus. Was not aware of the fact that a new object was required in order to remove the actual files. Your script worked perfectly (thanks for the warning about deleting all files).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 01, 2016 0