
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to create a script to delete txt file that older than file modified date?
Dear all, need some help here.
I need to know how to write a script to delete txt file that older than file modified date?
For exmaple: i have 3 files. i want the script to delete all the txt files , and only keep the latest modified date file.
myfile_20200610.txt
myfile_20200611.txt
myfile_20200612.txt
please help guys!


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Maybe you should look for Windows batch script to do this.


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I use the below script to do the job
Set Concatenate=;
sub ScanFolder(Root)
for each Ext in 'FileName*.txt'
for each FoundFile in filelist(Root & '\*' & Ext)
AllFiles:
$(Concatenate)
Load
'$(FoundFile)' AS FileName,
FileTime('$(FoundFile)') AS FileModifiedDate
Autogenerate(1);
set Concatenate = concatenate;
next FoundFile
next Ext
for each SubDirectory in dirlist(Root&'\*')
call ScanFolder(SubDirectory)
next SubDirectory
end Sub
Call ScanFolder('FilePath');
LatestFile:
NoConcatenate
First 1 Load
FileName as File_Do_not_Delete,
FileModifiedDate
Resident AllFiles
Order By FileModifiedDate Desc
;
Let vLatestFileName=Peek('File_Do_not_Delete', 0, 'LatestFile');
Trace $(vLatestFileName) is the latest file;
Older_Files:
NoConcatenate
Load
FileName,
FileModifiedDate
Resident AllFiles
Where
Not Exists(File_Do_not_Delete,FileName)
;
Drop Table AllFiles, LatestFile;
Let vNoofRows= Num(NoOfRows('Older_Files'),'#,##0');
Trace no of older files found $(vNoofRows);
Let vFileNo=0;
For vFileNo= 0 to NoofRows('Older_Files')
Let vDeleteFileName=Peek('FileName', $(vFileNo), 'Older_Files');
TRACE Deleting $(vDeleteFileName) $(vFileNo);
execute cmd.exe /C del $(vDeleteFileName);
Next;
Note: Check the "Can Execute External Programs" setting on the Settings tab in the script editor (bottom part of the screen).
.png)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If Vamsee's post got you a working solution, we would greatly appreciate it if you would return to your thread and close it out by using the Accept as Solution button on his post to mark it. If you did something else, you can post that and then use that as the solution, and if you still require assistance, please leave an update.
The only other option I see is along Arthur's post, maybe call a PowerShell script etc. to do things.
Regards,
Brett
I now work a compressed schedule, Tuesday, Wednesday and Thursday, so those will be the days I will reply to any follow-up posts.
