Delete files on specific date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Ubaid
Posts: 4
Joined: 05 Oct 2014 13:35

Delete files on specific date

#1 Post by Ubaid » 05 Oct 2014 14:16

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Delete files on specific date

#2 Post by Squashman » 05 Oct 2014 16:27

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\*.*

Ubaid
Posts: 4
Joined: 05 Oct 2014 13:35

Re: Delete files on specific date

#3 Post by Ubaid » 06 Oct 2014 02:45

Thanks for your reply.

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#4 Post by foxidrive » 06 Oct 2014 03:25

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?

Ubaid
Posts: 4
Joined: 05 Oct 2014 13:35

Re: Delete files on specific date

#5 Post by Ubaid » 06 Oct 2014 03:47

it is MSDOS 6.22 version

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#6 Post by foxidrive » 06 Oct 2014 06:59

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\

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files on specific date

#7 Post by ShadowThief » 06 Oct 2014 07:34

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#8 Post by foxidrive » 06 Oct 2014 09:08

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.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files on specific date

#9 Post by ShadowThief » 06 Oct 2014 09:23

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#10 Post by foxidrive » 06 Oct 2014 09:28

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.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files on specific date

#11 Post by ShadowThief » 06 Oct 2014 09:41

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#12 Post by foxidrive » 06 Oct 2014 09:55

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?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files on specific date

#13 Post by ShadowThief » 06 Oct 2014 10:16

I'm also running an MS-DOS 6.22 VM.

Image

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete files on specific date

#14 Post by foxidrive » 06 Oct 2014 10:26

Add an echo to show you the IF date compare - and you are using c:\temp and not c:\test

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Delete files on specific date

#15 Post by ShadowThief » 06 Oct 2014 10:34

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?

Post Reply