PowerShell 目录文件管理


目录文件相关方法的封装与案例详解

李俊才 的 CSDN 博客:https://blog.csdn.net/qq_28550263?type=blog
邮箱 :291148484@163.com
CSDN 主页https://blog.csdn.net/qq_28550263?spm=1001.2101.3001.5343
本文地址https://blog.csdn.net/qq_28550263/article/details/124378032

本文通过以一个 Path 类,归纳了 Powershell 中常用的文件路径方法。这些方法可以迅速方便的用于 Powershell 编写大型运维脚本中。


目 录


1. 获取绝对路径
2. 文件名
3. 获取盘符
4. 当前系统临时目录
5. 文件所在目录名
6. 文件或者目录是否实际存在
7. 获取相对路径
8. 获取目录的当前子项目
9. 递归获取目录的所有子孙项目
10. 在指定目录下筛选文件
11. 在指定目录下筛选目录
12. 获取文件或者目录的修改时间
13. 判断指定路径是否实际是一个文件夹
14. 判断指定路径是否实际是一个文件
15. 判断文件时间是否早于某个值
16. 判断目录是否为空
17. 判断某个路径值是否为绝对路径
18. 获取目录的属性描述符
19. 判 断是否包含某个属性描述符
20. 计算文件或目录的大小
21. 连结多个路径
22. 拆分路径
23. 创建目录
24. Path 环境变量相关方法
25. 删除路径对应的项目


1. 获取绝对路径

static [string]abspath([string]$path){<#Return absolute pathParams======- $path [string] Path of file.#>return Resolve-Path -Path $path;
}

如图:

2. 文件名

static [string]basename([string]$path){<#Returns the file name from the absolute path value represented by a string.Params======- $path [string] The absolute path of the file.- $extension [bool] Whether to remove the extension.#>return  [System.IO.Path]::GetFileName($path)
}
static [string]basename([string]$path,[bool]$extension=$True){if($extension){return  [System.IO.Path]::GetFileName($path)}else{return [System.IO.Path]::GetFileNameWithoutExtension($path)}
}

如图:

3. 获取盘符

static [string]rootname([string]$path){<# Return root directory information #>return [System.IO.Path]::GetPathRoot($path)
}

【例如】:

4. 当前系统临时目录

static [string]get_temp_path(){<# Returns the path of the current system temporary directory. #>return [System.IO.Path]::GetTempPath()
}

【例如】:

5. 获取文件所在目录名

static [string]dirname([string]$path){<#Returns the name of the folder where the file is located.Params======- $path [string] Represents the absolute path of a file.#>return [Path]::basename([System.IO.Path]::GetDirectoryName($path))
}
static [string]dirname([string]$path, [bool]$absolute=$True){if($absolute){return [System.IO.Path]::GetDirectoryName($path)}else{return [Path]::basename([System.IO.Path]::GetDirectoryName($path))}
}

【例如】:

6. 判断文件或者目录是否实际存在

static [bool] exists([string]$path){<#Determine whether the specified file or directory exists.Params======- $path [string] The path value represented by a string.Returns=======- $True  exist.- $False non-existent.#>return Test-Path -Path $path
}

例如:

7. 获取相对路径

static [string]relpath([string]$relativeTo, [string]$path){<#Returns the relative path from one path to another.Params======- $relativeTo [string] The source path relative to the result. This path is always treated as a directory.- $path [string] Target path.Returns=======[string] Relative path, or path if the paths do not share the same root.Exceptions==========- [ArgumentNullException] The value of $relativeTo or $path is null。- [ArgumentException] $relativeTo or $path is actually null.#>return [System.IO.Path]::GetRelativePath($relativeTo, $path)
}

【例如】:

8. 获取目录的当前子项目

static [string[]] get_items([string]$path){<#Get all subprojects in the specified directory. Params======- $path [string] The path value represented by a string.- $sift [string] Optional, used to indicate the filtered content.* When the parameter value is' file', only the absolute path of the files in it will be returned.* When the parameter value is' folder', the absolute path of the directory (folder) in it is returned.Notes=====This method only gets the current level of subdirectories, while the folders containing subdirectories will not be traversed.If you need to recursively obtain all descendants directories, you should consider using `get_descendants()` method instead.#>$item_obj = Get-ChildItem $path |  Sort-Object$ary = [ArrayList]::new()foreach ($item in $item_obj){$ary.Add($item.ToString())}return $ary
}
static [string[]] get_items([string]$path, [string]$sift){if($sift -eq 'file'){$files = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $False} | Sort-Object;$ary = [ArrayList]::new()foreach ($item in $files){$ary.Add($item.ToString())}return $ary}elseif ($sift -eq 'folder') {$folders = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object;$ary = [ArrayList]::new()foreach ($item in $folders){$ary.Add($item.ToString())}return $ary}else{$item_obj = Get-ChildItem $path |  Sort-Object$ary = [ArrayList]::new()foreach ($item in $item_obj){$ary.Add($item.ToString())}return $ary}
}

