PowerShell: Mix files
Hello, %habrauser%!
I recently planted quite simple, at first glance the task: it is Necessary to mix about 1000 photos for the voting system.
Manually doing this is a thankless task. Need some cute script.
PowerShell was the ideal option to put something third-party like python does not like, and with .bat files I have in my life and not friends..
Perhaps the start.
The names of the pictures were important and they wanted to keep. I decided to remove them in the file properties (the"Summary" for WinXp, "in Detail" for Win7). Moreover, voting is written on sharepoint, and he is able to pick up "Name" from the properties file. It was very convenient.
To edit the properties file was used component OLE File Property Reader 2.1 (In the samples folder vb7 is a wrapper library for .net), the benefit has already been installed on the machine. The component is designed for office documents, but for images came up.
The actual script:
Then move on to mixing:
It was possible to generate numeric names, but suddenly very much wanted to reinvent the wheel complex designs.
the
Thank you for your attention, I hope will be useful to someone.
Article based on information from habrahabr.ru
I recently planted quite simple, at first glance the task: it is Necessary to mix about 1000 photos for the voting system.
Manually doing this is a thankless task. Need some cute script.
PowerShell was the ideal option to put something third-party like python does not like, and with .bat files I have in my life and not friends..
Perhaps the start.
The names of the pictures were important and they wanted to keep. I decided to remove them in the file properties (the"Summary" for WinXp, "in Detail" for Win7). Moreover, voting is written on sharepoint, and he is able to pick up "Name" from the properties file. It was very convenient.
To edit the properties file was used component OLE File Property Reader 2.1 (In the samples folder vb7 is a wrapper library for .net), the benefit has already been installed on the machine. The component is designed for office documents, but for images came up.
The actual script:
[System.Reflection.Assembly]::LoadFrom('c:\DsoFile\Source\Vb7Demo\bin\Interop.DSOFile.dll')
SetSummary function-Name([string] $folder, [string]$pattern = '*.*')
{
$files = [System.IO.Directory]::GetFiles($folder, $pattern)
foreach ($f in $files)
{
$oled = New-Object -TypeName DSOFile.OleDocumentPropertiesClass
$oled.Open($f, $false, [DSOFile.dsoFileOpenOptions]::dsoOptionDefault)
$sp = $oled.SummaryProperties
$sp.Title = $f.Substring($f.lastindexof('\') + 1, $f.lastindexof('.') - $f.lastindexof('\') - 1)
$oled.close($true)
}
}
SetSummary-Name-folder 'c:\photo'
Then move on to mixing:
It was possible to generate numeric names, but suddenly very much wanted to reinvent the wheel complex designs.
the
# Replaces the file name to a random set of letters
function RandomName([string]$path)
{
$length = 20 # long new name
$sb = New-Object -TypeName System.Text.StringBuilder
for($i=0; $i -lt $length; $i++)
{
# Then I let go, the bike failed...
[char]$ch = [Convert]::ToChar([Convert]::ToInt32([Math]::Floor(26 * $rand.NextDouble() + 65)))
$tmp = $sb.Append($ch) # Append returns useful information, throw it in $tmp
}
$name = $path.Substring($path.lastindexof('\') + 1, $path.lastindexof('.') - $path.lastindexof('\') - 1)
$sb = $sb.ToString().ToLower()
return $path.Replace($name, $sb)
}
function MixFiles([string]$From, [string]$pattern = '*.*')
{
$files = [System.IO.Directory]::GetFiles($from, $pattern)
foreach($f in $files)
{
$path = RandomName -path $f
# Just in case check
while([System.IO.File]::Exists($path))
{
# To know if that happens
$path
$path = RandomName -path $f
}
# Beauty will show old and new names
Write-Host ($f + '-> ' + $path)
[System.IO.File]::Move($f, $path)
}
}
$rand = New-Object -TypeName Random
MixFiles -from 'c:\test\' -pattern '*.jpg'
Thank you for your attention, I hope will be useful to someone.
Комментарии
Отправить комментарий