1.

Solve : Batch file to delete file?

Answer»

Hello,

I am in need of a batch file to automatically delete a file older in some stated hours
at a specified hourly interval. I have a system that generates a report at some hourly
interval. It renames the new one leaving the old one still there. With this i require a
batch file to periodically delete the old file base on some hours older.

Thanks.
If all you are doing is keeping the newest file in the folder then this should WORK. Run this batch file outside of the folder it is working on.  Make sure you test this on some test files first.
Code: [Select]echo off
pushd "c:\folder path"
FOR /F "skip=1 delims=" %%G in ('dir /a-d /B /o-d') do del "%%~G"
popdI tried your script in a temporal folder and it deleted all files including it self and leaving just
the current one. My folder contains some other files and i don't want to end up deleting
other files as well. Please modify it to not delete other files. Thanks. Quote from: Akod on January 11, 2015, 11:24:59 AM

I tried your script in a temporal folder and it deleted all files including it self and leaving just
the current one. My folder contains some other files and i don't want to end up deleting
other files as well. Please modify it to not delete other files. Thanks.
If the batch file deleted itself then you didn't FOLLOW my instructions!!!!
Based on the information you gave me the script works as described.

Until you can supply more information on what folder the files are in and what files are in the folder and what files need to remain and what files need to be deleted there is little anyone can do to help you.  You need to think about what you are trying to do and be very specific with your instructions.Squashman, I suspect Akod doesn't have the knowledge to change this line:

Code: [Select]pushd "c:\folder path"
Akod - you must have failed to comprehend the instructions - why didn't you ask a question describing which part you didn't UNDERSTAND?Below is a description of the files in this location where the latest file is to be deleted:

Control3.dmp > (Time of creation 11:26pm)
Control2.dmp > (Time of creation 5:45pm)
schedule2 > (folder)
Schedule3 > (folder)
schedule > (folder)

The directory contains 2 files and 3 folders. The application we running automatically generates the two
files at the time indicated above. Because of low disk ISSUE, anytime a file is created, the old one needs
to be deleted.

Please hope this explanation helps to get a solution.
Thanks. You didn't answer all my questions but this again is the best I can do with the limited information.
Code: [Select]echo off
pushd "c:\folder path"
FOR /F "skip=1 delims=" %%G in ('dir /a-d /b /o-d control*.dmp') do del "%%~G"
popd


Discussion

No Comment Found