【例如】:


其中:

  • 由于该目录中没有直接子项目为文件,因此当指定第二个参数为file时,结果为空;
  • 指定第二个参数为folder和不指定第二个参数的效果完全一致。

9. 递归获取目录的所有子孙项目

static [string[]] get_descendants([string]$path){<#Get all items in the specified directory, and recursively traverse all descendant folders.Params======- $path [string] The path value represented by a string.- $sift [string] Optional, used to indicate the filtered content.* When the parameter value is' file', only the absolute path of the files in it will be returned.* When the parameter value is' folder', the absolute path of the directory (folder) in it is returned.#>$ary = [ArrayList]::new();$item_obj = Get-ChildItem $path |  Sort-Object; # current directoryforeach ($item in $item_obj){if([Path]::is_dirname($item)){$sub_ary = [Path]::get_descendants($item);$ary.AddRange($sub_ary);}else{$ary.Add($item);}}return $ary
}
static [string[]] get_descendants([string]$path, [string]$sift){$files = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $False} | Sort-Object;$folders = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object;$ary = [ArrayList]::new()# only fileif($sift -eq 'file'){if($null -ne $files){foreach ($file in $files){$ary.Add($file)}}foreach ($item in $folders){$ary.AddRange([Path]::get_descendants($item,'file'))}}# only direlseif ($sift -eq 'folder') {if($null -ne $folders){foreach ($item in $folders){$ary.Add($item);$ary.AddRange([Path]::get_descendants($item,'folder'))}}}# allelse{$item_obj = Get-ChildItem $path |  Sort-Object; # current directoryforeach ($item in $item_obj){if([Path]::is_dirname($item)){$sub_ary = [Path]::get_descendants($item);$ary.AddRange($sub_ary);}else{$ary.Add($item);}}}return $ary
}

【例如】:

方法 get_descendants 与方法 get_items 的区别在于, get_descendants的查找范围不限于其自身,还包括其所有的子孙目录。

10. 在指定目录下筛选文件

static [string[]]filter_files([string]$path, [string]$sub_string){<#Filter the files containing the specified string from the specified directory.Params======- $path [string] - $sub_string [string] - $recursion [bool] Recursively search all descendant directories, defaulf $False#>$ary = [ArrayList]::new();foreach ($item in [Path]::get_items($path,'file')) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary
}
static [string[]]filter_files([string]$path, [string]$sub_string, [bool]$recursion){$ary = [ArrayList]::new();if($recursion){$all = [Path]::get_descendants($path,'file')}else{$all = [Path]::get_items($path,'file')}foreach ($item in $all) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary
}

【例如】:

11. 在指定目录下筛选目录

static [string[]]filter_dirs([string]$path, [string]$sub_string){$ary = [ArrayList]::new();foreach ($item in [Path]::get_items($path,'folder')) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary
}
static [string[]]filter_dirs([string]$path, [string]$sub_string, [bool]$recursion){$ary = [ArrayList]::new();if($recursion){$all = [Path]::get_descendants($path,'folder')}else{$all = [Path]::get_items($path,'folder')}foreach ($item in $all) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary
}

【例如】:

12. 获取文件或者目录的修改时间

static [System.DateTime]get_modify_time([string]$path){<#Returns the last modification time of a file or directory.Params======- $path [string] The path value represented by a string.#>return [System.IO.DirectoryInfo]::new($path).LastWriteTime
}

【例如】:

13. 判断指定路径是否实际是一个文件夹

static [bool]is_dirname([string]$path){<#Specifies whether the path represents a folder.Params======- $path [string] The path value represented by a string.Returns=======- $True  If the specified path represents a folder- $False VersaNotes=====In most cases, you can also use the command: `Test-Path -Path $path -PathType leaf `, However, using the `Test-Path` command can't deal with the case that the file name contains "[" as the powershell compiler will throw back an error in this case.#>return [Path]::has_attributes($path,"Directory")
}

【例如】:

