Hey guys,
I’m trying to move pdf’s that are below or above a certain size to their appropriate folders. Here’s what I have so far, for some reason runs and deletes the source file, but doens’t move it and I don’t see any errors.
#----File Size---#
$Filesize=4KB
#----Define folders to use----#
$SourceFolder = "source folder path"
$AutoFolder = "good folder"
$TooSmallFolder = "toosmall folder"
#----Define extension----#
$Extension = "*.pdf*"
#----Get files that are too small and move them----#
$Files = Get-Childitem $SourceFolder -Include $Extension -Recurse | Where { $_.Length -lt $Filesize}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Moving File $File" -ForegroundColor "Red"
Move-Item -Destination '$TooSmallFolder''$File' -force
}
else
{
Write-Host "No more small files to move!" -foregroundcolor "Green"
}
}
#----Get files that are good and move them----#
$Files = Get-Childitem $SourceFolder -Include $Extension -Recurse | Where { $_.Length -gt $Filesize}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Moving File $File" -ForegroundColor "Red"
Move-Item -Destination '$AutoFolder''$File' -force
}
else
{
Write-Host "No more good files to move!" -foregroundcolor "Green"
}
}
Of course the desination folders should exist already before you run this code.
BTW: When you post code please format it as code using the preformatted text button ( </> ) . Simply place the cursor on an empty line, click the button and paste your ocde.
Any idea why it’s not outputting the file name to my log?
ForEach-Object{
if ($_.Length -lt $Filesize) {
Move-Item -Path $_.FullName -Destination $TooSmallFolder
logit -----'File is too small - moved to TooSmall Folder'
logit $File
}
Else{
Move-Item -Path $_.FullName -Destination $AutoFolder
logit -----'File is good - moved to Auto Folder'
logit $File
}
Right. The complete object would be $_ … if you only want the Name of the file it would be $_.Name, only the the base name would be $_.BaseName, only the extension would be $_.Extension only the folder would be $_.Directory and so on …
Hmmm … I don’t know what exactly you mean with that. The pipeline variable is an automatic variable and is always either $_ or $PSItem. These are reserved names you cannot change them.