1
0 Comments

I am currently working on a script that performs the following tasks:

  1. Eliminates all folders contained within an uploaded folder.
  2. Transfers all the files to a different folder.
  3. Uses ImageMagick to convert all the files into a single .pdf document.
  4. Relocates the resulting .pdf to another folder and deletes all other files.

Initially, I managed to execute the script without any issues. However, I am now attempting to implement a feature that checks if the folder has finished copying before running the script. Unfortunately, I have encountered a problem whereby the script is executing successfully, but it is doing so on my desktop where the file is stored, rather than within the specified directories. All I am trying to do is ensure that the script waits until the folder has finished copying before proceeding with execution.

My current Code:

$freshStart = 0
$input = 'C:\IT\Convert Drop'
While ($freshStart -eq 0)
{
    $status = Get-Item $input | Foreach { $_.LastWriteTime }
    If ($status -eq $statusOld)
    {
        $input = 'C:\IT\Convert Drop'
        $output = 'C:\IT\Processing'
        $pdf = 'C:\IT\Processing\*.pdf'
        $done = 'C:\IT\Converted PDF'
        $deleteME = 'C:\IT\Convert Drop\*'
        $deleteMEToo = 'C:\IT\Processing\*'

        $folder = get-childitem -Path $input -Directory -Name

        $fileName = $folder + ".pdf"

        Get-ChildItem -Path $input -Recurse -File | Move-Item -Destination $output
        & CD $output
        & magick "*.{png,jpeg,jpg,tif}" $fileName
        Get-ChildItem -Path $pdf -File | Move-Item -Destination $done
        Remove-Item $deleteMEToo -Recurse -Force
        Remove-Item $deleteME -Recurse -Force
    }
    Else
    {
        sleep 10
        $statusOld = $status
    }

    $freshStart = 1
}

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "$input"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true

$status = Register-ObjectEvent $watcher "Created"
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action =
{
    $status = Get-Item $input | Foreach { $_.LastWriteTime }
    If ($status -eq $statusOld)
    {
        $input = 'C:\IT\Convert Drop'
        $output = 'C:\IT\Processing'
        $pdf = 'C:\IT\Processing\*.pdf'
        $done = 'C:\IT\Converted PDF'
        $deleteME = 'C:\IT\Convert Drop\*'
        $deleteMEToo = 'C:\IT\Processing\*'

        $folder = get-childitem -Path $input -Directory -Name

        $fileName = $folder + ".pdf"

        Get-ChildItem -Path $input -Recurse -File | Move-Item -Destination $output
        #& CD $output
        #Set-Location -Path '$output'
        & magick "*.{png,jpeg,jpg,tif}" $fileName
        Get-ChildItem -Path $pdf -File | Move-Item -Destination $done
        Remove-Item $deleteMEToo -Recurse -Force
        Remove-Item $deleteME -Recurse -Force
    }
    Else{       
        sleep 10
        $statusOld = $status
    }
}

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

EDITED CODE (Not working):

$freshStart = 0
$inLoc = 'C:\IT\Convert Drop'
$outLoc = 'C:\IT\Processing'
$pdf = 'C:\IT\Processing\*.pdf'
$done = 'C:\IT\Converted PDF'
$deleteME = 'C:\IT\Convert Drop\*'
$deleteMEToo = 'C:\IT\Processing\*'