14. 判断指定路径是否实际是一个文件

static [bool]is_filename([string]$path){<#Returns whether the specified path value represents a file.Params======- $path The path value represented by a string.Notes=====In most cases, you can also use the command: `Test-Path -Path $path -PathType leaf `, However, using the `Test-Path` command can't deal with the case that the file name contains "[" as the powershell compiler will throw back an error in this case.#>return -not [Path]::has_attributes($path,"Directory")
}

【例如】:

15. 判断文件是否比指定日期新

static [bool]is_newer_than($path, $time){<#Test whether the time of a file is before the specified date.Params======- $path [string] The path value represented by a string.- $time A point-in-time string, such as "July 13, 2009"Returns=======- $True  If the modification time of the specified file is newer than this point in time;- $False Versa。#>return Test-Path -Path $path -NewerThan $time
}

该方法仅适用于文件系统驱动器。

【例如】:

16. 判断目录是否为空

static [bool]is_empty([string]$path){<#The folder is not empty, or the specified path is a file.Params======- $path [string] The path value represented by a string.Retuens=======- $True  Folder is empty.- $False The folder is not empty, or the specified path is a file.#>if([Path]::is_dirname($path)){if((Get-ChildItem $path).count -eq 0){return $True}else{return $False}}else{write-host -ForegroundColor Yellow "Warning: The actual parameter of the variable `$path is a file name while a folder name is expected. "return $False}
}

【例如】:

17. 判断某个路径值是否为绝对路径

static [bool]is_abs([string]$path){<# Determine whether the path value represents an absolute path.Params======- $path [string] The path value represented by a string.Retuens=======- $True  Is an absolute path.- $False Versa.#>return Split-Path -Path $path -IsAbsolute
}

【例如】:

18. 获取目录的属性描述符

static [string[]]get_attributes([string]$path){<#Gets the operator of attribute description of the directory.#>return [System.IO.File]::GetAttributes($path).ToString().Split(", ")
}

【例如】:


【又如】:

19. 判断是否包含某个属性描述符

static [bool]has_attributes([string]$path, [string]$attributes){<#Returns whether the file or directory contains an attribute descriptor.Params======- $path [string] The path value represented by a string.- $attributes [string] Represents an attribute descriptor of a directory. Retuens=======- $True  Includes.- $False Not includes.#>return [Path]::get_attributes($path).Contains($attributes)
}

【其中】:
$attributes 的值可以是:

描述
Archive 此文件标记为包含在增量备份操作中。 每当修改文件时,Windows 会设置该属性,并且在增量备份期间处理文件时,备份软件应进行清理该属性。
Compressed 此文件是压缩文件。
Device 留待将来使用。
Directory 此文件是一个目录。 Directory 在 Windows、Linux 和 macOS 上受支持。
Encrypted 此文件或目录已加密。 对于文件来说,表示文件中的所有数据都是加密的。 对于目录来说,表示新创建的文件和目录在默认情况下是加密的。
Hidden 文件是隐藏的,因此没有包括在普通的目录列表中。 Hidden 在 Windows、Linux 和 macOS 上受支持。
IntegrityStream 文件或目录包括完整性支持数据。 在此值适用于文件时,文件中的所有数据流具有完整性支持。 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应包括完整性支持。
Normal 该文件是没有特殊属性的标准文件。 仅当其单独使用时,此特性才有效。 Normal 在 Windows、Linux 和 macOS 上受支持。
NoScrubData 文件或目录从完整性扫描数据中排除。 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应不包括数据完整性。
NotContentIndexed 将不会通过操作系统的内容索引服务来索引此文件。
Offline 此文件处于脱机状态, 文件数据不能立即供使用。
ReadOnly 文件为只读文件。 ReadOnly 在 Windows、Linux 和 macOS 上受支持。 在 Linux 和 macOS 上,更改 ReadOnly 标记是权限操作。
ReparsePoint 文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。 ReparsePoint 在 Windows、Linux 和 macOS 上受支持。
SparseFile 此文件是稀疏文件。 稀疏文件一般是数据通常为零的大文件。
System 此文件是系统文件。 即,该文件是操作系统的一部分或者由操作系统以独占方式使用。
Temporary 文件是临时文件。 临时文件包含当执行应用程序时需要的,但当应用程序完成后不需要的数据。 文件系统尝试将所有数据保存在内存中,而不是将数据刷新回大容量存储,以便可以快速访问。 当临时文件不再需要时,应用程序应立即删除它。

