Page 1 of 2

Delete files on specific date

Posted: 05 Oct 2014 14:16
by Ubaid
Hi Friends,
Please can any one help to create scrip to delete all files on define date.
for example I would like to delete all files of directory "C:\test\" on date 24/10/2014.

appreciate in advance.

Re: Delete files on specific date

Posted: 05 Oct 2014 16:27
by Squashman
You will need to create a Windows scheduled task and have it run this command in a batch file.

Code: Select all

del /q c:\test\*.*

Re: Delete files on specific date

Posted: 06 Oct 2014 02:45
by Ubaid
Thanks for your reply.

My system is DOS operating system. I would make it in Autoexec.bat

Re: Delete files on specific date

Posted: 06 Oct 2014 03:25
by foxidrive
Ubaid wrote:Thanks for your reply.

My system is DOS operating system. I would make it in Autoexec.bat


Which version of dos are you using?

Is it FreeDos or Microsoft MSDOS V6.22 or some other dos system?

Re: Delete files on specific date

Posted: 06 Oct 2014 03:47
by Ubaid
it is MSDOS 6.22 version

Re: Delete files on specific date

Posted: 06 Oct 2014 06:59
by foxidrive
Ubaid wrote:it is MSDOS 6.22 version


Then a Qbasic script with batch file is useful:

Code: Select all

@echo off
echo open "!_~_!.bat" for output as #1: a$=DATE$>!_~_!.bas
echo ? #1, "SET DAY="+RIGHT$(a$,4)+LEFT$(a$,2)+MID$(a$,4,2)>>!_~_!.bas
echo SYSTEM>>!_~_!.bas
qbasic /run !_~_!.bas
call !_~_!.bat
del !_~_!.bat
del !_~_!.bas
if %day%==20141024 deltree /y c:\test\

Re: Delete files on specific date

Posted: 06 Oct 2014 07:34
by ShadowThief
I never realized just how I easy I had it with modern batch until I tried to help with this (I did eventually get it, though).

Try this:

Code: Select all

@echo off
REM Based on www.robvanderwoude.com/datetime.php
ver | date > temp.bat
echo set date=%%4>current.bat
call temp.bat
del temp.bat
del current.bat

echo y > y.txt
if "%date%"=="24-10-2014" del C:\test\*.* < y.txt
del y.txt


Note that this code assumes the output of the DATE command is in the DD-MM-YYYY format.

Re: Delete files on specific date

Posted: 06 Oct 2014 09:08
by foxidrive
ShadowThief wrote:

Code: Select all

echo y > y.txt
if "%date%"=="24-10-2014" del C:\test\*.* < y.txt
del y.txt



This also works in MSDOS

Code: Select all

echo y|del c:\test\*.*


The date variation was an issue back then too, and which is why the qbasic solution was preferable as it eliminates region differences.

Re: Delete files on specific date

Posted: 06 Oct 2014 09:23
by ShadowThief
foxidrive wrote:This also works in MSDOS

Code: Select all

echo y|del c:\test\*.*


It took me way longer than it should have to figure out a workaround for that not working in a batch script. Piping clearly works in the script, but does piping the output of an echo behave differently in DOS scripts vs on the command line?

Re: Delete files on specific date

Posted: 06 Oct 2014 09:28
by foxidrive
ShadowThief wrote:does piping the output of an echo behave differently in DOS scripts vs on the command line?


Do you have an example where it failed?

I don't recall a command.com prompt being any different in that respect.

Re: Delete files on specific date

Posted: 06 Oct 2014 09:41
by ShadowThief
foxidrive wrote:
ShadowThief wrote:does piping the output of an echo behave differently in DOS scripts vs on the command line?


Do you have an example where it failed?

I don't recall a command.com prompt being any different in that respect.

echo y|del c:\test\*.* didn't work (nothing was echoed to the screen and no files were deleted) when I tried putting it in the script, but it works fine on the command line.

Re: Delete files on specific date

Posted: 06 Oct 2014 09:55
by foxidrive
ShadowThief wrote:echo y|del c:\test\*.* didn't work (nothing was echoed to the screen and no files were deleted) when I tried putting it in the script, but it works fine on the command line.


It works fine in my MSDOS 6.22 VM in a batch file and also on the dos prompt.
The screen output is the same too.

Which OS are you using to test it in?

Re: Delete files on specific date

Posted: 06 Oct 2014 10:16
by ShadowThief
I'm also running an MS-DOS 6.22 VM.

Image

Re: Delete files on specific date

Posted: 06 Oct 2014 10:26
by foxidrive
Add an echo to show you the IF date compare - and you are using c:\temp and not c:\test

Re: Delete files on specific date

Posted: 06 Oct 2014 10:34
by ShadowThief
Image

It definitely should be working, and if it's working for you and not me, then there's some issue on my side. Maybe I have some syntax error where the del thinks the entire if statement is being piped into it?