.net core + vue + elementui 删除指定日期段、指定路径下的所有文件


1、呈现效果

img

2、后端

1)服务层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// <summary>
/// 删除指定修改日期段及指定路径下的所有文件
/// </summary>
/// <param name="filepath">指定路径</param>
/// <returns>返回删除结果提示</returns>
public string DeleteSpecifiedPathAllFile(FileInfomationArgs args)
{
if(args.IsNull()))
{
return "参数为空,请重新获取!";
}
try
{
DirectoryInfo info = new DirectoryInfo(args.FilePath);
// 去除文件夹的只读属性
info.Attributes = FileAttributes.Normal & FileAttributes.Directory;
// 去除文件的只读属性
File.SetAttributes(args.FilePath, FileAttributes.Normal);
// 判断文件夹是否存在
if(Directory.Exists(args.FilePath))
{
// 按指定修改时间段删除文件
if(args.StartTime != null && args.EndTime != null)
{
List < FileInfo > fileInfomations = null;
fileInfomations = info.GetFiles().Where(t => t.LastWriteTime >= args.StartTime && t.LastWriteTime <= args.EndTime).ToList();
foreach(var fileInfo in fileInfomations)
{
if(File.Exists(fileInfo.FullName))
{
// 如果有子文件则删除子文件
File.Delete(fileInfo.FullName);
}
else
{
// 循环递归删除子文件夹的所有文件
DeleteSpecifiedPathAllFile(args);
}
}
}
else // 删除指定路径的全部文件
{
foreach(var file in Directory.GetFileSystemEntries(args.FilePath))
{
if(File.Exists(file))
{
// 如果有子文件则删除子文件
File.Delete(file);
}
else
{
// 循环递归删除子文件夹的所有文件
DeleteSpecifiedPathAllFile(args);
}
}
}
// 删除已空文件夹(此步骤会删除指定目录的最底层文件夹,建议保留文件夹目录,此句注释)
// Directory.Delete(filepath, true);
}
return "当前文件修改日期段的指定路径下的所有文件删除成功!";
}
catch(Exception ex)
{
return "删除出现异常,异常原因为:" + ex.Message;
}
}

2)请求参数类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// <summary>
/// 文件信息参数
/// </summary>
public class FileInfomationArgs
{
private string _FilePath = "";
/// <summary>
/// 指定路径
/// </summary>
public string FilePath
{
get
{
return _FilePath;
}
set
{
_FilePath = value;
}
}
private DateTime ? _StartTime = null;
/// <summary>
/// 指定修改时间开始时间段
/// </summary>
public DateTime ? StartTime
{
get
{
return _StartTime;
}
set
{
_StartTime = value;
}
}
private DateTime ? _EndTime = null;
/// <summary>
/// 指定修改时间结束时间段
/// </summary>
public DateTime ? EndTime
{
get
{
return _EndTime;
}
set
{
_EndTime = value;
}
}
}

3)接口层

1
2
3
4
5
6
/// <summary>
/// 删除指定路径下的所有文件
/// </summary>
/// <param name="filepath">指定路径</param>
/// <returns>返回删除结果提示</returns>
public Result string DeleteSpecifiedPathAllFile(FileInfomationArgs args);

4)控制层

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// 删除指定路径下的所有文件
/// </summary>
/// <param name="filepath">指定路径</param>
/// <returns>返回删除结果提示</returns>
[HttpPost, HttpOptions]
public IActionResult DeleteSpecifiedPathAllFile(FileInfomationArgs args)
{
return ToJsonContent(_服务层注入.DeleteSpecifiedPathAllFile(args));
}

3、前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<html>
<head></head>
<body>
<template>
<el-form size="small" :model="form" :rules="rules" ref="form" label-width="120px">
<div class="head_main">
<el-row :gutter="24">
<el-col :span="20">
<div class="key_tip">
【关于文件清理的说明】
<p style="color: red;">本页面功能主要是删除本地指定日期段及指定路径下的所有文件</p>
<p style="color: green;">☞指定路径:本地文件夹的绝对路径</p>
<p style="color: green;">☞指定日期段:本地文件夹下的文件的最新修改日期</p>
</div>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="12">
<el-form-item label="指定路径:" prop="filePath">
<el-input class="entity" type="textarea" clearable="" placeholder="请输入指定路径" v-model="form.filePath">
</el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="12">
<el-form-item label="指定日期段:" prop="operationTime">
<el-date-picker class="entity" v-model="operationTime" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" clearable="" size="small" style="width:100%">
</el-date-picker>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="15">
<el-col :span="24">
<el-form-item>
<el-button type="primary" des="el" @click="cleanUp">
清理
</el-button>
<el-button des="el" @click="reset">
重置
</el-button>
</el-form-item>
</el-col>
</el-row>
</div>
</el-form>
</template>
<script>
import { 方法名 } from '方法名所在JS文件'

export default {
data () {
return {
form: {
filePath: '',
operationTime: '',
},
rules: {
filePath: [
{ required: true, message: '请输入指定路径', trigger: 'blur' },
],
},
}
},
created () {
},
methods: {
// 清理
cleanUp () {
this.$refs.form.validate(async (valid) => {
if (!valid) return
let startTime, endTime
if (this.operationTime) {
startTime = this.operationTime[0]
endTime = this.operationTime[1]
}
try {
await DeleteSpecifiedPathAllFile({
FilePath: this.form.filePath,
StartTime: startTime,
EndTime: endTime,
})
this.$notify({
type: 'success',
offset: 50,
title: '当前文件修改日期段的指定路径下的所有文件删除成功',
})
return
} catch (error) {
console.log('error:', error)
}
})
},
// 重置
reset () {
this.form.filePath = ''
this.operationTime = ''
},
},
}
</script>
<style lang="scss">
.head_main {
.key_tip {
box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
padding: 8px 16px;
background-color: #ecf8ff;
border-radius: 4px;
border-left: 5px solid #50bfff;
margin: 20px 0;
line-height: 22px;
}
}
</style>
</body>
</html>

代码和呈现效果如上所述。

注:指定路径为本地的绝对路径;指定日期段的判定依据是文件的修改日期


评论
  目录