• 0

[Delphi or BATCH] Deleting alot of files in dirs


Question

I need to make a script to delete temporary files in alot of users profiles.

The directory structure is like this:

C:/Users/username1/TempFiles/

C:/Users/username2/TempFiles/

C:/Users/username3/TempFiles/

C:/Users/username4/TempFiles/

I need a script that will automatically go through all the users directories and delete all the contents from TempFiles.

It can be done in either a BATCH file or in Delphi.

Thank-you to anyone that can help me.

3 answers to this question

Recommended Posts

  • 0

Try something like this:

procedure GetDirs(Path: string; FileList: TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
  begin
    repeat
      begin
        if sr.Attr and faDirectory = faDirectory then
          filelist.Add(sr.name)
        else

      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

procedure GetFiles(Path: string; FileList: TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
  begin
    repeat
      begin
        if sr.Attr and faDirectory = faDirectory then
        else
        if sr.Attr and faSysFile = faSysFile then
        else
        begin
          filelist.Add(sr.Name);
        end;
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

procedure ParseAndDelete;
var
  DirList, FileList: TStringList;
  i, j: integer;
  RootDir, TempDir: string;
begin
  RootDir:='c:\users\';
  TempDir:='\tempfiles\';

  DirList:=TstringList.Create;
  FileList:=TstringList.Create;
  try
    GetDirs(RootDir, DirList);
    if DirList.Count=0 then
    begin
      showmessage('No profile directories found!');
      exit;
    end;

    for i:=0 to DirList.Count - 1 do
    begin
      if directoryexists(RootDir + '\' + DirList[i] + '\' + TempDir) then
      begin
        FileList.Clear;
        GetFiles(RootDir + '\' + DirList[i] + '\' + TempDir, FileList);
        if FileList.Count=0 then continue;
        for j:=0 to FileList.Count - 1 do
          deletefile(RootDir + '\' + DirList[i] + '\' + TempDir + FileList[j]);
      end;

    end;

  finally
    DirList.Free;
    FileList.Free;
  end;
end;

Change rootdir and tempdir var's to whatever your actual path is. The code looks through all the directories inside rootdir, sees if tempdir exists inside those and if it does deletes all the files inside tempdir. This is just off the top of my head but it should work :)

Edited by bgbennyboy
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That has AI shoved in as well, and I expect at some point that will become more in your face.
    • Edge on a Mac? Hell no, as bad as Edge on Linux, I did not use Edge on Windows. The Ai rubbish is getting too much, seem like it is being put into every flipping app and devices these days. I have disabled it on my Mac, dead easy to do that, disabled it on Brave. I agree with David Uzondu, why can't browsers be simple like they used to be. Not that Chrome is simple and still have AI shoved into that. I like the Duck Duck go browser, but that have AI even, but it can be disabled, the only problem with the Duck Duck go browser is no support for extensions. Maybe we should back to Mosaic.
    • Another reason to stick with Firefox
    • Yes, the Control Pannel issue has been fixed. As I ask when people claim it hasn't, can you name a single thing in Windows 11 that can only be done by opening Control Panel? Your comment about Error 0x80070643 is so asinine that I debated if you were worth a reply, but here I am. The article mentioned a VERY specific issue with the WinRE partition size being too small in Windows 10 and preventing updates from being installed unless you reinstalled or manually fixed it. That issue does not exist on Windows 11, period. To point to random issues with Windows updates on 11 that don't affect everyone and don't involve that issue is being dishonest. Had the article said "Issues with updates, such as..." then I'd agree with you, but it didn't, it simply mentioned that one specific issue, which despite your claim, does not exist on 11 or on newer builds of 10 (so the article is kind of wrong, it did get fixed, just not gracefully). As far as UX/UI comes more down to opinion, but again, you are saying it is "terrible" which has nothing to do with consistency. Windows 11 has a more consistent interface. You are welcome to say it is constantly bad, but I don't see how any objective person can say it isn't more constant than Windows 10's unholy merger of Windows 7 and Windows 8. To be fair, I really like Windows 10's interface, it worked great, but was it consistent, NO, it was one of the least consistent interfaces MS has ever made. I agree with the lack of competition. Linux continues to get better
  • Recent Achievements

    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      671
    2. 2
      ATLien_0
      256
    3. 3
      Xenon
      165
    4. 4
      neufuse
      145
    5. 5
      +FloatingFatMan
      114
  • Tell a friend

    Love Neowin? Tell a friend!