【例如】:

可以知道,该文件是一个具有 Hidden的文件(隐藏文件)。

20. 计算文件或目录的大小

static [int64]$counter;
static [int64]get_size([string]$path){<#Returns the file size, or an error if the file does not exist.Params======- $path [string] The path value represented by a string.#>[Path]::counter = -1;return [Path]::get_size($path,[Path]::counter)
}
static [int64]get_size([string]$path,[string]$ParentId){$count_size = 0;$file_infos = Get-ChildItem $path;# If it is a file, directly return the file size.if($file_infos.Attribute -eq 'Archive'){write-host $path+" is a file."$count_size = $count_size + $file_infos.Length;}# If it is a directory, the total size is calculated recursively.else{$count = $file_infos.Count;for ($i = 0; $i -lt $file_infos.Count; $i++) {$child = $file_infos[$i];[Path]::counter = [Path]::counter+1; # Each one is assigned an ID separately.$ID = [Path]::counter;# If it is a file, the size is accumulated.if([Path]::is_filename($child)){$count_size =  $count_size + $child.Length;}# If it is a directory, continue to recursively accumulate the size.else{$count_size =  $count_size + [Path]::get_size($child,$ID)}# 进度条显示$percent = $i / $count;  # 以子条目计算百分比if($ParentId -eq -1){Write-Progress -ID $ID -Activity ("Total counts "+$count.ToString()+"bytes of path: "+$child.ToString()) -Status "$percent% completed." -PercentComplete $percent;}else{Write-Progress -ID $ID -ParentId $ParentId -Activity ("Total counts "+$count.ToString()+"bytes of path: "+$child.ToString()) -Status "$percent% completed." -PercentComplete $percent;}}}return $count_size
}

【例如】:

21. 连结路径

static [string]join($path, $childpath){<# Connect the directory and file name into a path.Params======- $path [string] The path value represented by a string.- $childpath [string] childpath path value represented by a string.#>return Join-Path -Path $path -ChildPath $childpath
}

【例如】:

22. 拆分路径

static [string[]]split([string]$path){<#Divide the path into two parts: dirname and basename.Params======- path [string] The path value represented by a string.Returns=======- $dirname   The absolute path of the folder.- $basename  Subfile or subdirectory name#>$dirname = Split-Path -Path $path -Leaf;$basename = Split-Path -Path $path;return $dirname,$basename
}

【例如】:

23. 创建目录

static [void]make($path){<#Create a series of folders according to the given path.Params======- path [string] The path value represented by a string.#>New-Item -Path ([Path]::dirname($path, $True)) -Name ([Path]::basename($path)) -ItemType "directory"
}

【例如】:


其中

  • 如果目录完全存在,则会提示错误 "An item with the specified name XXX already exists",例如两次重复创建:
  • 如果所创建的目录的祖先文件夹都不存在,则会逐个创建祖先文件夹。

24. Path 环境变量相关方法

static [string[]]get_enviroment(){return [Environment]::GetEnvironmentVariable('Path', 'Machine')
}
static [void]set_enviroment($path){[Environment]::SetEnvironmentVariable('Path', $path,'Machine')
}
static [void]set_enviroment($path,$env_var_target){if($env_var_target -eq 'Machine'){[Environment]::SetEnvironmentVariable('Path', $path,'Machine')}elseif($env_var_target -eq 'User'){[Environment]::SetEnvironmentVariable('Path', $path,'User')}else{write-host -ForegroundColor Red "ValueError: The value of variable  `"`$env_var_target`" can only be one of 'Machine' or 'User'." }
}

【例如】:

25. 删除路径对应的项目

static [void] delete([string]$path){<#Delete the file or directory corresponding to the specified path.Params======- path [string] The path value represented by a string.#>Remove-Item $path;
}

【例如】:

附录: Path类代码

