Deleting folder "Temporary Internet Files"

Discussion in 'Javascript' started by Rosa, Apr 6, 2005.

  1. Rosa

    Rosa Guest

    Hi,
    I'm trying to clear the TIF on Windows XP programmatically with the
    below code.
    This code works fine on any folder but the TIF. For some reason the
    atEnd() statements always defaults to true and no files are deleted in
    the folder.
    The peculiarity of this issue is that the files/subfolders cannot be
    seen through the windows explorer either. I can only access/delete
    them through a command shell.
    Any ideas?
    Many thanks
    Rosa
    CODE:
    #############################################################################
    // This is the root, which we analyse
    var sRoot="C:\\Documents and Settings";
    var sLocalSettings="\\Local Settings\\"

    // Declaration of other variables
    var fso, f, fc, fcf, logfile,TIFFolder, s;
    s= "";

    // Create the FileSystemObject
    fso = new ActiveXObject("Scripting.FileSystemObject");

    // Navigate to our root
    f = fso.GetFolder(sRoot);

    // Log-file
    logfile = fso.CreateTextFile("c:\\del.log", 1);
    logfile.WriteLine("Logging for the script to delete Temporary Internet
    Files Folder");
    logfile.WriteLine("================================================================");

    // Enumerate all Subfolders to fc, by using the SubFolders-method of
    FSO
    fc = new Enumerator(f.SubFolders);
    for (; !fc.atEnd(); fc.moveNext())
    {
    // store path to the Temporary Internet Files in TIFFolder
    TIFFolder = fc.item() + sLocalSettings + "Temporary Internet
    Files";

    // dont even try to delete in the following Profiles
    if ( (fc.item() == (sRoot + "\\All Users")) ||
    (fc.item() == (sRoot + "\\Default User")) ||
    (fc.item() == (sRoot + "\\LocalService")) ||
    (fc.item() == (sRoot + "\\NetworkService")))
    {
    continue;
    }


    // Check, if there is a folder Temporary IE Files
    if (fso.FolderExists(TIFFolder))
    {

    // Now we can delete that folder
    try
    {
    fso.DeleteFolder(TIFFolder);
    logfile.WriteLine("FOLDER: " + TIFFolder + " has been deleted
    successfully");
    }
    catch(e)
    {
    logfile.WriteLine("FOLDER: " + TIFFolder + " Could not be deleted.
    --- Error: " +e.number+ " Error-description: " + e.description);
    fcf = new Enumerator(f.files);
    if (fcf.atEnd())//for TIF this is always true
    {
    logfile.WriteLine("No files in folder.");
    }
    else
    {
    for (; !fcf.atEnd(); fcf.moveNext())
    {
    s = fcf.item();
    try
    {
    s.Delete();
    logfile.WriteLine("FILE: " + s + " has been deleted
    successfully");
    }
    catch(e)
    {
    logfile.WriteLine("FILE: " + s + " Could not be deleted. ---
    Error: " +e.number+ " Error-description: " + e.description);
    }
    }
    } }
    }
    }
    ##############################################################################
    Rosa, Apr 6, 2005
    #1
    1. Advertising

  2. Rosa

    kaeli Guest

    In article <>,
    enlightened us with...

    > the folder.
    > The peculiarity of this issue is that the files/subfolders cannot be
    > seen through the windows explorer either.


    Probably because they're hidden.

    Open Windows Explorer. (just open my documents or something)
    Click on Tools -> Folder Options
    Click on the View tab.
    Where it says "Hidden files and folders", choose the option that says "Show
    hidden files and folders".

    --
    --
    ~kaeli~
    She was engaged to a boyfriend with a wooden leg but broke
    it off.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Apr 6, 2005
    #2
    1. Advertising

  3. Rosa

    Grant Wagner Guest

    "Rosa" <> wrote in message
    news:...
    > Hi,
    > I'm trying to clear the TIF on Windows XP programmatically with the
    > below code.
    > This code works fine on any folder but the TIF. For some reason the
    > atEnd() statements always defaults to true and no files are deleted in
    > the folder.
    > The peculiarity of this issue is that the files/subfolders cannot be
    > seen through the windows explorer either. I can only access/delete
    > them through a command shell.
    > Any ideas?
    > Many thanks
    > Rosa


    To bring this on topic, I've converted the script to JScript. I've also
    removed the logging, but it should be easy enough to add back in (just
    look for where I WScript.Echo() each file I'm deleting).

    var TEMPORARY_INTERNET_FILES = 0x20;
    var COOKIES = 0x21;

    var shell = new ActiveXObject('Shell.Application');

    var folder = shell.Namespace(TEMPORARY_INTERNET_FILES);
    // or
    //var folder = shell.Namespace(COOKIES);

    // have to take the -self- property of the namespace to get the actual
    object
    folder = folder.self;

    var path = folder.Path + '\\';
    WScript.Echo(path);

    var fso = new ActiveXObject('Scripting.FileSystemObject');

    var stack = [];
    stack.push(fso.GetFolder(path));

    while (stack.length > 0)
    {
    var folder = stack.pop();
    for (var en = new Enumerator(folder.Subfolders); !en.atEnd();
    en.moveNext())
    {
    try
    {
    stack.push(en.item());
    }
    catch(e)
    {
    }
    }
    for (en = new Enumerator(folder.Files); !en.atEnd(); en.moveNext())
    {
    try
    {
    // WScript.Echo(en.item().Path);
    fso.DeleteFile(en.item().Path);
    }
    catch(e)
    {
    }
    }
    }

    Note a couple of things:

    1) My version doesn't depend on any "magic" folder names, where ever the
    Temporary Internet Files directory resides (for the current user) is
    where my script will retrieve it from (so you could set this as a logout
    script and it would clear the current user's cache when they log out of
    Windows). If you are going to modify it back to do all users, I'd
    suggest you try to figure out a more sensible way of determining the
    current profile directory, such as reading
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList\ProfilesDirectory from the Registry.

    2) Notice that I've wrapped the file manipulation code in try-catches.
    In this script if there is a problem reading a file -- like, I don't own
    it and therefore I get a security violation -- I want to skip the
    problem and continue on. I didn't want to write robust error handling
    for a script I was giving away for free and I don't use myself.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Apr 7, 2005
    #3
  4. Rosa

    Rosa Guest

    Hi Grant,

    Thanks for the adaptation. This doesn't however solve the problem
    yet...
    In regards to Kaeli, the settings are set so that I can see hidden
    files and folders. There is just some peculiarity with this particular
    folder.

    Thanks
    Rosa
    Rosa, Apr 8, 2005
    #4
  5. Rosa

    kaeli Guest

    In article <>,
    enlightened us with...
    > Hi Grant,
    >
    > Thanks for the adaptation. This doesn't however solve the problem
    > yet...
    > In regards to Kaeli, the settings are set so that I can see hidden
    > files and folders. There is just some peculiarity with this particular
    > folder.
    >


    I found this in Google Groups. I think it's relevant here. Looks like you're
    not supposed to be able to delete that stuff (TIF) programmatically?

    =========================================================
    Message-ID: <OGRVBovlBHA.1552@tkmsftngp02>

    As for Temporary Internet Files (TIFs), there is only one proper
    method for normally deleting these files. Internet Setting | General Tab |
    Delete Temporary Internet Files button. If prompted to delete Offline
    Content, do so, especially if you use Outlook Express 5.5 or above. (There's
    a bug, that's why.) Simply deleting TIFs using Windows Explorer (or
    "Cleanup" programs, for that matter) will not properly update the index.dat
    file for that folder. This can lead to problems. You *can* completely delete
    the folder, files, index.dat and all, but only in a "pure" DOS setting, and
    it's only necessary if there are problems suspected. For more in-depth
    descriptions of TIFs, Cookies, and History, go to http://groups.google.com,
    use Advanced Search, and look for messages with "Gary Terhune" as Author
    (without the quotation marks, though), and "Smoke and Mirrors" in the content
    section (this time *with* the quotation marks to indicate an exact phrase.)
    =========================================================

    --
    --
    ~kaeli~
    Black holes were created when God divided by 0.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Apr 8, 2005
    #5
  6. Rosa

    Rosa Guest

    Hello Kaeli,

    Thanks for your posting, it was very helpful!

    Cheers
    Rosa
    Rosa, Apr 12, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rosa
    Replies:
    1
    Views:
    714
    Rhino
    Mar 23, 2005
  2. Rosa
    Replies:
    2
    Views:
    605
  3. Purva
    Replies:
    0
    Views:
    115
    Purva
    Dec 28, 2005
  4. phpninja
    Replies:
    1
    Views:
    145
    Grant Wagner
    Nov 4, 2004
  5. ammalu

    deleting temporary internet files

    ammalu, Nov 26, 2008, in forum: Javascript
    Replies:
    3
    Views:
    252
    Stevo
    Nov 26, 2008
Loading...

Share This Page