More Powershell Scripts

More Powershell Scripts

So I’ve been working on a couple of scripts lately to basically go through log files. See at work we have log files pretty much for everything our Point of Sale system does, and while I’m sure at some point we will delete them for now the owner wants them all kept. So I’m using these two scripts basically. I’m sure they can be combined into one script, but I’m not that good at powershell yet, and while I’ve had time to tweak and test these two scripts I really don’t have the time to combine them. So for now they’re separate, and maybe at one point I’ll combine them.

First one I’ll put up I a sort script. Basically what this does it takes an input and an output destination. Then it will take the files in the input and move them to the output directory and will put them in folders based on the year and month they were created.

#This script is designed to take files in a folder and then sort them and put them into folders based on their creation date.


$srcFolder = "C:\input"
$targetFolder = "C:\output"

$files = Get-ChildItem -Path $srcFolder -include *.* –Recurse 

foreach ($file in $files){
	try{
		$path = $file.FullName
		$shell = New-Object -COMObject Shell.Application
		$folder = Split-Path $path
		$file1 = Split-Path $path -Leaf
		$shellfolder = $shell.Namespace($folder)
		$shellfile = $shellfolder.ParseName($file1)
		
		#0..287 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_) }
		#32 CameraMaker,#12 DateTaken,#30 CameraModel
		
		$dateTaken = $shellfolder.GetDetailsOf($shellfile, 12)
			
		if([string]::IsNullOrWhiteSpace($dateTaken)) {    
			$parseDate =[datetime]$file.CreationTime  
		} 	
		else{
			#http://stackoverflow.com/questions/25474023/file-date-metadata-not-displaying-properly
			$dateTaken = ($dateTaken -replace [char]8206) -replace [char]8207
			$parseDate =[datetime]::ParseExact($dateTaken,"g",$null)
		}
		
		$year = $parseDate.Year	
		$monthNr = "{0:MM}" -f $parseDate
		$month = "{0:MMMM}" -f $parseDate		
		
		$fileName = "{0:yyyyMMdd}" -f $parseDate
		$fileExtension = $file.Extension
	
		
		$directory = $targetFolder + "\" + $year + "\" + "$monthNr"
		if (!(Test-Path $Directory))
		{
			New-Item $directory -type directory | Out-Null
		}
		
		$newFileName = "$fileName$fileExtension"
		$targetFile = "$directory\$newFileName"
		
		Move-Item $file.FullName -Destination $targetFile
	}
	catch{
		Write-Host "Could not copy file $file"
	}
}

The next script is designed to zip all folders in a directory individually.


#This script zips up any folders found in a particular directory and then removes the original
Add-Type -AssemblyName System.IO.Compression.FileSystem
 
$directory = "C:\folder to zip\"
 
$folders = Get-ChildItem -Path $directory | Where-Object {$_.PSIsContainer} | Select-Object -ExpandProperty FullName
 
foreach ($folder in $folders) {
    $archive = $folder + '.zip'
    [System.IO.Compression.ZipFile]::CreateFromDirectory($folder, $archive, 'Optimal', $True)
}
Remove-Item $folders -Recurse -Confirm:$false

Like I said at some point I may combine these two, but for now they are the way they are.

Leave a Reply

Your email address will not be published. Required fields are marked *