batch file to delete files based on criteria

D

djc

I need to throw together a simple batch file to backup a file. I will
schedule this via Task Scheduler to run every hour or so. I will have the
file go to a compressed folder but space will still be an issue. I would
like to create a second batch file that I will schedule to delete 'some' of
the files to make room. I have done something like this via vbscript and wsh
in the past put for times sake I want to keep this quick and simple. 2 files
are fine.

any suggestions on the cleanup part? I need to delete only some of the
files... not all.

Actually, now that I'm thinking about it a little, I used some string
manipulation with vbscript to make unique file names for each copy (using
the now() function and striping invalid characters for filenames). How will
I get unique filenames just using a batch file? Is this going to be simpler
or more complex?

any input is appreciated. My goal is something very simple. (all windows
2000 machines by the way)

thanks.
 
M

Matthias Tacke

djc said:
I need to throw together a simple batch file to backup a file. I will
schedule this via Task Scheduler to run every hour or so. I will have the
file go to a compressed folder but space will still be an issue. I would
like to create a second batch file that I will schedule to delete 'some' of
the files to make room. I have done something like this via vbscript and wsh
in the past put for times sake I want to keep this quick and simple. 2 files
are fine.

any suggestions on the cleanup part? I need to delete only some of the
files... not all.

Actually, now that I'm thinking about it a little, I used some string
manipulation with vbscript to make unique file names for each copy (using
the now() function and striping invalid characters for filenames). How will
I get unique filenames just using a batch file? Is this going to be simpler
or more complex?

any input is appreciated. My goal is something very simple. (all windows
2000 machines by the way)

See the help of set /? at the very end are %random% and %date% %time%.
depending on your date format you will have to reorder date elements /
strip /- characters also possible with the set commands.

HTH
 
P

Paul R. Sadowski

This will give you a fairly Unique filename in the form of YYYYMMDDHHMMSS

In theory there could be a name collision if you run the file twice at the
same time (within 1 second) so you could do a IF EXIST then call
MkNewFileName again till you get a truly unique name.

'UniqueFn.vbs
wscript.echo Year(Now) & Right("00" & Month(Now),2) & Right("00"&Day(Now),2)
& _
Right("00"&Hour(Now),2) & Right("00"&Minute(Now),2) &
Right("00"&Second(Now),2)
Wscript.Sleep 1000

Then from a batch file

setlocal
for /f %c in ('cscript UniqueFn.vbs') do set FN=%c

You can then either use %FN% as the new name or as part of the new name.

E.g:
setlocal
for /f %%c in ('cscript UniqueFn.vbs') do set FN=%%c
call :MkNewFileName "MY File.txt" %%FN%%
ren "MY File.txt" "%FN%"
:: %FN% now contains something like MY File.20040602133002.txt
goto :EOF

:MkNewFileName
echo %2
Set FN=%~n1.%2%~x1
Goto :EOF
 
P

Paul R. Sadowski

'UniqueFn.vbs
wscript.echo Year(Now) & Right("00" & Month(Now),2) & _
Right("00"&Day(Now),2) & Right("00"&Hour(Now),2) & _
Right("00"&Minute(Now),2) &
Right("00"&Second(Now),2)
wscript.Sleep 1000

Then from a batch file

setlocal
for /f %c in ('cscript UniqueFn.vbs') do set FN=%c

You can then either use %FN% as the new name or as part of the new name.

E.g:
setlocal
for /f %%c in ('cscript UniqueFn.vbs') do set FN=%%c
call :MkNewFileName "MY File.txt" %FN%
:: %FN% now contains something like MY File.20040602133002.txt
ren "MY File.txt" "%FN%"
goto :EOF

:MkNewFileName
Set FN=%~n1.%2%~x1
Goto :EOF
 
J

Jerold Schulman

I need to throw together a simple batch file to backup a file. I will
schedule this via Task Scheduler to run every hour or so. I will have the
file go to a compressed folder but space will still be an issue. I would
like to create a second batch file that I will schedule to delete 'some' of
the files to make room. I have done something like this via vbscript and wsh
in the past put for times sake I want to keep this quick and simple. 2 files
are fine.

any suggestions on the cleanup part? I need to delete only some of the
files... not all.

