I have a batch file that does a backup of data from the users local profile
to their home directory on the file server. This way they have data backed
up and we also do a backup of the server every night.
There has been an on going complaint from our main office to get all old
data some four years and older off of the server and the user hard disks.
Main concerns is that the file server is low on space mainly due to poor
housekeeping. People will not clean up no matter how often we send
messages. We even have Lotus 123 and Word Perfect files DOS stored on the
server.
My supervisors want me to rewrite the batch file to check their local drive
and home directory for files over two years old and give a short notice and
delete the files. We have backup tapes that are saved for one year of all
data so it can be restored if necessary.
Thanks for any help with this code.
Bob
BobA...@home.com if you reply to me from this message remove ATHOME from
my address.
Here is a batch file that will delete files more than two years old. I will leave it up
to you to change it to 'give a short notice and delete the files' (whatever that means).
Note that the batch file will not actually delete anything until you remove the word
'echo' from line 47, which you should do once you are satisfied that the batch file
is doing what you intend. You might want to 'uncomment' line 46 (by removing the
two colons) to either echo the information to the screen or to a log file, etc.
Have fun!
==========begin file c:\CMD\TEST\del2yearsold.cmd ==========
001. @echo off
002. ::
003. :: delete all files older than two years
004. ::
005. setlocal
006. set cd=
007. set filespec=%1
008. set filepath=%~dp1
009.
010. :: first get the date two years ago
011.
012. for /f "tokens=2-4 delims=/ " %%a in (
013. 'date /t'
014. ) do (
015. set /a year = %%c - 2
016. set month=%%a
017. set day=%%b
018. )
019.
020. set expire_date=%year%%month%%day%
021.
022. :: now compare the file dates of all files in
023. :: the specified directory against the expire_date
024.
025. for /f "tokens=*" %%a in (
026. 'dir %filespec%
027. ^| find "/"
028. ^| findstr /v "DIR"'
029. ) do call :chkdate %%a
030. goto :EOF
031.
032.
033. :chkdate
034. set inrec=%*
035. if not defined cd set inrec=%inrec:~1%
036. set mm=%inrec:~0,2%
037. set dd=%inrec:~3,2%
038. set yy=%inrec:~6,2%
039. if 1%yy% GEQ 180 (
040. set century=19
041. ) else (
042. set century=20
043. )
044. set /a filedate=%century%%yy%%mm%%dd%
045. if %filedate% GEQ %expire_date% goto :EOF
046. ::echo deleting %inrec:~0,38% %filepath%%inrec:~39%
047. echo del "%filepath%%inrec:~39%"
048. goto :EOF
==========end file c:\CMD\TEST\del2yearsold.cmd ==========
--
Phil Robyn
Univ. of California, Berkeley
u n z i p m y a d d r e s s t o s e n d e - m a i l
Hi, Bob,
If you are running Win98SE, then please IGNORE my earlier reply to your post about
deleting files over two years old. My solution will not work on Win98SE. Why did
you post your question to the alt.msdos.batch.NT newsgroup? You should post your
question to the alt.msdos.batch newsgroup instead.
Sorry I didn't mention that this is mainly an NT domain which this group is
for. I do have a mix of workstations, Win98, NT, 2000. Mostly NT and now a
growing number of Win 2000 workstations. I have a separate batch file for
the Win9x workstations.
Will your example work for NT, 2000?
Thanks,
Bob Askey
"Phil Robyn" <pro...@zipuclink.berkeley.edu> wrote in message
news:3B8012DE...@zipuclink.berkeley.edu...
> Phil,
>
> Sorry I didn't mention that this is mainly an NT domain which this group is
> for. I do have a mix of workstations, Win98, NT, 2000. Mostly NT and now a
> growing number of Win 2000 workstations. I have a separate batch file for
> the Win9x workstations.
>
> Will your example work for NT, 2000?
>
> Thanks,
> Bob Askey
Yes, it will work for NT and 2000.
Our policy is that the hard drive belongs to IT and the home directory to
the user. If they decide, against policy, to store user files on the hard
drive, they are already aware that this is not supported, and that the files
are subject to loss in the following situations:
- the hard drive fails and we provide a new one with a clean image;
- their machine gets so badly misconfigured that it is faster for us to
re-image than to do all the tweaks;
- their "friend" doesn't know that their quarterly report (usually called
something like "sex-talk.doc" and located in "C:\windows\temp\" was
important and will delete it. Again.
- they clobber their files all by themselves (hey, they *all* know that this
happens :-)
> There has been an on going complaint from our main office to get all old
> data some four years and older off of the server and the user hard disks.
Server is easy, as long as management supports whatever technical policies
it takes to ensure that their system does not crash.
Hard disks is easy too, but also requires a strong policy.
But the solution depends on what the specific problem with the old files on
the hard drive is, i.e.:
- sensitive information that should not be left hanging around (requires a
strong policy that outlaws ALL use of hard drives for user files, PLUS
adequate facilities for home directories, PLUS policies that keep them from
getting clogged).
- PC performance issues.
> Main concerns is that the file server is low on space mainly due to poor
> housekeeping. People will not clean up no matter how often we send
> messages. We even have Lotus 123 and Word Perfect files DOS stored on the
> server.
Look for an auto-archiving package for the server that archives old files.
> My supervisors want me to rewrite the batch file to check their local
drive
> and home directory for files over two years old and give a short notice
and
> delete the files. We have backup tapes that are saved for one year of all
> data so it can be restored if necessary.
Process the home drives directly from the server. Re the hard drives, just
make sure that the o/s does not rely on any files that old (mine does).
No matter how many times you tell the users to delete files, they will still
say that they are important to the business. The management will then be
understandably loath to allow you to delete simply by date. You need to find
an approach that puts you, the users, and the management all on the same
side.
/Al
We have given users the option to copy files to a folder called CDCopy.
This is for the files that are old as dirt or hold some speacial purpose.
We then copy the data test it and give them two copies on CDRs. That way
they have a CD copy that they can store and reference if they need to. We
then have to be sure that the data is taken off of the local and server home
directory.
We actually do have a policy that users are responsible for data stored on
their hard drives. Some people do have data outside of the user default
data folder. This data if lost is their responsibility.
I have one concern and that is if someone has critical data and they spend
months perfecting it, it had better be backed up. If lost the user looses
but so does the company that is paying them to recreate what they lost.
Most people this happens to though become real believers in backing up.
Thanks for your input,
Bob
"Al Dunbar" <Al_D...@HoTMaiL.com> wrote in message
news:to39lcd...@corp.supernews.com...
I tried the batch file and it works great. One thing I was wondering about
is how do you set the batch to scan the path given and all sub folders?
Thanks,
Bob
> Phil,
>
> I tried the batch file and it works great. One thing I was wondering about
> is how do you set the batch to scan the path given and all sub folders?
>
> Thanks,
> Bob
>
Well, I guess you could use the following two modules. DELOLDSUBS
calls DEL2YEARSOLD for whatever directory you are in and all of its
subdirectories. You can still run DEL2YEARSOLD (slightly changed
from the previously posted version) by itself to do a single (sub)directory
only. However, this is now no longer a good design, as DEL2YEARSOLD
is now redundantly determining what the date was two years ago every
time it is called. But since I'm not getting paid for this .... ;^)
========begin file C:\CMD\TEST\DELOLDSUBS.CMD==================================
001. @echo off
002. for /r %%a in (.) do call :subdirs %%a
003. goto :EOF
004. :subdirs
005. set currdir=%1
006. set currdir=%currdir:\.=%
007. pushd %currdir%
008. call del2yearsold %currdir%
009. popd
010. goto :EOF
========end file C:\CMD\TEST\DELOLDSUBS.CMD==================================
========begin file C:\CMD\TEST\del2yearsold.cmd==================================
001. @echo off
002. ::
003. :: delete all files older than two years
004. ::
005. setlocal
006. set cd=
007. set filespec=%1
008. if defined filespec set filespec=%filespec%\
046. :: echo deleting %inrec:~0,38% %filespec%%inrec:~39%
047. echo del "%filespec%%inrec:~39%"
048. goto :EOF
========end file C:\CMD\TEST\del2yearsold.cmd==================================
I have not yet used the batch files but ran into a few problems that I
remedied. Thanks mainly to your expert example of course.
Below is what I have done. I added pause to every group of commands so I
could step through and rem'd out the @Echo Off so it displays all code
executed.
I still haven't tried to actually delete files. Also if anyone trys the
code remember I have pauses all through both batch files.
In DelOldSubs I have typed in the path to the batch files ex. Set
Startdir=C:\Temp. Using the subdirs once past the starting path the "Call
DEL2YearsOld.cmd" was invalid. ex. start C:\Temp later the changed path
would be something like C:\Temp\Backup.
Also I added quotes to the:
:subdirs "%%a" - in DelOLDSubs.cmd. This will take care of folder names
with spaces. Ex. "C:\Temp\MYBackup" would work but "C:\Temp\MY Backup"
would not work without the quotes. By reming the @Echo Off and putting
pauses all through the batch I could step through each group of commands and
see invalid paths etc. Without quotes I would see the path converted to
"C:\Temp\MY" which is invalid without the space and remainder of folder name
Backup.
Thanks for your great help!
Bob
------------------------------------
:: ========begin file
::C:\CMD\TEST\DELOLDSUBS.CMD==================================
::@echo off
:: enter the folder you will be storing batch files in.
set startdir=C:\temp
for /r %%a in (.) do call :subdirs "%%a"
goto :EOF
pause
:subdirs
set currdir=%1
set currdir=%currdir:\.=%
pause
pushd %currdir%
pause
call %startdir%\del2yearsold %currdir%
pause
popd
pause
goto :EOF
pause
::========end file
::C:\CMD\TEST\DELOLDSUBS.CMD==================================
:: ========begin file
::C:\CMD\TEST\DEL2Yearsold.CMD==================================
::@echo off
::
:: delete all files older than two years
::
setlocal
set cd=
set filespec=%1
set filepath=%~dp1
pause
:: first get the date two years ago
for /f "tokens=2-4 delims=/ " %%a in (
'date /t'
) do (
set /a year = %%c - 2
set month=%%a
set day=%%b
)
pause
set expire_date=%year%%month%%day%
pause
:: now compare the file dates of all files in
:: the specified directory against the expire_date
for /f "tokens=*" %%a in (
'dir %filespec%
^| find "/"
^| findstr /v "DIR"'
) do call :chkdate %%a
goto :EOF
pause
:chkdate
set inrec=%*
if not defined cd set inrec=%inrec:~1%
set mm=%inrec:~0,2%
set dd=%inrec:~3,2%
set yy=%inrec:~6,2%
if 1%yy% GEQ 180 (
set century=19
) else (
set century=20
)
pause
set /a filedate=%century%%yy%%mm%%dd%
if %filedate% GEQ %expire_date% goto :EOF
::echo deleting %inrec:~0,38% %filepath%%inrec:~39%
echo del "%filepath%%inrec:~39%"
goto :EOF
pause
::========end file
::C:\CMD\TEST\DEL2YearsOld.CMD==================================
> Phil,
>
> I have not yet used the batch files but ran into a few problems that I
> remedied. Thanks mainly to your expert example of course.
>
> Below is what I have done. I added pause to every group of commands so I
> could step through and rem'd out the @Echo Off so it displays all code
> executed.
>
> I still haven't tried to actually delete files. Also if anyone trys the
> code remember I have pauses all through both batch files.
>
> In DelOldSubs I have typed in the path to the batch files ex. Set
> Startdir=C:\Temp. Using the subdirs once past the starting path the "Call
> DEL2YearsOld.cmd" was invalid. ex. start C:\Temp later the changed path
> would be something like C:\Temp\Backup.
>
> Also I added quotes to the:
> :subdirs "%%a" - in DelOLDSubs.cmd. This will take care of folder names
> with spaces. Ex. "C:\Temp\MYBackup" would work but "C:\Temp\MY Backup"
> would not work without the quotes. By reming the @Echo Off and putting
> pauses all through the batch I could step through each group of commands and
> see invalid paths etc. Without quotes I would see the path converted to
> "C:\Temp\MY" which is invalid without the space and remainder of folder name
> Backup.
>
> Thanks for your great help!
> Bob
Hi, Bob,
You're very welcome. Sounds like you're on the right track to becoming a batch
programmer! Keep up the good work!
I had a chance to test the delete part of the batch file DEL2YearsOld.cmd.
I found a problem with the date set for the file date before comparison of
two years ago and file date stamp.
:chkdate
set inrec=%*
if not defined cd set inrec=%inrec:~1%
set mm=%inrec:~0,2%
set dd=%inrec:~3,2%
set yy=%inrec:~8,2% This was 6,2 it now works.
The next line that does the comparison to determine wheither to delete
doesn't work. I am not sure what to change to get that to work??
Thanks,
Bob
echo Check File Date for Deletion.
set /a filedate=%century%%yy%%mm%%dd%
pause
Gets to here and then goes to end of file. Do I need to remove the :EOF? I
copied some files that where from 1995 and it just skipped them.
if %filedate% GEQ %expire_date% goto :EOF
pause
echo.
echo delete the file.
echo deleting %inrec:~0,38% %filepath%%inrec:~39%
pause
echo del "%filepath%%inrec:~39%"
pause
goto :EOF
"Bob" <BobAske...@home.com> wrote in message
news:sWTf7.36343$vW2.16...@news1.sttln1.wa.home.com...
> Phil,
>
> I had a chance to test the delete part of the batch file DEL2YearsOld.cmd.
>
> I found a problem with the date set for the file date before comparison of
> two years ago and file date stamp.
> :chkdate
> set inrec=%*
> if not defined cd set inrec=%inrec:~1%
>
> set mm=%inrec:~0,2%
> set dd=%inrec:~3,2%
> set yy=%inrec:~8,2% This was 6,2 it now works.
>
> The next line that does the comparison to determine wheither to delete
> doesn't work. I am not sure what to change to get that to work??
> Thanks,
> Bob
Hi, Bob,
If you post a COMPLETE copy of what you are running and a screen print
of a partial directory listing (are you using WinNT 4.0, Win2000, or WinXP?),
then maybe I can help you. It's not really possible to diagnose your problem
by seeing only fragments of the batch file.
I am working with Windows 2000. I will be working with both NT and 2000
mostly.
I setup some folders under C:\Temp ex. C:\Temp\My Old Files. I did a search
for files '98 through '99.
While stepping through I can see the comparison of date two years ago and
the file date. Then the next step just loops back to the next file.
I am at home right now so I can't get a screen capture.
Code currently using which is called from DELOldSubs.cmd that you posted for
me. Also in code below there is the command GEQ what does it mean ex.
if %filedate% GEQ %expire_date% goto :EOF?
Bob
--------------------------------------------------
::@echo off
::
:: delete all files older than two years
::
setlocal
set cd=
set filespec=%1
set filepath=%~dp1
pause
:: first get the date two years ago
for /f "tokens=2-4 delims=/ " %%a in (
'date /t'
) do (
set /a year = %%c - 2
set month=%%a
set day=%%b
)
pause
set expire_date=%month%%day%%year%
pause
:: now compare the file dates of all files in
:: the specified directory against the expire_date
for /f "tokens=*" %%a in (
'dir %filespec%
^| find "/"
^| findstr /v "DIR"'
) do call :chkdate %%a
goto :EOF
pause
:chkdate
set inrec=%*
if not defined cd set inrec=%inrec:~1%
set mm=%inrec:~0,2%
set dd=%inrec:~3,2%
set yy=%inrec:~8,2%
if 1%yy% GEQ 180 (
set century=19
) else (
set century=20
)
pause
echo Check File Date for Deletion.
set /a filedate=%century%%yy%%mm%%dd%
pause
if %filedate% GEQ %expire_date% goto :EOF ::**I GET TO HERE AND IT FAILS**
pause
echo.
echo delete the file.
echo deleting %inrec:~0,38% %filepath%%inrec:~39%
pause
echo del "%filepath%%inrec:~39%"
pause
goto :EOF
pause
In order for the date comparison to work, both expire_date
and the date of each file MUST be in the format YYYYMMDD.
For some reason, you have decided to alter the format of
expire_date to MMDDYYYY. You will have to restore it
to its original format:
set expire_date=%year%%month%%day%
Please see comments above regarding the necessary date format of YYYYMMDD
>
> pause
> echo.
> echo delete the file.
> echo deleting %inrec:~0,38% %filepath%%inrec:~39%
> pause
> echo del "%filepath%%inrec:~39%"
> pause
> goto :EOF
> pause
I checked and the problem is that the %expiredate% is wrong I will try
fixing it using something similar to your file date format.
Thanks,
Bob
What does GEQ mean?
GEQ means 'Greater than or EQual to'. Type 'IF /?' (without the apostrophes)
at the CMD prompt for information.
"Greater than or EQual".
/Al