-
Notifications
You must be signed in to change notification settings - Fork 31
/
IOUtils.psm1
97 lines (78 loc) · 3.38 KB
/
IOUtils.psm1
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
using namespace System;
using namespace System.IO;
using namespace System.Collections.Generic;
#检查路径合法性;
function Assert-Path([string]$path){
if([String]::IsNullOrEmpty($path)){
throw $path + "can't be null or empty";
}
if(!(Test-Path $path)){
throw ($path + "is not a valid path!");
}
}
#拷贝函数,当目标文件的最后写入时间晚于源文件时,将不执行拷贝;
function Copy-FileEx([string]$originFileName,[string]$targetFileName){
#若目标文件存在;
if([File]::Exists($targetFileName)){
$originFileInfo = New-Object FileInfo($originFileName);
$targetFileInfo = New-Object FileInfo($targetFileName);
#若目标文件不比源文件更"旧",则不继续执行;
if($originFileInfo.LastWriteTime -le $targetFileInfo.LastWriteTime){
Write-Output ($msg = $originFileInfo.Name + " no need to update")
return;
}
}
#若目录不存在尝试创建;
[string]$targetDirectory = [Path]::GetDirectoryName($targetFileName);
if(!([Directory]::Exists($targetDirectory))){
[Directory]::CreateDirectory($targetDirectory);
}
Copy-Item $originFileName $targetFileName
Write-Output ($msg = $originFileName + " copied")
}
#读取指定文件的所有行,空白或"#"开头的将被忽略;
function Get-ValidLines([string]$fileName){
$allLines = [File]::ReadAllLines($fileName);
$validLines = New-Object List[String];
foreach($line in $allLines){
$line = [string]$line;
if([String]::IsNullOrEmpty($line)){
continue;
}
if($line.StartsWith("#")){
continue;
}
$validLines.Add($line);
}
return $validLines;
}
#拷贝目录,当目标文件的最后写入时间晚于源文件时,将不执行拷贝;
function Copy-DirectoryEx([string]$originDirectoryName,[string]$tartgetDirectoryName){
Assert-Path $originDirectoryName;
if(![Directory]::Exists($originDirectoryName)){
Write-Error ($msg = $originDirectoryName + " doesn't exist.");
Exit;
}
if(![Directory]::Exists($tartgetDirectoryName)){
[Directory]::CreateDirectory($tartgetDirectoryName);
}
#目录字符串统一化;
$originDirectoryName = [Path]::GetDirectoryName($originDirectoryName+"\\");
$tartgetDirectoryName = [Path]::GetDirectoryName($tartgetDirectoryName+"\\");
$innerDirectories = [Directory]::EnumerateDirectories($originDirectoryName,"*.*", [System.IO.SearchOption]::AllDirectories);
$innerFiles = [Directory]::EnumerateFiles($originDirectoryName,"*", [System.IO.SearchOption]::AllDirectories);
foreach($directoryName in $innerDirectories){
$directoryName = [string]$directoryName;
$innerDirectoryName = $directoryName.Substring($originDirectoryName.Length + 1);
$thisTargetDirectoryName = [Path]::Combine($tartgetDirectoryName,$innerDirectoryName);
if(![Directory]::Exists($thisTargetDirectoryName)){
[Directory]::CreateDirectory($thisTargetDirectoryName);
}
}
foreach($fileName in $innerFiles){
$fileName = [string]$fileName;
$innerFileName = $fileName.Substring($originDirectoryName.Length + 1);
$thisTargetFileName = [Path]::Combine($tartgetDirectoryName,$innerFileName);
Copy-FileEx $fileName $thisTargetFileName
}
}