Actually, now that I'm thinking about it a little, I used some string
manipulation with vbscript to make unique file names for each copy (using
the now() function and striping invalid characters for filenames). How will
I get unique filenames just using a batch file? Is this going to be simpler
or more complex?

any input is appreciated. My goal is something very simple. (all windows
2000 machines by the way)

thanks.

Let's assume that your file path is c:\folder\filename.dat

and that you wish to backit it up to c:\bkpfolder and keep 2 copies.

One batch file will suffice. You will need univdate.bat from tip 4835 in the
'Tips & Tricks' at http://www.jsiinc.com

@echo off
setlocal
call univdate
if "%yy%" LSS "10" set yy=200%yy%
if "%yy%" LSS "100" set yy=20%yy%
set /a mm=100%mm%%%100
if %mm% LSS 10 set mm=0%mm%
set /a dd=100%dd%%%100
if %dd% LSS 10 set dd=0%dd%
for /f "Tokens=1* Delims=:" %%a in ('time /t') do (
set hh=%%a
set mn=%%b
)
for /f "Skip=1 Tokens=*" %%a in ('dir "c:\bkpfolder\filename*.dat" /b') do (
del /q "c:\bkpfolder\%%a"
)
copy "c:\folder\filename.dat" "c:\bkpfolder\filename_%yy%%mm%%dd%_%hh%%mn%.dat"
endlocal


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
G

guard

I need to throw together a simple batch file to backup a file. I will
schedule this via Task Scheduler to run every hour or so. I will have the
file go to a compressed folder but space will still be an issue. I would
like to create a second batch file that I will schedule to delete 'some' of
the files to make room. I have done something like this via vbscript and wsh
in the past put for times sake I want to keep this quick and simple. 2 files
are fine.

any suggestions on the cleanup part? I need to delete only some of the
files... not all.

Actually, now that I'm thinking about it a little, I used some string
manipulation with vbscript to make unique file names for each copy (using
the now() function and striping invalid characters for filenames). How will
I get unique filenames just using a batch file? Is this going to be simpler
or more complex?

any input is appreciated. My goal is something very simple. (all windows
2000 machines by the way)

thanks.

