Solved

batch file needed to delete files

Posted on 2007-11-26
6
499 Views
Last Modified: 2008-09-30
Hello!
 I would like to have a batch file that I can run as a scheduled task one a week.

I would like to search the c:/userfiles folder and its subfolders and delete .log, .dat and .zmtp files that are 31days or older.
0
Comment
Question by:bobby35ny
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
ncrones earned 25 total points
ID: 20353161
check out the MS Scripting Centre - great resource for all things scripting - has tools (eg scriptomatic) that can help you do what you are trying to get done - that is if they don't already have an eg up there for you to copy...

http://www.microsoft.com/technet/scriptcenter

0
 
LVL 10

Assisted Solution

by:fm250
fm250 earned 25 total points
ID: 20354158
0
 
LVL 8

Expert Comment

by:Nivlesh
ID: 21952277
Hi there.

There is a beautiful command forfiles.exe that is part of the windows 2000 resource kit and comes standard in windows 2003 server. To address your problem, run the following script with forfiles

rem set variables etc
SET _forfiles="{path to forfiles .. for eg c:\windows\system32\forfiles.exe}"
SET _start_dir=c:\userfiles
SET _age=31

rem start the command

%_forfiles% /p %_start_dir% /m *.log /d -%_age% /c "cmd /c delete @path"
%_forfiles% /p %_start_dir% /m *.dat /d -%_age% /c "cmd /c delete @path"
%_forfiles% /p %_start_dir% /m *.zmtp /d -%_age% /c "cmd /c delete @path"

rem end of script


I suggest running the following to ensure you are capturing the right files before deleting

%_forfiles% /p %_start_dir% /m *.log /d -%_age% /c "cmd /c echo @path was modified on @fdate"
%_forfiles% /p %_start_dir% /m *.dat /d -%_age% /c "cmd /c echo @path was modified on @fdate"
%_forfiles% /p %_start_dir% /m *.zmtp /d -%_age% /c "cmd /c echo @path was modified on @fdate"

Once you sure, run the script. If you cannot get the forfiles.exe then let me know and I will email it to you. Also, if you want forfiles to recurse subdirectories from c:\userfiles then use the /s switch.

Full command syntax of forfiles is

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {dd-MM-yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "dd-MM-yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01-01-2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +8-7-2008 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"


All the best. Let me know how you go


0
 
LVL 8

Expert Comment

by:Nivlesh
ID: 21952304
my apologies .. in the command line use del instead of delete

%_forfiles% /p %_start_dir% /m *.log /d -%_age% /c "cmd /c del @path"
%_forfiles% /p %_start_dir% /m *.dat /d -%_age% /c "cmd /c del @path"
%_forfiles% /p %_start_dir% /m *.zmtp /d -%_age% /c "cmd /c del @path"
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question