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

Script AE Remove all temporary Files

Community Beginner ,
Mar 03, 2020 Mar 03, 2020

Hello everybody , i'm trying to make a script to remove all the files inside 4 differents folders.

this files are generating errors when i work with .swf files...

So i made this script :

It works whith extendscript manager ,all the files in all folders are removed but when i copy my script in the "script ui "file and i launch it inside Ae is not working anymore...

If somebody can help me whit this issue 

Thank you for you help

Tom

//====================UI=======================================  
//=============================================================
function myScript(thisObj){
function myScript_buildUI(thisObj){
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "ta_Cache&Memory", [300, 300, 300, 300], {resizeable:true});

res = "group{orientation:'column', alignment:['left', 'top'], alignChildren:['center', 'center'],\
      myPanel01: Panel{text:'SessionWin10', orientation:'row', alignChildren:['center', 'center'],\
                            Button01: EditText{text:'', preferredSize:[80,25]},\
                            },\
                     myPanel02: Panel{text:'Cleaners', orientation:'column', alignChildren:['center', 'center'],\
                            Button04: Button{text:'CleanTemp',preferredSize:[80,25]},\
                            },\
},\
}";           
        //Add resource string to panel
myPanel.grp = myPanel.add(res);
      
      //Add txt File With session name
var scriptFolderPath = File($.fileName).path; // the URI of the folder that contains the script file    
var SessionFile=File(scriptFolderPath + encodeURI("/SessionName.txt"));


if(!SessionFile.exists){
     var JFile = new File(scriptFolderPath + encodeURI("/SessionName.txt"));
     var content = "admin";
     writeFile(JFile, content);    
}
//ReadMyFile.txt
SessionFile.open("r");
var MyTxtText = SessionFile.read();

//ChangeButton01 text content
myPanel.grp.myPanel01.Button01.text=MyTxtText;
//====================CLICK====================================
//=============================================================
myPanel.grp.myPanel02.Button04.onClick = function(){

var MyNewText=myPanel.grp.myPanel01.Button01.text   //check the new text
var JFile = new File(SessionFile);  
var content = MyNewText;  
writeFile(JFile, content);


var Folder1="C://Users/"+MyNewText+"/AppData/Roaming/Adobe/Common/Media Cache Files/"
var Folder2="C://Users/"+MyNewText+"/AppData/Roaming/Adobe/Common/Media Cache/"
var Folder3="C://Users/"+MyNewText+"/AppData/Roaming/Adobe/Common/Peak Files/"
var Folder4="C://Users/"+MyNewText+"/AppData/Local/Temp/"

var fldr1 = Folder (Folder1)
var fldr2 = Folder (Folder2);
var fldr3 = Folder (Folder3);
var fldr4 = Folder (Folder4);
   
var files1 = fldr1.getFiles();
    var files2 = fldr2.getFiles();
        var files3 = fldr3.getFiles();          
            var files4 = fldr4.getFiles();
for each (var file1 in files1){
file1.remove();
        for each (var file2 in files2){
file2.remove();
             for each (var file3 in files3){
     file3.remove();
                  for each (var file4 in files4){
           file4.remove();
       }}}}
alert("CleanTemporaryFilesDone!!")
}
//====================SHOW==UI================================
//=============================================================
//Setup panel sizing and make panel resizable
myPanel.layout.layout(true);
return myPanel;
}
var myScriptPal = myScript_buildUI(thisObj);
if (myScriptPal != null && myScriptPal instanceof Window){
myScriptPal.center();
myScriptPal.show();
}
}
myScript(this);      
//====================FUNCTIONS===============================
//=============================================================
function writeFile(fileObj, fileContent, encoding) {
    encoding = encoding || "utf-8";
    fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);
    var parentFolder = fileObj.parent;
    if (!parentFolder.exists && !parentFolder.create())
        throw new Error("Cannot create file in path " + fileObj.fsName);
    fileObj.encoding = encoding;
    fileObj.open("w");
    fileObj.write(fileContent);
    fileObj.close();
    return fileObj;
}

TOPICS
Scripting
1.9K
Translate
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
Community Expert ,
Mar 04, 2020 Mar 04, 2020

What's not working exactly? Are you getting an error?

Translate
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
Community Beginner ,
Mar 05, 2020 Mar 05, 2020

In AE (UI) the script clean only the first folder then he stops . But when i launch the script with extendscript manager he clean all the four folders.

