patternshellMinor
Script which migrates files to secondary storage and symlinks them
Viewed 0 times
scriptsymlinksmigratesandstoragesecondaryfileswhichthem
Problem
I wrote this code to clean up some of the space on our file server. We've got 15 years of legacy data that nobody accesses or changes or cares about that we have to keep regardless. I'd rather have it not sitting on our main file server so that I don't have to add an additional TB to it annually. This script goes through the file structure, finds files that haven't been touched in 4 years and copies them to slower storage, then replaces the source file with a symlink.
I'm new to PowerShell, so any pointers regarding style or a better way to do things would be nice. I know that my way of bracing things is wrong and that closing curly braces should be indented one less level, but I don't care and find this easier to deal with.
```
param(
[string]$Dir = "",
[string]$ArchiveDrive = ""
)
if ($ArchiveDrive -eq ""){
$hostname = hostname
$ArchiveDrive = "\\Archives\"+$hostname+"\"+$Dir[0]+"\"
}
import-module PSCX
import-module new-symlink
$FileList = @()
$SourceDrive = $dir[0] + ":\"
$date = Get-Date -Format yyyy-MM-dd
$ErrLog = "C:\ErrorLog $date.txt"
$DelLog = "C:\DelLog $date.txt"
$PathWarning = "C:\_PROBLEMS DETECTED.txt"
function BuildLists($dir){
$FileList = @()
$DirList = (dir $dir -recurse)
foreach ($item in $DirList){
if ( ((get-date).Subtract($item.LastWriteTime).Days-gt 1460) -eq $True) {
$FileList += $item
}
#else {write-host "$item is modified recently"}
}
return $FileList
}
function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName}
}
function ArchiveFile($SourceFile){
$DestFile = ($SourceFile.fullname.replace($SourceDrive, $ArchiveDrive))
$DestDir = ($SourceFile.DirectoryName.replace($SourceDrive, $ArchiveDrive))
mkdir -Path $DestDir 2>$ErrLog
copy $SourceFile.FullName $DestFile
}
function HashCheckFile($SourceFile){
$DestFile = $SourceFile.FullName.replace($SourceDri
I'm new to PowerShell, so any pointers regarding style or a better way to do things would be nice. I know that my way of bracing things is wrong and that closing curly braces should be indented one less level, but I don't care and find this easier to deal with.
```
param(
[string]$Dir = "",
[string]$ArchiveDrive = ""
)
if ($ArchiveDrive -eq ""){
$hostname = hostname
$ArchiveDrive = "\\Archives\"+$hostname+"\"+$Dir[0]+"\"
}
import-module PSCX
import-module new-symlink
$FileList = @()
$SourceDrive = $dir[0] + ":\"
$date = Get-Date -Format yyyy-MM-dd
$ErrLog = "C:\ErrorLog $date.txt"
$DelLog = "C:\DelLog $date.txt"
$PathWarning = "C:\_PROBLEMS DETECTED.txt"
function BuildLists($dir){
$FileList = @()
$DirList = (dir $dir -recurse)
foreach ($item in $DirList){
if ( ((get-date).Subtract($item.LastWriteTime).Days-gt 1460) -eq $True) {
$FileList += $item
}
#else {write-host "$item is modified recently"}
}
return $FileList
}
function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName}
}
function ArchiveFile($SourceFile){
$DestFile = ($SourceFile.fullname.replace($SourceDrive, $ArchiveDrive))
$DestDir = ($SourceFile.DirectoryName.replace($SourceDrive, $ArchiveDrive))
mkdir -Path $DestDir 2>$ErrLog
copy $SourceFile.FullName $DestFile
}
function HashCheckFile($SourceFile){
$DestFile = $SourceFile.FullName.replace($SourceDri
Solution
Not bad at all. Nice to see functions but perhaps you have too many.
Do you really need this as a function?
This function is listed twice but is only called once. It could have probably stayed in the Archive function.
Do you really need this as a function?
function DeleteFIle($File){
del $file.fullname
}This function is listed twice but is only called once. It could have probably stayed in the Archive function.
function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName
}
}Code Snippets
function DeleteFIle($File){
del $file.fullname
}function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName
}
}Context
StackExchange Code Review Q#41113, answer score: 2
Revisions (0)
No revisions yet.