The Advanced Version of Snapshot will create date/time coded backups that
can be easily processed for deletion based on whatever criteria you decide.
This script is included with the FREE Advanced NT/2K/XP/K3 Command Library
from (http://ntlib.com). Help screen follows (watch for line wraps):

*******

^
=========================================================================
/~\ Snapshot Advanced Release 2004.03.28 (Free from TheSystemGuard.com!)
/cmd\
=========================================================================

Create a snapshot backup when a file changes.
____________________________________________________________________________
___

TSG_Snapshot [/?] [drive:][path]FileToProtect [SafeLocation] [Interval]

SWITCHES:
/? Display this help.

PARAMETERS: Default values are shown in {}'s
FileToProtect A SINGLE file to protect (no wildcards!)
[SafeLocation] Folder to hold snapshots {C:\!Snapshot}
[Interval] Minutes between file checks {5}

ERRORLEVELS:
0 = Normal or expected finish.
1 = Help screen was displayed.
2 = Script exited with a known error. See %ErrorMsg% for details.
3 = Script exited with an unspecified error.

NOTES:
The SafeLocation folder will be created if necessary.

Interval time DOES NOT include time spent comparing and copying files.
The Interval counter is reset at the beginning of each waiting period.
Actual wait time is NOT PRECISE, and can overshoot by several seconds,
depending upon system load (see the .Wait command).

If any optional parameter is used, all optional parameters to it's left
MUST
be specified. For example, to use Interval, also specify SafeLocation.

All Advanced Scripts (those prefixed with "TSG_") from TheSystemGuard.com
REQUIRE the Advanced NT/2K/XP/K3 Command Library (ntlib.cmd). This library
provides over 200 resources for writing, testing and maintaining RELIABLE
shell scripts using only the native commands and utilities that are present
in
ALL NT-based operating systems. Write a script ONCE using ntlib.cmd and
your
script will perform CONSISTENTLY under Windows NT4, 2000, XP and Server
2003!
Request your FREE copy of the Advanced Library today at (http://ntlib.com).

****************************************************************************
***

Snapshot Expert uses the enlarged "Expert Command Library" (NTCmdLib.cmd),
enabling many additional command line switches and parameters. All Expert
Scripts (those prefixed with "!") are complete, ready-to-run applications
that do not require any commands or utilities not already present in every
NT/2K/XP/K3 install. !Scripts are distributed uncompressed and are fully
commented so you can quickly fashion them to your specific requirements.

An example of what you're missing (from the help screen of !Snapshot.cmd):

SWITCHES: Default values are shown in {}'s
/C Compact window during run (size restored at Final Report). {off}
/FP FullPath (use the full path of FileToProtect in window title)
{name.ext}
/NV NoVisual (don't change console colors to reflect operating status)
{on}
/P Pause (pause console after Final Report) {off}
/WF WaitforFile (bypass error if FileToProtect does not exist) {off}

PARAMETERS: Default values are shown in {}'s
[Warning_Trigger] Consecutive missed snapshots before warning. {1}
[Error_Trigger] Consecutive missed snapshots before error. {2}
[MaxSnaps] Maximum snapshots to take before stopping. {25}
[MaxTime] Maximum time (in minutes) to wait. {480} (8
hours)

!Snapshot.cmd provides a color-coded, self-adjusting console window to
enable
at-a-glance monitoring of all running snapshots while using minimum desktop
real estate. We also include a ".reg" file to allow right-click access to
the
!Snapshot script from inside Windows Explorer. Just highlight any file,
then
right-click, and select "Protect with !Snapshot".

Both the Expert Command Library and !Snapshot are "Almost Free" at $9 or
less!
See (http://TheSystemGuard.com/AlmostFree.asp) for The Almost Free Price
List.

The VERY LATEST KNOWLEDGE pertaining to the Windows NT/2K/XP/K3 command
line
can be found at TheSystemGuard.com. Check the MasterCatalog often for a
continually growing collection
(http://TheSystemGuard.com/MasterCatalog.asp).
You'll learn to build RELIABLE and MAINTAINABLE shell scripts that will
always
perform CONSISTENTLY on every Windows NT-based system right out of the box!
Don't forget our FREE KnowledgeLetter at (http://BoomingOrFuming.com)
and...

Congratulations On Your Decision To RISE ABOVE THE REST!!!
____________________________________________________________________________
___

TSG_Snapshot [/?] [drive:][path]FileToProtect [SafeLocation] [Interval]

*******

For more information, see the Snapshot Page at
(http://TheSystemGuard.com/Scripts/Snapshot).

An overview of what the script does is at
(http://TheSystemGuard.com/Scripts/Snapshot/TSG_Snapshot.outline.htm)

The complete commented cmdsrc ("Cmd Source") is at
(http://TheSystemGuard.com/Scripts/Snapshot/TSG_Snapshot.cmdsrc.htm)

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
D

djc

sounds cool. I'll check it out.
Thanks

guard said:
I need to throw together a simple batch file to backup a file. I will
schedule this via Task Scheduler to run every hour or so. I will have the
file go to a compressed folder but space will still be an issue. I would
like to create a second batch file that I will schedule to delete 'some' of
the files to make room. I have done something like this via vbscript and wsh
in the past put for times sake I want to keep this quick and simple. 2 files
are fine.

any suggestions on the cleanup part? I need to delete only some of the
files... not all.

Actually, now that I'm thinking about it a little, I used some string
manipulation with vbscript to make unique file names for each copy (using
the now() function and striping invalid characters for filenames). How will
I get unique filenames just using a batch file? Is this going to be simpler
or more complex?

any input is appreciated. My goal is something very simple. (all windows
2000 machines by the way)

thanks.

The Advanced Version of Snapshot will create date/time coded backups that
can be easily processed for deletion based on whatever criteria you decide.
This script is included with the FREE Advanced NT/2K/XP/K3 Command Library
from (http://ntlib.com). Help screen follows (watch for line wraps):

*******

^
=========================================================================
/~\ Snapshot Advanced Release 2004.03.28 (Free from TheSystemGuard.com!)
/cmd\
=========================================================================

Create a snapshot backup when a file changes.
____________________________________________________________________________
___

TSG_Snapshot [/?] [drive:][path]FileToProtect [SafeLocation] [Interval]

SWITCHES:
/? Display this help.

PARAMETERS: Default values are shown in {}'s
FileToProtect A SINGLE file to protect (no wildcards!)
[SafeLocation] Folder to hold snapshots {C:\!Snapshot}
[Interval] Minutes between file checks {5}

ERRORLEVELS:
0 = Normal or expected finish.
1 = Help screen was displayed.
2 = Script exited with a known error. See %ErrorMsg% for details.
3 = Script exited with an unspecified error.

NOTES:
The SafeLocation folder will be created if necessary.

Interval time DOES NOT include time spent comparing and copying files.
The Interval counter is reset at the beginning of each waiting period.
Actual wait time is NOT PRECISE, and can overshoot by several seconds,
depending upon system load (see the .Wait command).

If any optional parameter is used, all optional parameters to it's left
MUST
be specified. For example, to use Interval, also specify SafeLocation.

All Advanced Scripts (those prefixed with "TSG_") from TheSystemGuard.com
REQUIRE the Advanced NT/2K/XP/K3 Command Library (ntlib.cmd). This library
provides over 200 resources for writing, testing and maintaining RELIABLE
shell scripts using only the native commands and utilities that are present
in
ALL NT-based operating systems. Write a script ONCE using ntlib.cmd and
your
script will perform CONSISTENTLY under Windows NT4, 2000, XP and Server
2003!
Request your FREE copy of the Advanced Library today at (http://ntlib.com).****************************************************************************
***

Snapshot Expert uses the enlarged "Expert Command Library" (NTCmdLib.cmd),
enabling many additional command line switches and parameters. All Expert
Scripts (those prefixed with "!") are complete, ready-to-run applications
that do not require any commands or utilities not already present in every
NT/2K/XP/K3 install. !Scripts are distributed uncompressed and are fully
commented so you can quickly fashion them to your specific requirements.

An example of what you're missing (from the help screen of !Snapshot.cmd):

SWITCHES: Default values are shown in {}'s
/C Compact window during run (size restored at Final Report). {off}
/FP FullPath (use the full path of FileToProtect in window title)
{name.ext}
/NV NoVisual (don't change console colors to reflect operating status)
{on}
/P Pause (pause console after Final Report) {off}
/WF WaitforFile (bypass error if FileToProtect does not exist) {off}

PARAMETERS: Default values are shown in {}'s
[Warning_Trigger] Consecutive missed snapshots before warning. {1}
[Error_Trigger] Consecutive missed snapshots before error. {2}
[MaxSnaps] Maximum snapshots to take before stopping. {25}
[MaxTime] Maximum time (in minutes) to wait. {480} (8
hours)

!Snapshot.cmd provides a color-coded, self-adjusting console window to
enable
at-a-glance monitoring of all running snapshots while using minimum desktop
real estate. We also include a ".reg" file to allow right-click access to
the
!Snapshot script from inside Windows Explorer. Just highlight any file,
then
right-click, and select "Protect with !Snapshot".

Both the Expert Command Library and !Snapshot are "Almost Free" at $9 or
less!
See (http://TheSystemGuard.com/AlmostFree.asp) for The Almost Free Price
List.

The VERY LATEST KNOWLEDGE pertaining to the Windows NT/2K/XP/K3 command
line
can be found at TheSystemGuard.com. Check the MasterCatalog often for a
continually growing collection
(http://TheSystemGuard.com/MasterCatalog.asp).
You'll learn to build RELIABLE and MAINTAINABLE shell scripts that will
always
perform CONSISTENTLY on every Windows NT-based system right out of the box!
Don't forget our FREE KnowledgeLetter at (http://BoomingOrFuming.com)
and...

Congratulations On Your Decision To RISE ABOVE THE REST!!!
____________________________________________________________________________
___

TSG_Snapshot [/?] [drive:][path]FileToProtect [SafeLocation] [Interval]

*******

For more information, see the Snapshot Page at
(http://TheSystemGuard.com/Scripts/Snapshot).

An overview of what the script does is at
(http://TheSystemGuard.com/Scripts/Snapshot/TSG_Snapshot.outline.htm)

The complete commented cmdsrc ("Cmd Source") is at
(http://TheSystemGuard.com/Scripts/Snapshot/TSG_Snapshot.cmdsrc.htm)

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top