Forum Discussion

DuaneH's avatar
DuaneH
Copper Contributor
Feb 01, 2022
Solved

Watch Source Folder and Create and Move to Destination Folder

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

 

  • Harm_Veenstra's avatar
    Harm_Veenstra
    Feb 02, 2022

    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 😉 

     

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.

    • Harm_Veenstra's avatar
      Harm_Veenstra
      MVP

      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 😉 

       

Resources