SOLVED

Watch Source Folder and Create and Move to Destination Folder

%3CLINGO-SUB%20id%3D%22lingo-sub-3101276%22%20slang%3D%22en-US%22%3EWatch%20Source%20Folder%20and%20Create%20and%20Move%20to%20Destination%20Folder%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3101276%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20There%2C%3C%2FP%3E%3CP%3ECan%20someone%20assist%20with%20my%20code%20below.%20It%20wont%20create%20the%20sub%20directory%20and%20move%20the%20.zip%20file%20to%20the%20destination%20folder.%20I%20need%20the%20script%20to%20watch%20for%20new%20.zip%20files%2C%20create%20a%20subfolder%20with%20the%20day%20number%20and%20move%20the%20.zip%20file%20to%20this%20new%20folder.%20Code%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24ZipFileSearchFolder%20%3D%20%22C%3A%5CTESTsearch%22%3C%2FP%3E%3CP%3E%24DestionationFolder%20%3D%20%22C%3A%5CTESTdestination%5C2022%22%3C%2FP%3E%3CP%3E%24Filter%20%3D%20%22*.zip%22%3C%2FP%3E%3CP%3Eif(-not%20(Test-Path%20%24DestionationFolder))%3CBR%20%2F%3E%7B%3CBR%20%2F%3ENew-Item%20-Path%20%24DestionationFolder%20-ItemType%20Directory%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%24Watcher%20%3D%20New-Object%20IO.FileSystemWatcher%20%24ZipFileSearchFolder%2C%20%24Filter%20-Property%20%40%7B%3CBR%20%2F%3EIncludeSubdirectories%20%3D%20%24true%3CBR%20%2F%3ENotifyFilter%20%3D%20%5BIO.NotifyFilters%5D'FileName%2C%20LastWrite'%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24OnCreated%20%3D%20Register-ObjectEvent%20%24Watcher%20Created%20-SourceIdentifier%20FileCreated%20-Action%20%7B%3CBR%20%2F%3E%24DayOfYear%20%3D%20(Get-Date).DayOfYear%3CBR%20%2F%3E%24SourcePath%20%3D%20%24Event.SourceEventArgs.FullPath%3CBR%20%2F%3E%24SourceName%20%3D%20%24Event.SourceEventArgs.Name%3CBR%20%2F%3E%24File%20%3D%20Get-Item%20%24SourcePath%3CBR%20%2F%3E%24NewName%20%3D%20%24File.BaseName%2B%22-%22%2B%24DayOfYear%2B%24File.Extension%3CBR%20%2F%3EMove-Item%20-Path%20%24SourcePath%20-Destination%20%24DestionationFolder%5C%24NewName%20-Force%20-Verbose%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3101276%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3103445%22%20slang%3D%22en-US%22%3ERe%3A%20Watch%20Source%20Folder%20and%20Create%20and%20Move%20to%20Destination%20Folder%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3103445%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F665488%22%20target%3D%22_blank%22%3E%40DuaneH%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20looks%20like%20hte%20problem%20might%20be%20in%20the%20new%20file%20name%20value%20-%20I%20think%20you%20are%20missing%20the%20.%20between%20name%20and%20extension.%20When%20I%20run%20the%20code%20with%20this%20syntax%20it%20works%20as%20expected.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24NewName%20%3D%20%22%7B0%7D-%7B1%7D.%7B2%7D%22%20-f%20(%24File.BaseName)%2C%20%24DayOfYear%2C%20(%24File.Extension)%0A%0AMove-Item%20-Path%20%24SourcePath%20-Destination%20(join-path%20%24DestinationFolder%20%24NewName)%20-Force%20-Verbose%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EI%20have%20also%20used%20Join-Path%20to%20create%20the%20-Destination%20value%20just%20to%20be%20sure%20it%20is%20a%20valid%20string%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3103492%22%20slang%3D%22en-US%22%3ERe%3A%20Watch%20Source%20Folder%20and%20Create%20and%20Move%20to%20Destination%20Folder%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3103492%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F665488%22%20target%3D%22_blank%22%3E%40DuaneH%3C%2FA%3E%26nbsp%3BIt%20looks%20like%20there%20is%20a%20problem%20with%20the%20Destination%20path%20construction%2C%20try%20this%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24NewName%20%3D%20%22%7B0%7D-%7B1%7D%7B2%7D%22%20-f%20(%24File.BaseName)%2C%20%24DayOfYear%2C%20(%24File.Extension)%0A%0AMove-Item%20-Path%20%24SourcePath%20-Destination%20(join-path%20%24DestionationFolder%20%24NewName)%20-Force%20-Verbose%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20is%20working%20for%20me.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi There,

Can someone assist with my code below. It wont create the sub directory and move the .zip file to the destination folder. I need the script to watch for new .zip files, create a subfolder with the day number and move the .zip file to this new folder. Code below:

 

$ZipFileSearchFolder = "C:\TESTsearch"

$DestionationFolder = "C:\TESTdestination\2022"

$Filter = "*.zip"

if(-not (Test-Path $DestionationFolder))
{
New-Item -Path $DestionationFolder -ItemType Directory
}

$Watcher = New-Object IO.FileSystemWatcher $ZipFileSearchFolder, $Filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$OnCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
$DayOfYear = (Get-Date).DayOfYear
$SourcePath = $Event.SourceEventArgs.FullPath
$SourceName = $Event.SourceEventArgs.Name
$File = Get-Item $SourcePath
$NewName = $File.BaseName+"-"+$DayOfYear+$File.Extension
Move-Item -Path $SourcePath -Destination $DestionationFolder\$NewName -Force -Verbose
}

 

Thanks

 

3 Replies

@DuaneH It looks like there is a problem with the Destination path construction, try this

$NewName = "{0}-{1}{2}" -f ($File.BaseName), $DayOfYear, ($File.Extension)

Move-Item -Path $SourcePath -Destination (join-path $DestionationFolder $NewName) -Force -Verbose

This is working for me.

best response confirmed by DuaneH (New Contributor)
Solution

@DuaneH 

 

Changed it a little to this:

 

 

$ZipFileSearchFolder = "C:\TESTsearch"

$DestionationFolder = "C:\TESTdestination\2022"

$Filter = "*.zip"

if(-not (Test-Path $DestionationFolder))
{
New-Item -Path $DestionationFolder -ItemType Directory
}

$Watcher = New-Object IO.FileSystemWatcher $ZipFileSearchFolder, $Filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$OnCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
$DayOfYear = (Get-Date).DayOfYear
$SourcePath = $Event.SourceEventArgs.FullPath
$SourceName = $Event.SourceEventArgs.Name
$File = Get-Item $SourcePath
if (-not (Test-Path "$($DestionationFolder)\$($DayOfYear)")) {
New-Item -Path "$($DestionationFolder)\$($DayOfYear)" -ItemType Directory
}
$NewName = $File.BaseName+"-"+$DayOfYear+$File.Extension
Move-Item -Path $SourcePath -Destination "$($DestionationFolder)\$($DayOfYear)" -Force -Verbose
}

 

But @Jonathan_Allen has a nice looking solution as well ;) 

 

@Harm_Veenstra  thanks so much that has fixed it, Cheers!