ps第4课:玩转文件目录
powershell吧
全部回复
仅看楼主
level 7
pck6636 楼主
文件,目录对象介绍:
第一个要学的命令就是dir,powershell没有find。
问:为什么要用powershell的dir【即Get-ChildItem】,而不用cmd的dir?
答:
get-childitem d:\xxx -file #过滤,只输出文件
-Directory #过滤,只输出目录
-Hidden #过滤,只输出隐藏
-Recurse #包含子目录
-Depth #目录深度
问:文件的常用属性是什么?
答:
$文件 = dir a:\pscode\temp183\aaa.txt
$文件.FullName #全路径属性
$文件.name #文件名和扩展名
.BaseName #文件名
.Extension #扩展名
.LastWriteTime #返回最后写入时间属性
.Length #文件字节长度
.DirectoryName #父目录
问:如何测试文件,目录是否存在?
答:
test-path d:\xxx\yyy.txt
test-path /xxx/yyy.txt
test-path /xxx/yyy -pathtype Container #测试是否有此目录
test-path /xxx/yyy -pathtype Leaf #测试是否有此文件
问:如何拆分目录,文件?
答:
Split-Path -Path "C:\Test\Logs\*.log" -Leaf -Resolve #返回所有文件名即
Pass1.log
Pass2.logSplit-Path -Path "C:\Test\Logs\*.log"
返回:C:\Test\Logs\
问:如何合并目录,文件?
答:
1 join-path
2 直接用构造字符串的方法:
$目录名= '/root'
$目录名加文件名 = "$目录名/abc/def.txt"
#返回
/root/abc/def.txt
问:神马是文件,目录的-LiteralPath?
答:
不包含正则,通配符的。不会被正则转义的路径。
test-path -Path e:\电影\[神秘博士][第十季]第6集_bd.mp4 #返回假,因为有[]
test-path -LiteralPath e:\电影\[神秘博士][第十季]第6集_bd.mp4 #返回真
2018年10月19日 02点10分 1
level 7
pck6636 楼主
名词解释, item:
可以是文件,目录,注册表,数据库,环境变量等。
其他处理item的命令:
PS A:\pscode> get-command *-item*CommandType Name Version Source
----------- ---- ------- ------
清空 Clear-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Clear-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
复制 Copy-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Copy-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
获取 Get-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Get-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Get-ItemPropertyValue 3.1.0.0 Microsoft.PowerShell.Management
执行 Invoke-Item 3.1.0.0 Microsoft.PowerShell.Management
移动 Move-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Move-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
新建 New-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet New-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
删除 Remove-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Remove-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
改名 Rename-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Rename-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
设置 Set-Item 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Set-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management
2018年11月01日 04点11分 2
level 7
pck6636 楼主
问:目录的常用属性是什么?
答:
$目录 = dir a:\pscode\TEMP_2019\temp150 -Recurse
.count
#含有文件的数量。比如这个目录中含有一个文件a.txt,还含有一个目录bbb,
那么.count值为2。
.length
#比较诡异。
目录内若无子目录,返回所有文件长度的总和。
若有子目录。则返回值和.count 相同。
问:给出一个目录,如何得到它的父目录?
答:
Split-Path a:\pscode\TEMP_2019\temp150
返回:
a:\pscode\TEMP_2019
2019年04月18日 05点04分 5
level 3
写的不错
2019年04月28日 12点04分 6
level 13
辛苦了[真棒]
2019年06月10日 23点06分 7
1