Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use powershell to delete files before the specified date and add them to the scheduled task

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The machine system used for the video conversion of the company meeting is windows. When the program conversion fails, temporary files will be generated on disk D, which over time will cause the disk to be full and the new video conference recording content cannot be converted. In order to avoid this situation, the corresponding delete and add scheduled task scripts are temporarily written with powershell.

System environment: Microsoft Windows Server 2012 R2 Standard

Powershell version: 4.0

1. Delete script

Here, put it under the root directory of the D disk, and the file name is D:\ delete_tmp_files.ps1. Delete the files and directories from 15 days ago.

Delete the script delete_tmp_files.ps1 as follows:

# delete old tmp files,just save files in 15 days~$TimeOutDays=15$ filePath= "D:\ tmp" $allFiles=get-childitem-path $filePath foreach ($files in $allFiles) {$daypan= ((get-date)-$files.lastwritetime). Days if ($daypan-gt $TimeOutDays) {remove-item $files.fullname-Recurse-force}}

-Recurse indicates recursion, and subdirectories can be deleted

-force forced deletion, can delete hidden and read-only files (delete all file force fully,delete all hidden or read-only files)

two。 Add scheduled task

Select the add_schedule_task.ps1 script and choose to run it using PowerShell with the right mouse button.

The powershell script add_schedule_task.ps1 that joins the scheduled task is as follows:

Ipmo PSScheduledJob $T = New-JobTrigger-Weekly-DaysOfWeek 0 Trigger 1, 2, 3, 4, 5, 5, 6-At 2:38AMRegister-ScheduledJob-Name Delete-Tmp-Files-FilePath "D:\ delete_tmp_files.ps1"-Trigger $T

-DaysOfWeek: in weekly scheduled tasks, specify the day of the week to run, which is usually used with-Weekly.

Sunday-- > Monday-- >.-- > Saturday can be expressed in English or in numbers, and the corresponding table is as follows:

The English SundayMondayTuesdayWednesdayThursdayFridaySaturday number 01234563 on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. View scheduled tasks

Click Server Manager-- > Task Scheduler-- > Microsoft-- > Windows-- > PowerShell-- > ScheduledJobs

Reference:

Use powershell to delete files earlier than a specified date

Delete-files-older-than-15-days-using-powershell

HOW TO CREATE SCHEDULE TASK USING POWERSHELL

New-JobTrigger

How-to-delete-a-folder-or-file-using-powershell

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report