PowerTip of the Day from powershell.com上周汇总(四)
来源:互联网 发布:网络上夯是什么意思 编辑:程序博客网 时间:2024/04/30 22:32
读取“神奇密码”
Reading File "Magic Number"
http://powershell.com/cs/blogs/tips/archive/2010/08/09/reading-file-quot-magic-number-quot.aspx
文件类型并不是完全依靠文件的扩展名的,二进制文件有一个内部ID号叫做“神奇密码”(Magic numbers),windows通过这个可以知道这个文件时什么类型。下面的函数用来显示这个“神奇密码”:
function Get-MagicNumber ($path)
{ Resolve-Path $path | Foreach-Object {
$magicnumber = Get-Content -encoding byte $_ -read 4 -total 4
$hex1 = ("{0:x}" -f ($magicnumber[0] * 256 + $magicnumber[1])).PadLeft(4, "0")
$hex2 = ("{0:x}" -f ($magicnumber[2] * 256 + $magicnumber[3])).PadLeft(4, "0")
[string] $chars = $magicnumber| %{ if ([char]::IsLetterOrDigit($_))
{ [char] $_ } else { "." }}
"{0} {1} '{2}'" -f $hex1, $hex2, $chars }
}
显示16进制文件
Displaying Hex Dumps
http://powershell.com/cs/blogs/tips/archive/2010/08/10/displaying-hex-dumps.aspx
powershell不仅可以显示纯文本文件,同时也可以读取二进制文件。下面的方法用来读取一个16机制文件。
function Get-HexDump($path,$width=10, $bytes=-1)
{
$OFS=""
Get-Content -Encoding byte $path -ReadCount $width `
-totalcount $bytes | Foreach-Object {
$byte = $_
if (($byte -eq 0).count -ne $width)
{
$hex = $byte | Foreach-Object {
" " + ("{0:x}" -f $_).PadLeft(2,"0")}
$char = $byte | Foreach-Object {
if ([char]::IsLetterOrDigit($_))
{ [char] $_ } else { "." }}
"$hex $char"
}
}
}
下面是如何调用这个方法:
get-hexdump $env:windir/explorer.exe -width 15 -bytes 150
删除最近浏览文件夹
Remove Recents Folder
http://powershell.com/cs/blogs/tips/archive/2010/08/12/remove-recents-folder.aspx
windows用一个专门的文件夹保存用户最近打开的文件,通过下面这个命令可以查看这个文件夹。
Dir ([Environment]::GetFolderPath("Recent"))
通过下列命令就可以删除这些文件:
Del "$([Environment]::GetFolderPath("Recent"))/*.*" –whatif
批量更改文件扩展名
Bulk-Changing File Extensions
http://powershell.com/cs/blogs/tips/archive/2010/08/11/bulk-changing-file-extensions.aspx
只需要下面一行命令就可以更改文件扩展名,主要是通过rename:
Dir $home/ *.ps1 -recurse | Foreach-Object { Rename-Item $_.FullName ([System.IO.Path]::GetFileNameWithoutExtension($_.FullName) + ".old.ps1") -whatif }
-recurse枚举子文件夹。
显示IE的Cookies
Listing Internet Explorer Cookies
http://powershell.com/cs/blogs/tips/archive/2010/08/13/listing-internet-explorer-cookies.aspx
显示Cookies所在的文件夹:
Dir ([Environment]::GetFolderPath("Cookies"))
在资源管理器中显示:
Explorer ([Environment]::GetFolderPath("Cookies"))
显示内容包含sc的cookie文件:
Dir ([Environment]::GetFolderPath("Cookies")) | Select-String sc -List
以上来自powershell.com
2010年八月份9日到13日的PowerTip of the Day
---------------------------------------------------------------
来自博客园的aspnetx
- PowerTip of the Day from powershell.com上周汇总(四)
- PowerTip of the Day from powershell.com上周汇总
- PowerTip of the Day from powershell.com上周汇总(二)
- PowerTip of the Day from powershell.com上周汇总(三)
- PowerTip of the Day from powershell.com上周汇总(五)
- PowerTip of the Day from powershell.com上周汇总(六)
- PowerTip of the Day from powershell.com上周汇总(七)
- PowerTip of the Day from powershell.com上周汇总(八)
- PowerTip of the Day from powershell.com上周汇总(九)
- PowerTip of the Day from powershell.com上周汇总(十)
- PowerTip of the Day from powershell.com上周汇总(十一)
- Tip of the Day(for AS)
- The day of 918
- The day of beginning
- http://www.georgerrmartin.com/excerpt-from-the-winds-of-winter/
- The last day of October
- the first day of march
- The last day of 5.1
- Python入门6
- PowerTip of the Day from powershell.com上周汇总(三)
- 开源微博statusNET折腾——国际化(续)
- Linux操作系统内核启动参数详细解析
- 数学建模98的题,找高手指点
- PowerTip of the Day from powershell.com上周汇总(四)
- 数据库中视图的作用
- 委托
- SAP PS 项目模块 PA学习笔记——PLM200
- 数学建模98的题,找高手指点
- 说说标准--CSS核心可视化格式模型(visual formatting model)之八:float 特性以及浮动(Float)定位细则一
- 数学建模98的题,找高手指点
- Oracle数据库视图管理经验技巧
- Silverlight4实现三维企业网站