考虑以下目录结构:
rootdir
subdir_1
12345.tif
56789.tif
subdir_2
00000.tif
...
subdir_n
99999.tif
54321.tif
54345.tif
我需要:
因此,所需的输出将是:
newdir
2070e4cfb8f24209647d3c9ec55098ee.tif
52e31fe630cebe73cc959d371bd6b353.tif
eec032f144f5af1be2b7f0535a2010d2.tif
936ed95431293660e3499d88e5ae22b0.tif
8bc884dce30d89b53b120adb8bd658b2.tif
ec87470ae6683b539ea69004894e23dd.tif
我现在拥有了(在PS 2.0 bc中,我必须使用它):
Get-ChildItem "D:\rootdir" -filter *.tif |
Foreach-Object {
$content = Get-Content $_.FullName
$md5 = New-Object -TypeName
System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($content)))
## Write-Host $hash = CB-A9-EB-00-92-18-06-71-D9-DD-7C-2A-08-D9-D9-EF
Set-Content $_.FullName('newroot/'+ $hash +'.tif')
}
这里的第一个问题是MD5-
介于两者之间。那么,我在这里做错了什么?
第二,为什么新文件未写入文件系统?
任何帮助将不胜感激!
首先,这些破折号是的结果[System.BitConverter]::ToString()
。您可以根据需要删除它们。其次,如果要复制文件,则应调用Copy-Item
而不是Set-Content
。像这样:
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
Get-ChildItem "D:\rootdir" -filter *.tif -Recurse |
Foreach-Object {
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($_.fullname)))
$hash = $hash -replace "-","" # drop all dashes off $hash
Copy-Item -Path $_.fullname -Destination "newroot/$hash.tif"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句