WinSCP downloads wrong file when used with Windows Task Scheduler

Advertisement

Greg1
Joined:
Posts:
2
Location:
Chicago

WinSCP downloads wrong file when used with Windows Task Scheduler

I have a .bat file that executes perfectly when run on its own. When used in Windows Task Scheduler it runs, but always downloads the wrong file (the same file each time). I know the scripting is good because we have other scheduled tasks that use the same script and work perfectly. The task is configured the same as all other tasks utilizing the same script, and it is using the same credentials as when I run the .bat file manually.

WinSCP is supposed to download a file based upon yesterday's date. When run manually it does it perfectly every time. When run from the task scheduler it always downloads the file dated '20180928'. I am including my .bat script here for review, but I truly believe the problem is elsewhere. Any ideas?

@echo off

:: Create a variable containing yesterday's date
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%yyyy%%mm%%dd%"

:: Create a variable to hold the password needed to unzip the LiveVox recordings
set yourZipPassword=magic

:: Create a variable for the output folder for zipped LiveVox calls to send to callminer
set output=C:\

:: Create a folder with yesterday's date on Charlie
md \\charlie\Recordings\LiveVox\%date-yesterday% >nul 2>&1 

:: Script the FTP download action to a txt file
echo option batch abort >> DownloadLiveVoxCalls.txt
echo option confirm off >> DownloadLiveVoxCalls.txt
echo open LiveVox-Recordings >> DownloadLiveVoxCalls.txt
echo get -preservetime -transfer=binary /%date-yesterday%_RECORDINGS* \\jerry\c$\LiveVoxDailyCalls\ >> DownloadLiveVoxCalls.txt
echo exit >> DownloadLiveVoxCalls.txt

:: Execute the download script file
"C:\Program Files\WinSCP\winscp.com" /script=DownloadLiveVoxCalls.txt

:: Copy 7z.exe file to LiveVoxDailyCalls folder
pushd "\\jerry\c$\LiveVoxDailyCalls"
copy "C:\Program Files\7-Zip\7z.exe"

:: Unzip recording file using password
for %%I in ("*.zip") do (
  7z x -p%yourZipPassword% -y -o"%%~dpI" "%%~fI"
)

:: Delete downloaded zip file
del "C:\LiveVoxDailyCalls\*.zip"

:: Copy csv file from Jerry LiveVoxDailyCalls to Charlie csv file
pushd "\\charlie\Recordings\LiveVox\csv files"
copy "C:\LiveVoxDailyCalls\%date-yesterday%.csv"
popd

:: Copy contents of LiveVoxDailyCalls to Charlie
pushd "\\charlie\Recordings\LiveVox\%date-yesterday%"
copy "C:\LiveVoxDailyCalls\*"
popd

:: Zip the call files to send to callminer
7z a %output%\LiveVoxDailyCalls_%date-yesterday%.zip *.vox

:: Delete the 7zip executable from Charlie
del "\\charlie\Recordings\LiveVox\%date-yesterday%\7z.exe" /q

:: Delete the contents of the LiveVoxDailyCalls folder
del "C:\LiveVoxDailyCalls\*" /q

:: Delete the FTP download script file so a fresh one can be made
del "C:\Program Files\WinSCP\DownloadLiveVoxCalls.txt" /q

Reply with quote

Advertisement

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

Re: WinSCP downloads wrong file when used with Windows Task Scheduler

Your code to resolve the yesterday date has nothing to do with WinSCP. So, is there any reason you believe that this is WinSCP problem? If you believe so, please attach a full session log file both when executed "run on its own", and when "used in Windows Task Scheduler".

Anyway, note that WinSCP has a built-in syntax to resolve yesterday timestamp.
It should make your script a way simpler:
https://winscp.net/eng/docs/scripting#timestamp

Reply with quote

Advertisement

You can post new topics in this forum