While ($freshStart -eq 0)
{
    $status = Get-Item $inLoc | Foreach { $_.LastWriteTime }
    $statusOld = 0
    If ($status -eq $statusOld)
    {

        $folder = get-childitem -Path $inLoc -Directory -Name

        $fileName = $folder + ".pdf"

        Get-ChildItem -Path $inLoc -Recurse -File | Move-Item -Destination $outLoc
        & CD $outLoc
        & magick "*.{png,jpeg,jpg,tif}" $fileName
        Get-ChildItem -Path $pdf -File | Move-Item -Destination $done
        Remove-Item $deleteMEToo -Recurse -Force
        Remove-Item $deleteME -Recurse -Force
    }
    Else
    {
        $statusOld = $status
        $status = Get-Item $inLoc | Foreach { $_.LastWriteTime }
        sleep 10
    }

    $freshStart = 1
}

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "$inLoc"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action =
{
    $status = Get-Item $inLoc | Foreach { $_.LastWriteTime }
    $statusOld = 0
    If ($status -eq $statusOld)
    {

        $folder = get-childitem -Path $inLoc -Directory -Name

        $fileName = $folder + ".pdf"

        Get-ChildItem -Path $inLoc -Recurse -File | Move-Item -Destination $outLoc
        #& CD $outLoc
        #Set-Location -Path '$outLoc'
        & magick "*.{png,jpeg,jpg,tif}" $fileName
        Get-ChildItem -Path $pdf -File | Move-Item -Destination $done
        Remove-Item $deleteMEToo -Recurse -Force
        Remove-Item $deleteME -Recurse -Force
    }
    Else{
        $statusOld = $status
        $status = Get-Item $inLoc | Foreach { $_.LastWriteTime }
        sleep 10
    }
}

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

UPDATED SCRIPT

#File Locations
$rootPath = 'C:\IT\'
$inLoc = 'Convert Drop'
$prossLoc = 'Processing'
$outLoc = 'Converted PDF'

#File types to include in PDF creation.
$fileTypes = '*.{png,jpeg,jpg,tiff,tif}'

#Function Variables
$inPath  = Join-Path -Path "$rootPath" -ChildPath "$inLoc"
$outPath = Join-Path -Path "$rootPath" -ChildPath "$outLoc"
$runPath = Join-Path -Path "$rootPath" -ChildPath "$prossLoc"
$remove1 = Join-Path -Path "$rootPath" -ChildPath "$($inLoc + "\*")"
$remove2 = Join-Path -Path "$rootPath" -ChildPath "$($outLoc + "\*")"

#Folder Watching Variables
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "$inPath"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

#Lone Counter
$freshStart = $null
$statusOld  = $null
$pathLoc    = (Get-Item -Path ".\").FullName

#Pulls the last write time of a folder to compare later.
$grabStatus = {$status = Get-Item $pathLoc | Foreach { $_.LastWriteTime } }

#Get PDF name from Folder
$grabFileName = {
    $folder = get-childitem -Path $inPath -Directory -Name
    $fileName = $folder + ".pdf"
}

#Move all nested files to single folder.
$moveFiles = {
    Get-ChildItem -Path $inPath -Recurse -File | Move-Item -Destination $runPath
}

#Convert Nested files into single PDF
$makePDF = {
    & CD $runPath
    & magick "$fileTypes" $fileName
}

#Move final PDF
$moveCmplt = {
    Get-ChildItem -Path $pdf -File | Move-Item -Destination $outPath
}

#Delete Old files
$deleteOld = {
    Remove-Item $remove1 -Recurse -Force
    Remove-Item $remove2 -Recurse -Force
}

#Set compare status to current status then fetches new status.
$stats = {
    $statusOld = $status
    $grabStatus
    sleep 10
}

#Exicute main conversion together.
$action = {
    $grabStatus
    If ($status -eq $statusOld){
        $grabFileName
        $moveFiles
        & CD $runPath
        $grabStatus
        If ($status -eq $statusOld) {
            $makePDF
        }
        Else{
            $stats
        }
        $deleteOld
    }
    Else
    {
        $stats
    }
}

#First Time Start, Then Loop run.
While ($freshStart -eq $null) {
    If ((Get-ChildItem $inPath | Measure-Object).Count -eq 0) {
    }
    Else {
        $action
    }
    $freshStart = "FreshStartDone!"
}

#Scan folder every 5 seconds for new content then run convert on change.
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}
Askify Moderator Edited question May 1, 2023