I have an error message : the function file3.remove is not defined....??ErrorFunction.png

Translate
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
Engaged ,
Mar 05, 2020 Mar 05, 2020

Possible issues below, also admin access to delete the files would need to be checked. Also writeFile function is out of scope when launched as a UI in After Effects, so include into the UI script.

 

 

 

2020-03-04 17_24_24-_ egLaunch.jsx.png

Translate
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
Community Beginner ,
Mar 05, 2020 Mar 05, 2020

i rewrite the script to call a function...... but i have the same probleme it removes all the files in extendsciptManager but in Ae he remove only the first folder and then he stoped with this error:       file.remove() is not defined....??? 

 


//====================UI=======================================
function myScript(thisObj){
function myScript_buildUI(thisObj){
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "ta_Cache&Memory", [300, 300, 300, 300], {resizeable:true});

res = "group{orientation:'column', alignment:['left', 'top'], alignChildren:['center', 'center'],\
myPanel02: Panel{text:'Cleaner', orientation:'column', alignChildren:['center', 'center'],\
Button04: Button{text:'CleanTemp',preferredSize:[80,25]},\
},\
},\
}";

//Add resource string to panel
myPanel.grp = myPanel.add(res);

//====================CLICK====================================

myPanel.grp.myPanel02.Button04.onClick = function(){

var Folder1="C://Users/toon/Desktop/TestRemoveFiles/001/"
var Folder2="C://Users/toon/Desktop/TestRemoveFiles/002/"
var Folder3="C://Users/toon/Desktop/TestRemoveFiles/003/"

RemoveAllInsideFolder (Folder1)

RemoveAllInsideFolder (Folder2)

RemoveAllInsideFolder (Folder3)

alert("CleanTemporaryFilesDone!!")

}

//====================SHOW==UI================================
//Setup panel sizing and make panel resizable
myPanel.layout.layout(true);
return myPanel;
}
var myScriptPal = myScript_buildUI(thisObj);
if (myScriptPal != null && myScriptPal instanceof Window){
myScriptPal.center();
myScriptPal.show();
}
}
myScript(this);

//====================FUNCTIONS===============================

function RemoveAllInsideFolder(MyFolder){
var fldr = Folder (MyFolder);
if(fldr==null) return;

var files = fldr.getFiles();
if (files.length == 0) return;

for each (var file in files){
file.remove();
}}


function writeFile(fileObj, fileContent, encoding) {

encoding = encoding || "utf-8";
fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);
var parentFolder = fileObj.parent;
if (!parentFolder.exists && !parentFolder.create())
throw new Error("Cannot create file in path " + fileObj.fsName);
fileObj.encoding = encoding;
fileObj.open("w");
fileObj.write(fileContent);
fileObj.close();

return fileObj;

}

Translate
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
Community Expert ,
Mar 05, 2020 Mar 05, 2020

Firstly, you don't need nested for loops for non-related arrays. 

Secondly, check permissions, try running AE as Administrator if you're on PC or run with sudo if you're on Mac and see if that changes anything. 

 

Also, you may wanna throw some checks in there to see if the file/folder exists before removing.

Translate
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
Mentor ,
Mar 05, 2020 Mar 05, 2020
LATEST

It's good practice to end each line/logical block with a semicolon, also even function.

All semicolons are missing in the block //===CLICK.

ExtendedScript might be more generous to this as AE itself.

 

Also, I would limit the onClick-function as much as possible - literally just let it call your main function.

Main function should only declare the foldernames as array (1), call your remove-function in a for-loop (2) and print the alert (3) at the end.

 

Beside this, the code looks good. As others said, make sure permissions are set and test-run AE in adminmode.

 

If this is not solving the issue, I would next add alerts to some critical spots to get a feedback at runtime:

- Checking first if the code runs through completly by replacing file.remove() with alert(file) [or file.name].

- Alerting the filenames could be insightful and help to understand, what is going on. Maybe you ran against hidden or protected files (thumb.db, for example).

- Checking if folder names are parsed correctly and that the function is called 3 times as you have written it.

- As soon as the final alert ("CleanTemporaryFilesDone!!") appears, take a closer look to remove() and replace it with some other similar functions, like copying or renaming and see what happens.

 

As last resort, make a systemcall to run a batch or Python script which does the job. You'll find snippets or final scripts for both, for sure.

 

*Martin

Translate
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