SomaFM commercial free internet radio All posts tagged 'scripting'

Paul Sturm

My notebook of discovered facts

Sorting photos via powershell v1

I have a drive full of photos - over 300GB - that are sorted into folders for each location photographed. The folder name is a location code of sorts.  While NTFS can have billions of files in a folder, explorer will seize up for minutes as it retrieves data for this drive.

I needed a way to sort my folders into subfolders, just to speed up browsing the drive.  If a location code was DJKSASWW, I wanted that sorted into a folder name DJKS.  This would group much smaller sets of data and still allow for an intelligent, browseable structure if human intervention was necessary.

Powershell came thorough for me.

# source location of folder to copy/move
$source = "E:\IMAGEVAULT\"

# where we want these to wind up
$destination = "R:\imagevault\"

# get our objects (this is filtered for just those that start
# with the letter 'a' right now
$folders = Get-ChildItem -filter A* $source

# loop each folder object from our source
foreach ($folder in $folders){
    # the this specific folder name
    $foo = $folder.name
    # new folder name is going to be the first 4 characters
    $newfolder = [char]$foo[0]+[char]$foo[1]+[char]$foo[2]+[char]$foo[3]
    # set the path to this new destination folder
    $newDest = $destination + $newfolder

    # test to see if this path exists
    if(Test-Path $newDest)
        {   # it does so we will show what we're copying/moving
            $folder.FullName + " to " + $newDest
            # and perform the task
            Copy-Item $folder.FullName $newDest -recurse
        } else {
            # create path since it didn't exist
            New-Item $newDest -Type Directory
            # show what we're copying/moving
            $folder.FullName + " to " + $newDest
            # and perform the task
            Copy-Item $folder.FullName $newDest -recurse
        }
}

This allows me to set a source and destination and the script (while time consuming) will fix the entire structure.

All you'd have to do is change Copy-Item to Move-Item if you didn't want a 2nd set.


Categories: powershell | scripting
Permalink | Comments (21) | Post RSSRSS comment feed

Robocopy exit codes

When task manager runs a robocopy command for me, I get different error codes.  They are not all really errors but status codes would be more accurate.  Here is a list from the README:

Hex Bit Value Decimal Value Meaning If Set

  • 0×10 16 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.
  • 0×08 8 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
  • 0×04 4 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
  • 0×02 2 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
  • 0×01 1 One or more files were copied successfully (that is, new files have arrived).
  • 0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

These are bits so 0×03, for example, would be both 'files were copied (0×01)' and 'extra file/directories were detected (0×02)'


Tags:
Categories: scripting
Permalink | Comments (76) | Post RSSRSS comment feed

Robocopy exit codes

When task manager runs a robocopy command for me, I get different error codes.  They are not all really errors but status codes would be more accurate.  Here is a list from the README:

Hex Bit Value Decimal Value Meaning If Set

  • 0×10 16 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.
  • 0×08 8 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.
  • 0×04 4 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.
  • 0×02 2 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.
  • 0×01 1 One or more files were copied successfully (that is, new files have arrived).
  • 0×00 0 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

These are bits so 0×03, for example, would be both 'files were copied (0×01)' and 'extra file/directories were detected (0×02)'


Tags:
Categories: scripting
Permalink | Comments (397) | Post RSSRSS comment feed

Remove files based on age in days and email success message in Powershell

I have to cleanup a server share daily.  The following powershell script will remove old files and email a note to me telling me it ran.

#
# setup variables 
#
$ageInDays = 7
$fileShare = "\\server\share\subfolder"
#
# cleanout $fileShare and leave
#
$ageInDays worth of recent files
#
Get-ChildItem $fileShare -Recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(-$ageInDays)} | Remove-Item -recurse
#
# setup to mail the results 
#
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "smtp.domain.com"$SmtpClient.host = $SmtpServer 
$From = new-object System.Net.Mail.MailAddress("scheduledtask@domain.com", "Schedule Task")
$To = "me@domain.com"
$Title = "Snapshot cleanup run"
$Body = "The powershell snapshot cleanup of $fileShare successfully ran.
   This left $ageInDays days of snapshots. Script location is '\\server\share\cleanup_snapshots.ps1'" 
#
# mail the results 
#
$SmtpClient.Send($from,$to,$title,$Body) 

I need to get it to mail on failure, but I've not gotten that far.


Permalink | Comments (8) | Post RSSRSS comment feed