Batch File to delete SOME files in folder

June 22, 2010 at 11:46:08
Specs: Windows XP
I have searched all over and have not found
anything specific to what I am looking for. I am
trying to create a Batch file that will do a
couple different things:
1) Delete files(.dot to be exact) from
C:\Documents and Settings\CURRENT
USER(?)\Application Data\Microsoft\Word,
Excel, and Templates

2) Not delete files created by the user, or at
very least not delete files older than 2 days.

I have found many batch examples to delete
all files older than x days, but that is the exact
opposite of what I figure I need. I am trying to
push this out over a small network so would
like to be able to keep one file for all users, but
the path has been giving me trouble (I have
been using C:\Documents and
settings\%UserName%\etc)

tl:dr I need a batch file to delete template files
created by Microsoft Office, while preserving
any user created templates.


See More: Batch File to delete SOME files in folder

Report •


#1
June 22, 2010 at 13:23:38
You could try %USERPROFILE% to point to the *Documents
and Settings\*...

Insomniac at large


Report •

#2
June 22, 2010 at 13:30:13
Thank you, that works and I got to test it on a couple of
profiles. Any ideas how to not delete user created files or at
least delete files created in the recent day or two, and ignore
files that were created before?

Ideally I'd like to have this run when users log in so any
automatically created templates are cleared out because they
cause issues with some programs we use.

Thanks again


Report •

#3
June 22, 2010 at 13:44:35
The first thing that came to mind it the %~t optional modifier.

From HELP CALL:

 %~t1        - expands %1 to date/time of file

I'll have to look to see what this actually returns. Could be
create date/time, or modified date/time. Manipulating the
information however would be a chore, with determining how
many days are in the prior month if date/time is "first of the
month", leapyear, weekend, etc. You don't want to wipe out
Friday's files first thing on Monday morning...

EDIT: %~t returns modified date/time...

Insomniac at large


Report •

Related Solutions

#4
June 22, 2010 at 19:25:52
I would suggest researching alternative archive-attribute.
My guess is that the "generic" templates would NOT have the "A" attribrute, and other templates would, but I might be totally wrong. If they do, you can use archive-bit much more easily than date-math (always a bitch). Then use batch to create list of all "A"-tagged files to retain.
If date-math is the only other option, call this batch "subdate" f/e::

:: subdate.bat: subtract ONE day from the input (or current if no input provided) date
:: valid inputs: (null=current), 0m-0d-yy, m-d-yy, 0m-d-yyyy, etc. (and all subst. -=/.)
@echo off
SETLOCAL enabledelayedexpansion
:: setup to allow default of current date, or override
if "%1" equ "" (set d8=%date%) else (set d8=xxx %1)
for /f "tokens=2-4 delims=-/ " %%m in ("%d8%") do (
set mo= %%m
set day= %%n
set yr=20%%o)
set mo=%mo: 0=%+0
set /a day=%day: 0=%-1
set yr=%yr:~-4%
if %day% equ 0 (
set day=31
set /a mo-=1
call :!mo! 2>nul)
echo %mo%-%day%-%yr%
GOTO :EOF

:0
set /a yr-=1
set /a mo=12
goto :eof
:2
set day=28
set /a leap="yr%%4"
if %leap% equ 0 (set /a day+=1)
goto :eof
:4
:6
:9
:11
set /a day-=1
goto :eof
:: ------ end subdate

to use subdate routine, this will subtract 3 days by calling it successively:

set subt=3
for /L %%a in (1 1 %subt%) do (
for /f %%a in ('subdate !prev!') do set prev=%%a
)
echo today minus %subt%: %prev%
::---- end snippet
note that this is very slow for any significant numbers! vbscript is much better way to do datemath. If you want
that (vbscript) version, let me know.
If you want to go this miserable (batch) route, let me know and i'll help. I would seriously look into
the Archive-bit settings first.


Report •

#5
June 22, 2010 at 23:20:37
Where in the world is the use of SET in this manner
documented?!

set mo=%mo:00=%+0
set /a day=%day:00=%-1

Insomniac at large


Report •

#6
June 23, 2010 at 00:19:22
it's not - i just experimented trying to cut down on steps, Lol!
I honestly didnt' think it'd work, but when it seemed to, what the hell... might as well...
ps:
set mo=%mo:00=%+0
that was a screw-up - should-a been set /a!
set /a mo=%mo:00=%+0

now i gotta go back to see if i'm in the crapper.


Report •

#7
June 23, 2010 at 08:20:50
Nice! String substitution within an arithmetic statement. Who
woulda thunk it? Besides you, that is! ;-)

Insomniac at large


Report •

#8
June 23, 2010 at 09:33:58
You've got a problem here. If month is August to December,
let's say for example August and %%m = "8":

set mo = 0%%m -> mo = "08"

%mo:00=% is still "08"

set /a mo=%mo:00=%+0 will give you an error because 08 is
invalid, as anything beginning with a 0 is octal and there is no
8 in octal!
You need to remove ALL leading zeros, not just when there
are two leading zeros.


Report •

#9
June 23, 2010 at 09:46:27
If %%m = "8" to begin with, there would be no need for logic to
strip leading zeros. I've not seen (computer generated)
sequences like 01, 02, 3, 04, 5, 06, 07, 8...

Insomniac at large


Report •

#10
June 23, 2010 at 09:52:17
@Orangeboy: Thanks, but then i went and threw a wrench in,
see foll...
@Klint: You're right. I had it right the first time, then screwed it up, Lol!!
I fixed the script, these statements:
set mo= %%m
set day= %%n
set yr=20%%o)
set mo=%mo: 0=%+0
set /a day=%day: 0=%-1
::---- (using space instead of zero.)
here's the testing sieve:
@echo off & setlocal enabledelayedexpansion
for /L %%a in (1 1 %1) do (
for /f %%a in ('subdate !prev!) do set prev=%%a
echo !prev!

Report •


Ask Question