<#
@Auth: Jack Lee;
@Email: 291148484@163.com;
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
using namespace System;
using namespace System.Collections;
using namespace System.Collections.Generic;class Path{static [string]abspath([string]$path){<#Return absolute pathParams======- $path [string] Path of file.#>return Resolve-Path -Path $path; }static [string]basename([string]$path){<#Returns the file name from the absolute path value represented by a string.Params======- $path [string] The absolute path of the file.- $extension [bool] Whether to remove the extension.#>return  [System.IO.Path]::GetFileName($path)}static [string]basename([string]$path,[bool]$extension=$True){if($extension){return  [System.IO.Path]::GetFileName($path)}else{return [System.IO.Path]::GetFileNameWithoutExtension($path)}}static [string]rootname([string]$path){<# Return root directory information #>return [System.IO.Path]::GetPathRoot($path)}static [string]get_temp_path(){<# Returns the path of the current system temporary directory. #>return [System.IO.Path]::GetTempPath()}static [string]dirname([string]$path){<#Returns the name of the folder where the file is located.Params======- $path [string] Represents the absolute path of a file.#>return [Path]::basename([System.IO.Path]::GetDirectoryName($path))}static [string]dirname([string]$path, [bool]$absolute=$True){if($absolute){return [System.IO.Path]::GetDirectoryName($path)}else{return [Path]::basename([System.IO.Path]::GetDirectoryName($path))}}static [bool] exists([string]$path){<#Determine whether the specified file or directory exists.Params======- $path [string] The path value represented by a string.Returns=======- $True  exist.- $False non-existent.#>return Test-Path -Path $path}static [string]relpath([string]$relativeTo, [string]$path){<#Returns the relative path from one path to another.Params======- $relativeTo [string] The source path relative to the result. This path is always treated as a directory.- $path [string] Target path.Returns=======[string] Relative path, or path if the paths do not share the same root.Exceptions==========- [ArgumentNullException] The value of $relativeTo or $path is null。- [ArgumentException] $relativeTo or $path is actually null.#>return [System.IO.Path]::GetRelativePath($relativeTo, $path)}static [string[]] get_items([string]$path){<#Get all subprojects in the specified directory. Params======- $path [string] The path value represented by a string.- $sift [string] Optional, used to indicate the filtered content.* When the parameter value is' file', only the absolute path of the files in it will be returned.* When the parameter value is' folder', the absolute path of the directory (folder) in it is returned.Notes=====This method only gets the current level of subdirectories, while the folders containing subdirectories will not be traversed.If you need to recursively obtain all descendants directories, you should consider using `get_descendants()` method instead.#>$item_obj = Get-ChildItem $path |  Sort-Object$ary = [ArrayList]::new()foreach ($item in $item_obj){$ary.Add($item.ToString())}return $ary}static [string[]] get_items([string]$path, [string]$sift){if($sift -eq 'file'){$files = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $False} | Sort-Object;$ary = [ArrayList]::new()foreach ($item in $files){$ary.Add($item.ToString())}return $ary}elseif ($sift -eq 'folder') {$folders = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object;$ary = [ArrayList]::new()foreach ($item in $folders){$ary.Add($item.ToString())}return $ary}else{$item_obj = Get-ChildItem $path |  Sort-Object$ary = [ArrayList]::new()foreach ($item in $item_obj){$ary.Add($item.ToString())}return $ary}}static [string[]] get_descendants([string]$path){<#Get all items in the specified directory, and recursively traverse all descendant folders.Params======- $path [string] The path value represented by a string.- $sift [string] Optional, used to indicate the filtered content.* When the parameter value is' file', only the absolute path of the files in it will be returned.* When the parameter value is' folder', the absolute path of the directory (folder) in it is returned.#>$ary = [ArrayList]::new();$item_obj = Get-ChildItem $path |  Sort-Object; # current directoryforeach ($item in $item_obj){if([Path]::is_dirname($item)){$sub_ary = [Path]::get_descendants($item);$ary.AddRange($sub_ary);}else{$ary.Add($item);}}return $ary}static [string[]] get_descendants([string]$path, [string]$sift){$files = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $False} | Sort-Object;$folders = Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object;$ary = [ArrayList]::new()# only fileif($sift -eq 'file'){if($null -ne $files){foreach ($file in $files){$ary.Add($file)}}foreach ($item in $folders){$ary.AddRange([Path]::get_descendants($item,'file'))}}# only direlseif ($sift -eq 'folder') {if($null -ne $folders){foreach ($item in $folders){$ary.Add($item);$ary.AddRange([Path]::get_descendants($item,'folder'))}}}# allelse{$item_obj = Get-ChildItem $path |  Sort-Object; # current directoryforeach ($item in $item_obj){if([Path]::is_dirname($item)){$sub_ary = [Path]::get_descendants($item);$ary.AddRange($sub_ary);}else{$ary.Add($item);}}}return $ary}static [string[]]filter_files([string]$path, [string]$sub_string){<#Filter the files containing the specified string from the specified directory.Params======- $path [string] - $sub_string [string] - $recursion [bool] Recursively search all descendant directories, defaulf $False#>$ary = [ArrayList]::new();foreach ($item in [Path]::get_items($path,'file')) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary}static [string[]]filter_files([string]$path, [string]$sub_string, [bool]$recursion){$ary = [ArrayList]::new();if($recursion){$all = [Path]::get_descendants($path,'file')}else{$all = [Path]::get_items($path,'file')}foreach ($item in $all) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary}static [string[]]filter_dirs([string]$path, [string]$sub_string){$ary = [ArrayList]::new();foreach ($item in [Path]::get_items($path,'folder')) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary}static [string[]]filter_dirs([string]$path, [string]$sub_string, [bool]$recursion){$ary = [ArrayList]::new();if($recursion){$all = [Path]::get_descendants($path,'folder')}else{$all = [Path]::get_items($path,'folder')}foreach ($item in $all) {if([Path]::basename($item).Contains($sub_string)){$ary.Add($item)}}return $ary}static [System.DateTime]get_modify_time([string]$path){<#Returns the last modification time of a file or directory.Params======- $path [string] The path value represented by a string.#>return [System.IO.DirectoryInfo]::new($path).LastWriteTime}static [bool]is_dirname([string]$path){<#Specifies whether the path represents a folder.Params======- $path [string] The path value represented by a string.Returns=======- $True  If the specified path represents a folder- $False VersaNotes=====In most cases, you can also use the command: `Test-Path -Path $path -PathType leaf `, However, using the `Test-Path` command can't deal with the case that the file name contains "[" as the powershell compiler will throw back an error in this case.#>return [Path]::has_attributes($path,"Directory")}static [bool]is_filename([string]$path){<#Returns whether the specified path value represents a file.Params======- $path The path value represented by a string.Notes=====In most cases, you can also use the command: `Test-Path -Path $path -PathType leaf `, However, using the `Test-Path` command can't deal with the case that the file name contains "[" as the powershell compiler will throw back an error in this case.#>return -not [Path]::has_attributes($path,"Directory")}static [bool]is_newer_than($path, $time){<#Test whether the time of a file is before the specified date.Params======- $path [string] The path value represented by a string.- $time A point-in-time string, such as "July 13, 2009"Returns=======- $True  If the modification time of the specified file is newer than this point in time;- $False Versa。#>return Test-Path -Path $path -NewerThan $time}static [bool]is_empty([string]$path){<#The folder is not empty, or the specified path is a file.Params======- $path [string] The path value represented by a string.Retuens=======- $True  Folder is empty.- $False The folder is not empty, or the specified path is a file.#>if([Path]::is_dirname($path)){if((Get-ChildItem $path).count -eq 0){return $True}else{return $False}}else{write-host -ForegroundColor Yellow "Warning: The actual parameter of the variable `$path is a file name while a folder name is expected. "return $False}}static [bool]is_abs([string]$path){<# Determine whether the path value represents an absolute path.Params======- $path [string] The path value represented by a string.Retuens=======- $True  Is an absolute path.- $False Versa.#>return Split-Path -Path $path -IsAbsolute}static [string[]]get_attributes([string]$path){<#Gets the operator of attribute description of the directory.#>return [System.IO.File]::GetAttributes($path).ToString().Split(", ")}static [bool]has_attributes([string]$path, [string]$attributes){<#Returns whether the file or directory contains an attribute descriptor.Params======- $path [string] The path value represented by a string.- $attributes [string] Represents an attribute descriptor of a directory. The value of $attributes could be:* Archive 此文件标记为包含在增量备份操作中。 每当修改文件时,Windows 会设置该属性,并且在增量备份期间处理文件时,备份软件应进行清理该属性。* Compressed 此文件是压缩文件。* Device 留待将来使用。* Directory 此文件是一个目录。 Directory 在 Windows、Linux 和 macOS 上受支持。* Encrypted 此文件或目录已加密。 对于文件来说,表示文件中的所有数据都是加密的。 对于目录来说,表示新创建的文件和目录在默认情况下是加密的。* Hidden 文件是隐藏的,因此没有包括在普通的目录列表中。 Hidden 在 Windows、Linux 和 macOS 上受支持。* IntegrityStream 文件或目录包括完整性支持数据。 在此值适用于文件时,文件中的所有数据流具有完整性支持。 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应包括完整性支持。* Normal 该文件是没有特殊属性的标准文件。 仅当其单独使用时,此特性才有效。 Normal 在 Windows、Linux 和 macOS 上受支持。* NoScrubData 文件或目录从完整性扫描数据中排除。 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应不包括数据完整性。* NotContentIndexed 将不会通过操作系统的内容索引服务来索引此文件。* Offline 此文件处于脱机状态, 文件数据不能立即供使用。* ReadOnly 文件为只读文件。 ReadOnly 在 Windows、Linux 和 macOS 上受支持。 在 Linux 和 macOS 上,更改 ReadOnly 标记是权限操作。* ReparsePoint 文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。 ReparsePoint 在 Windows、Linux 和 macOS 上受支持。* SparseFile 此文件是稀疏文件。 稀疏文件一般是数据通常为零的大文件。* System 此文件是系统文件。 即,该文件是操作系统的一部分或者由操作系统以独占方式使用。* Temporary 文件是临时文件。 临时文件包含当执行应用程序时需要的,但当应用程序完成后不需要的数据。 文件系统尝试将所有数据保存在内存中,而不是将数据刷新回大容量存储,以便可以快速访问。 当临时文件不再需要时,应用程序应立即删除它。Retuens=======- $True  Includes.- $False Not includes.#>return [Path]::get_attributes($path).Contains($attributes)}static [int64]$counter;static [int64]get_size([string]$path){<#Returns the file size, or an error if the file does not exist.Params======- $path [string] The path value represented by a string.#>[Path]::counter = -1;return [Path]::get_size($path,[Path]::counter)}static [int64]get_size([string]$path,[string]$ParentId){$count_size = 0;$file_infos = Get-ChildItem $path;# If it is a file, directly return the file size.if($file_infos.Attribute -eq 'Archive'){write-host $path+" is a file."$count_size = $count_size + $file_infos.Length;}# If it is a directory, the total size is calculated recursively.else{$count = $file_infos.Count;for ($i = 0; $i -lt $file_infos.Count; $i++) {$child = $file_infos[$i];[Path]::counter = [Path]::counter+1; # Each one is assigned an ID separately.$ID = [Path]::counter;# If it is a file, the size is accumulated.if([Path]::is_filename($child)){$count_size =  $count_size + $child.Length;}# If it is a directory, continue to recursively accumulate the size.else{$count_size =  $count_size + [Path]::get_size($child,$ID)}# Progress bar display$percent = $i / $count;  # Calculate the percentage by subentry.if($ParentId -eq -1){Write-Progress -ID $ID -Activity ("Total counts "+$count.ToString()+"bytes of path: "+$child.ToString()) -Status "$percent% completed." -PercentComplete $percent;}else{Write-Progress -ID $ID -ParentId $ParentId -Activity ("Total counts "+$count.ToString()+"bytes of path: "+$child.ToString()) -Status "$percent% completed." -PercentComplete $percent;}}}return $count_size}static [string]join($path, $childpath){<# Connect the directory and file name into a path.Params======- $path [string] The path value represented by a string.- $childpath [string] childpath path value represented by a string.#>return Join-Path -Path $path -ChildPath $childpath}static [string[]]split([string]$path){<#Divide the path into two parts: dirname and basename.Params======- path [string] The path value represented by a string.Returns=======- $dirname   The absolute path of the folder.- $basename  Subfile or subdirectory name#>$dirname = Split-Path -Path $path -Leaf;$basename = Split-Path -Path $path;return $dirname,$basename}static [string[]]get_enviroment(){return [Environment]::GetEnvironmentVariable('Path', 'Machine')}static [void]set_enviroment($path){[Environment]::SetEnvironmentVariable('Path', $path,'Machine')}static [void]set_enviroment($path,$env_var_target){if($env_var_target -eq 'Machine'){[Environment]::SetEnvironmentVariable('Path', $path,'Machine')}elseif($env_var_target -eq 'User'){[Environment]::SetEnvironmentVariable('Path', $path,'User')}else{write-host -ForegroundColor Red "ValueError: The value of variable  `"`$env_var_target`" can only be one of 'Machine' or 'User'." }   }static [void]delete([string]$path){<#Delete the file or directory corresponding to the specified path.Params======- path [string] The path value represented by a string.#>Remove-Item $path;}static [void]make($path){<#Create a series of folders according to the given path.Params======- path [string] The path value represented by a string.#>New-Item -Path ([Path]::dirname($path, $True)) -Name ([Path]::basename($path)) -ItemType "directory" }
}

【运维】PowerShell编程 目录文件相关方法的封装与案例详解相关推荐

  1. Java网络编程实现文件上传和下载案例详解

    客户端: 文件上传案例的客户端:读取本地文件,上传到服务器,读取服务器回写的数据 明确: 数据源:c:\1.jpg 目的地:服务器 实现步骤: 1.创建一个本地字节输入流FileInputStream ...

  2. 运维工程师必备之负载 均衡集群及LVS详解

    原文地址:运维工程师必备之负载 均衡集群及LVS详解作者:蚁巡运维平台 来源: chrinux 的BLOG 时间: 2013-07-01 14:00 此博文主要介绍集群和负载均衡的基本理论和类别,内容 ...

  3. linux modprobe自动加载,Linux运维知识之Linux modprobe自动处理可载入模块命令详解

    本文主要向大家介绍了Linux运维知识之Linux modprobe自动处理可载入模块命令详解,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. modprobe(module p ...

  4. linux sfdisk命令,Linux运维知识之Linux sfdisk硬盘分区工具程序命令详解

    本文主要向大家介绍了Linux运维知识之Linux sfdisk硬盘分区工具程序命令详解,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. 功能说明:硬盘分区工具程序. 语 法:s ...

  5. 运维工程师必备之负载均衡集群及LVS详解

    此博文主要介绍集群和负载均衡的基本理论和类别,内容看着比较枯燥.无味的,但是要想成为一个好的linux运维工程师,这些基本理论是必须理解透彻,才会在后来的系统调优和集群架构中得心应手,所以想成为lin ...

  6. FatFs 之一 R0.13c版源码目录文件、函数、全配置项详解及移植说明

      FatFs 是用于小型嵌入式系统的通用 FAT/exFAT 文件系统模块.FatFs 模块的编写符合 ANSI C(C89),并与磁盘 I/O 层完全分离,因此它独立于硬件平台. 它可以集成到资源 ...

  7. 90.网络安全渗透测试—[常规漏洞挖掘与利用篇6]—[文件包含-PHP封装伪协议详解实战示例]

    我认为,无论是学习安全还是从事安全的人,多多少少都有些许的情怀和使命感!!! 文章目录 一.文件包含-PHP封装伪协议简介 1.php内置封装协议 2.data://命令执行-伪协议 3.zip:// ...

  8. saltStack运维工具的部署及master迁移实现的过程详解

    服务器端:192.168.3.87 客户端:192.168.3.86.192.168.3.108 1.salt服务器端安装 192.168.3.87 rpm -Uvh http://mirrors.y ...

  9. python编程入门与案例详解-Pythony运维入门之Socket网络编程详解

    Socket是什么? Socket 是电脑网络中进程间数据流的端点Socket 是操作系统的通信机制应用程序通过Socket进行网络数据的传输 首先,简单了解一下TCP通信过程: TCP三次握手(面试 ...

最新文章

  1. 写给对 ”游戏开发” 感兴趣的朋友们
  2. C++变量命名规则和数据类型
  3. HTML5之placeholder属性以及如何更改placeholder属性中文字颜色大小位置
  4. numpy---(精简)
  5. swift 将图片保存到本地_Swift实现截屏并保存相册
  6. jdk1.8新特性的应用-Stream 的终止操作
  7. 三星s20计算机怎么添加到桌面,三星s20+怎么分屏?三星s20系列多窗口分屏设置操作步骤...
  8. L1-014. 简单题-PAT团体程序设计天梯赛GPLT
  9. 3.8 RIPv2的认证机制
  10. 读取.Properties配置文件
  11. python代码段_python代码段有哪些
  12. 国内android源码下载方法
  13. Qt优秀开源项目之十四:SortFilterProxyModel
  14. Python操作MyS QL
  15. 老人手机藏“吸费”陷阱
  16. 对VC 一些常见问题的整理
  17. 安全合规/GDPR--24--研究:GDPR合规体系设立与执行
  18. 中兴通讯联合SWAN Mobile打通斯洛伐克首个5G视频电话
  19. cacai安装与配置
  20. Firefox火狐无法启动出现弹窗--小黑日常超细解决教程

热门文章

  1. C语言中的逗号操作符
  2. 模块打包之CommonJS与ES6模块比较初探
  3. 关于max(X,Y),min(X,Y)
  4. 重装ubuntu系统后VS Code远程连接失败
  5. Gram-Schmidt正交化
  6. python : 正确复制列表的方法
  7. PHP 7天前的时间戳
  8. 博客园在我的博客添加点击小心心特效
  9. InvalidClassException
  10. python的collections模块的学习