Getting SECOND newest file?

Advertisement

jpwanabe
Joined:
Posts:
4

Getting SECOND newest file?

So I am running a few game servers for multiple games and am running into an issue with automatic backup of some of the servers. I have a script that downloads the latest zip file once a day at 2am. The issue is with the larger servers that take a long time to make that zip file. If the file is in the process of still being zipped and the script runs to download that zip, the whole process stalls out. If there was a way to download the second newest file this issue would not be a problem. I can't seem to find a way to do this after searching for a few days. Any help would be appreciated.

One line of current script for reference
"get -latest IP_PORT/backups/*.zip F:\Server_backups\ATM6\*" ^

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,478
Location:
Prague, Czechia

Re: Getting SECOND newest file?

That's not possible with simple WinSCP scripting.
But you can use WinSCP .NET assembly from a PowerShell script.
See https://winscp.net/eng/docs/script_download_most_recent_file#library
In the code, just add Select-Object -Skip 1 | before Select-Object -First 1.

This guide will help you converting your current script:
https://winscp.net/eng/docs/library_from_script

Reply with quote

jpwanabe

I've been trying to get this working but I do have a question. How would I set multiple remote and local locations in the script for powershell? I have never messed with it before now. I was doing it all from .bat files. I can't seem to figure it out without multiple scripts. Sorry for the newbie question.

Reply with quote

jpwanabe
Joined:
Posts:
4

This is what my script is atm
"F:\Program Files (x86)\WinSCP\WinSCP.exe" ^
  /log="F:\desktop\MC server\!auto\backup.log" /ini=nul ^
  /command ^
    "open ftp://user:pass@ip/ -rawsettings ProxyPort=0" ^
      "get -latest folder1/backups/*.zip F:\Server_backups\ATM6\*" ^
      "get -latest folder2/backups/*.zip F:\Server_backups\RLCRAFT\*" ^
      "get -latest folder3/backups/*.zip F:\Server_backups\POKE\*" ^
      "get -latest folder4/backups/*.zip F:\Server_backups\BMF\*" ^
      "exit"

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,478
Location:
Prague, Czechia

I'm no PowerShell expert, and there are probably many ways. But for example this will do:
$foldersSet = @( 
    @{ remote = "folder1/backups"; local = "F:\Server_backups\ATM6" },
    @{ remote = "folder2/backups"; local = "F:\Server_backups\RLCRAFT" },
    @{ remote = "folder3/backups"; local = "F:\Server_backups\POKE" },
    @{ remote = "folder4/backups"; local = "F:\Server_backups\BMF" }
)
 
foreach ($folders in $foldersSet)
{
    $remotePath = $folders.remote
    $localPath = $folders.local
 
    Write-Host "$remotePath => $localPath"
 
    $directoryInfo = $session.ListDirectory($remotePath)
    # ... and the rest of the code up to $session.GetFileToDirectory
}

Reply with quote

jpwanabe

Thanks for the help man, but I just cant get this to work. Getting errors that I can't figure out what they mean or how to fix em. I think I'm going to just give up at this point. Been trying for a while. I just cant seem to get power shell.

Reply with quote

Advertisement

You can post new topics in this forum