• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

aFile.remove() not working as expected

New Here ,
Feb 25, 2016 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;

}

TOPICS
Scripting

Views

1.4K

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
community guidelines

correct answers 1 Correct answer

Enthusiast , Feb 25, 2016 Feb 25, 2016

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 myTarget

...

Votes

Translate

Translate
Enthusiast ,
Feb 25, 2016 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;
}
}
}

}

Votes

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
community guidelines
New Here ,
Mar 01, 2016 Mar 01, 2016

Copy link to clipboard

Copied

LATEST

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).

Votes

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
community guidelines