Get the App
SLTechnology News&Howtos  ›  Servers  › 

Windows automatically deletes the batch script of files from n days ago

Shulou Source: shulou.com Published: 2022-06-03 06:27:10 09月20日 Update

The database backs up files every day, but only needs to keep the backup for seven days. Write a batch script file to automatically delete the files from seven days ago and set up scheduled tasks.

Automatic deletion of files requires the forfiles command

Forfiles parameters:

/ p specified path

/ s includes subdirectories

File name mask for / m lookup

/ d specifies the date, with absolute and relative dates, where-7 refers to 7 days before the current date

The command line run by / c represents the commands executed for each file. The command string should be enclosed in double quotes.

The default command is "cmd / c echo @ file". The following variables can be used in the command string:

@ file-returns the file name.

@ fname-returns a file name without an extension.

@ ext-returns only the extension of the file.

Path-returns the full path to the file.

Relpath-returns the relative path to the file.

Isdir-returns "TRUE" if the file type is a directory, or "FALSE" if it is a file.

Fsize-returns the file size in bytes.

Fdate-returns the date the file was last modified.

Ftime-returns the time when the file was last modified.

Create a new notepad file, enter the following command, and name the file "Delete the file that was backed up 7 days ago. Bat". Note the path where the file is located.

@ echo off # requires the password forfiles / p "C:\ autoscpfile\ data" / s / m * .bak / d-7 / c "cmd / c del / f @ path" forfiles / p "C:\ autoscpfile\ log" / s / m * .txt / d-7 / c "cmd / c del / f @ path"

Set up scheduled tasks after the script file is written

Open the task schedule on Windows server 2003

Start-> all programs-> attachments-> system tools-> Task schedule

Right-click the new scheduled task in the space, name the task "Delete backup 7 days ago", and double-click the task

Select browse, find the path to the batch file, and set the password, which is the login password of the user who executed the script

Set the time for the execution of the script, execute the script at 3:00 every day, and then click apply to determine

Open the task schedule on Windows server 2008 R2

Start-> Administrative tools-> Task Scheduler

In the action bar on the right, click create Task

Name the task, select the configuration, and run with the highest permissions

Select trigger, new

Select the new trigger, set the time for the task to execute, and determine

Select action, new

Click Browse, select the script path to be executed, start with the option to fill in the script path, and then determine

Tags: File task script command path selection date backup password file name time character string tool program trigger previous browse run maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Docker Shulou Technology Redmi