1
0 Comments

I had a script that was functioning properly before I began breaking it into smaller parts. However, I’ve noticed that as I try to use variables, the information is not being brought in. I have to manually copy and paste the variable into my “if”, “else”, “while”, or other functions for them to work. I was advised to use “$script:”, but from what I understand, that only allows the variable to be “reported out” of the function for later use. I don’t need that, I just need to pull the information in.

My script so far:

  • A user drops a folder into a designated drop-off folder.
  • The script detects a new file and starts running.
  • The name of the folder is recorded.
  • All files are moved from their original folder structure to another folder.
  • These files are then converted into a single .pdf.
  • The .pdf is moved to a “completed” folder.
  • All empty folders and files are deleted.
  • The script runs all these actions upon startup, and then waits for updates.

My code:

#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}

UPDATED CODE It works on the first time run, but the loop is broken after converting everything to functions.

#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 "$($prossLoc + "\*")"

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

#Lone Vars
$freshStart = $null
$statusOld = $null
$pathLoc = (Get-Item -Path ".\").FullName
#$pathMagick = 'C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe'

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

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

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

#Convert Nested files into single PDF
function makePDF
{
    & CD $runPath
    & magick $fileTypes $global:fileName
}

#Move final PDF
function moveCmplt
{
    Get-ChildItem -Path "$runPath\*.pdf" -File | Move-Item -Destination $outPath
}

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

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

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

$runIt = { action }

#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 $runIt
#Register-ObjectEvent $watcher "Created" -Action $action
while ($true) { sleep 5 }
Askify Moderator Edited question May 4, 2023