#Hello people just a guy trying to finish a code

7 messages · Page 1 of 1 (latest)

midnight lagoon
#

$timer = [System.Diagnostics.StopWatch]::StartNew()
while ($timer.Elapsed.TotalMinutes -lt 30) {
    if (Extract-And-Delete ...) {
        $timer.Reset()
    }
    Start-Sleep -Seconds 30
}  
     
function Extract-And-Delete {
    param(
        [string]$SourcePath,
        [string]$DestinationPath,
        [string]$7zPath = "C:\Program Files\7-Zip\7z.exe"
    )
    
    if (-not (Get-Command $7zPath -ErrorAction SilentlyContinue)) {
        Write-Error "7-Zip is not installed. Please install it and try again."
        return
    }
   
    $archiveFiles = Get-ChildItem $SourcePath | Where-Object Extension -in ".zip", ".7z", ".rar"
   
    foreach ($archive in $archiveFiles) {
        $archiveName = $archive.Name
        $archivePath = $archive.FullName

        $destinationName = $archiveName -replace "\.(zip|7z|rar)$"
        $destinationFolder = Join-Path $DestinationPath $destinationName
        New-Item $destinationFolder -ItemType Directory -ErrorAction Ignore
        Write-Verbose "Extracting: $archiveName to $destinationFolder"

        & $7zPath x $archivePath -y "-o$destinationPath"
                if ($LastExitCode -ne 0) {
                    Write-Verbose "Something went wrong with 7z. Exit code was $LastExitCode when
        I tried to extract '$archivePath'"
                    continue
        }

        Write-Verbose "Deleting: $archiveName"
        Remove-Item $archivePath
    }
}

$sourcePath = "C:\foldersource"
$destinationPath = "C:\folderdest"

Extract-And-Delete -SourcePath $sourcePath -DestinationPath $destinationPath```|

Withouth the timeloop at the top the code is in perfect working order Im just trying to get the script to run every 5 min and to automatically stop after 30min of inactivity. I would really appreciate someone to hold my had Im going to need it. I have never written script before and only got this far because of very generous discord members.
river wasp
#

When you call $timer.Reset() it stops the timer, and resets it. So even if it runs , and succeeds the loop will not stop. You need to check if $timer.IsRunning -eq $True otherwise when the ResetTimer is called, and sets the time to 0 .. it will indeed always evalaute to -lt 30

$sourcePath = "C:\foldersource"
$destinationPath = "C:\folderdest"


$timer = [System.Diagnostics.StopWatch]::StartNew()
while ($timer.IsRunning -and $timer.Elapsed.TotalMinutes -lt 30) {
    if (Extract-And-Delete -SourcePath $sourcePath -DestinationPath $destinationPath) {
        $timer.Reset()
    }
    Start-Sleep -Seconds 30
}  
     
function Extract-And-Delete {
    param(
        [string]$SourcePath,
        [string]$DestinationPath,
        [string]$7zPath = "C:\Program Files\7-Zip\7z.exe"
    )
    
    if (-not (Get-Command $7zPath -ErrorAction SilentlyContinue)) {
        Write-Error "7-Zip is not installed. Please install it and try again."
        return
    }
   
    $archiveFiles = Get-ChildItem $SourcePath | Where-Object Extension -in ".zip", ".7z", ".rar"
   
    foreach ($archive in $archiveFiles) {
        $archiveName = $archive.Name
        $archivePath = $archive.FullName

        $destinationName = $archiveName -replace "\.(zip|7z|rar)$"
        $destinationFolder = Join-Path $DestinationPath $destinationName
        New-Item $destinationFolder -ItemType Directory -ErrorAction Ignore
        Write-Verbose "Extracting: $archiveName to $destinationFolder"

        & $7zPath x $archivePath -y "-o$destinationPath"
                if ($LastExitCode -ne 0) {
                    Write-Verbose "Something went wrong with 7z. Exit code was $LastExitCode when
        I tried to extract '$archivePath'"
                    continue
        }

        Write-Verbose "Deleting: $archiveName"
        Remove-Item $archivePath
    }
}
barren vigil
#

Loop must be at the bottom after the function. You can't call the function before you define it (language limitation).

.Reset() should have been .Restart() then the timer will still be running.

midnight lagoon
#

@river wasp @barren vigil thank you both for taking the time to answer. I ended up completely rewriting my code. I spent a lot of time researching scripting/powershell and have a working code. The only issues I'm having now are how to call for a program the right way. I want my script to acknowledge Wabbajack "Installation Complete" window and when it is displayed and detected to close Wabbajack. For those who don't know what Wabbajack is it's a mod downloader. When I say Installation complete I don't mean if the .exe but actually the mods it is downloading internally.

#

I had it right once and somehow lost it or moved something and now it will monitor but not actual detect.

coral glen
#

Don't you have to launch the extract and delete process in a separate thread to monitor its runtime and then kill it?

#

I thought you wanted to kill it after 30 minutes right?