I have a script which moves files with a specific extension from a source directory to a destination directory.
My source directory looks like:
FILESFOLDER
File1.td
File2.td
SUBFOLDER
File3.td
File4.td
My script is short and looks like:
if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
Write-Host "The source- or destination path is incorrect, please check "
break
}else
{
Write-Host "Success"
Copy-Item -path $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath -Recurse
}
I have to mention that the $SourceDirPath
comes from a config file and only works if I declare it as C:\FILESFOLDER\*
The script works but doesn't copy the files from the subfolder to destination. The destination only has File1 and File2.
What's wrong with my Copy-Item command?
So this will compare the directory source versus the destination and then copy over the content. Ignore my funky variables, I've amended them below on a edit
#Release folder
$Source = ""
#Local folder
$Destination = ""
#Find all the objects in the release
get-childitem $Source -Recurse | foreach {
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5 # Obtain hash
$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination #Escape the hash
Write-Host "comparing $SrcFile to $DestFile" -ForegroundColor Yellow
if (Test-Path $DestFile)
{
#Check the hash sum of the file copied
$DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
#compare them up
if ($SrcHash.hash -ne $DestHash.hash) {
Write-Warning "$SrcFile and $DestFile Files don't match!"
Write-Warning "Copying $SrcFile to $DestFile "
Copy-Item $SrcFile -Destination $DestFile -Force
}
else {
Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor
Green
}
}
else {
Write-host "$SrcFile is missing! Copying in" -ForegroundColor Red
Copy-Item $SrcFile -Destination $DestFile -Force
}
}