If/Else Logic in Script

Advertisement

noochi01
Joined:
Posts:
9
Location:
USA

If/Else Logic in Script

Hi Guys,

I would like to take two of my scripts one step futher and need some assistance. I'll provide my examples below, but first a very quick explaination. AND, I'm extremely new to WINSCP scripting....

I wrote a batch file that runs on an internal server with winscp installed, that calls a .txt file I've written with scp code. This code connects to a server in my DMZ (ftp server), and then successfully copies a specific file from my ftp server to a different internal server. After it copies the file internally, it finally deletes the file on the ftp server. All of that works perfectly. What I'm looking to do is add an if else statement, or some sort of logic so that if it finds the specific file on the remote server (ftp server), then perform the internal copy. If it doesn't find any file, I want it to close out properly.


Script that calls WINSCP and passes my FromFTPServer Script
=========================
start "" "C:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script=C:\WINSCP_Test_Scripts_12_22_13\FromFTPServer.txt
/log="C:\WINSCP_Test_Scripts_12_22_13\FromFTP.log"

#PAUSE
exit
=========================

FromFTPServerScript
=========================
open sftp://Username:password@LocalFTPServerinDMZ

cd /home/User01/incoming/

option transfer binary
lcd \\InternalServer\InternalDirectory
get INCOMINGTESTFILE.TXT INCOMINGTESTFILE.TXT
rm /home/User01/incoming/INCOMINGTESTFILE.TXT
=========================

So, I need assistance adding some logic to this so that, if INCOMINGTESTFILE.TXT exists, then copy locally and delete. AND, If INCOMINGTESTFILE.TXT doesn't exist, then exit gracefully.

Any thoughts on how I can make this happen?

Thanks in advance,
Mike

Reply with quote

Advertisement

noochi01
Joined:
Posts:
9
Location:
USA

Update!

Hi Guys,

I figured it out, and unlike so many other threads that I see on the web, I'd like to post a solution to help others who are new like me, and might face a similar situation. Full code is in my previous thread.

SO, in order to avoid errors in my script from files not being present, I first check to see the file exists. If it does, then I proceed...
=========================
IF EXIST \\InternalServer\InternalDirectory\FILENAME.TXT start "" "C:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script=C:\WINSCP_Test_Scripts_12_22_13\FromFTPServer.txt /log="C:\WINSCP_Test_Scripts_12_22_13\FromFTP.log"

exit
=========================
If the internalserver file exists, then it proceeds to call WINSCP. If the file doesn't exist, then the script closes. Works great through all of my tests.

Also, I was unaware of these two commands previously, but am using them now. They allow me to perform an overwrite, and then copy a file and delete the source file after copy.

option confirm off
get INCOMINGTESTFILE.TXT INCOMINGTESTFILE.TXT -delete

FromFTPServerScript
=========================
open sftp://Username:password@LocalFTPServerinDMZ

cd /home/User01/incoming/

option transfer binary
option confirm off
lcd \\InternalServer\InternalDirectory
get INCOMINGTESTFILE.TXT INCOMINGTESTFILE.TXT -delete

I hope it helps!
Mike

Reply with quote

noochi01
Joined:
Posts:
9
Location:
USA

Scheduled batch not running when logged off

martin wrote:

Thanks for sharing your solution.

Anytime! I have a follow-up Martin and need some assistance....

I'm running WINSCP on Server 2003, moving files back and forth between my internal network and DMZ network. I'm accomplishing this through scheduled jobs that call my batch files which in turn call my script. Everything works perfectly. I can launch the batch files manually, and they perform as expected, I can schedule the batch jobs, and again they peform as they should. The issue I have now identified, the last piece of the puzzle, is that when I log off the server, the jobs no longer run. If I Remote Desktop to the server, and click the X to kill my session, the jobs still run because technically my profile is still logged on. Any idea of how I can have the scheduled jobs continue to run while logged off? This I thought would have been the easiest part of this process!

My scheduled job calls this batch file:
start "" "C:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script=C:\WINSCP_Test_Scripts_12_22_13\FromFTPServer.txt
/log="C:\WINSCP_Test_Scripts_12_22_13\FromFTP.log"

#PAUSE
exit

FromFTPServer.TXT Contents:
open sftp://LOCALUSERNAME:PWD@IPADDRESS

cd /home/LOCALUSERNAME/incoming/

option transfer binary
option confirm off
lcd \\InternalServer\InternalDirectory
get INCOMINGFILE.TXT INCOMINGFILE.TXT -delete

#end script
#close
exit

Works perfectly while logged in. I included all code, but really I think it's something with the actual batch file being called. The Run As is a local admin and has full access to all directories.

Any asistance is greatly appreciated!!!!

Thanks,
Mike

Reply with quote

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

Re: Scheduled batch not running when logged off

noochi01 wrote:

I'm running WINSCP on Server 2003, moving files back and forth between my internal network and DMZ network. I'm accomplishing this through scheduled jobs that call my batch files which in turn call my script. Everything works perfectly. I can launch the batch files manually, and they perform as expected, I can schedule the batch jobs, and again they peform as they should. The issue I have now identified, the last piece of the puzzle, is that when I log off the server, the jobs no longer run. If I Remote Desktop to the server, and click the X to kill my session, the jobs still run because technically my profile is still logged on. Any idea of how I can have the scheduled jobs continue to run while logged off? This I thought would have been the easiest part of this process!
Please read FAQ:
https://winscp.net/eng/docs/faq_scheduler

If that does not help, attach a full log file showing the problem (using the latest version of WinSCP).

To generate log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you may email it to me. You will find my address (if you log in) in my forum profile. Please include link back to this topic in your email. Also note in this topic that you have emailed the log.

Reply with quote

Advertisement

noochi01
Joined:
Posts:
9
Location:
USA

Re: Scheduled batch not running when logged off

[quote="prikryl"]

noochi01 wrote:


Please read FAQ:
https://winscp.net/eng/docs/faq_scheduler

If that does not help, attach a full log file showing the problem (using the latest version of WinSCP).

To generate log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem).

Hi Martin,

Thank you for your reply. It looks like it's reading the registry. I'm adding two attachments- one for each script that I'm attempting to run while logged off, but both fail to execute. These logs were generated while logged in. When logged off, the scripts don't get called, therefore nothing is added to the log files.

Let me know what you think. Verison 5.50 (Build 3839)

Thanks,
Mike
Description: Log Sending to the FTP Server
Description: Log Pulling from the FTP server

Reply with quote

noochi01
Joined:
Posts:
9
Location:
USA

Hi Martin,

Any thoughts on why this wouldn't be working? I still haven't been able to resolve this yet.

Thanks,
Mike

Reply with quote

martin
Site Admin
martin avatar

Re: Scheduled batch not running when logged off

OK, I may understand finally. So it's nothing to do with WinSCP right? It's just that the Windows Scheduler does not start you task. Maybe you have Scheduler's task option Run only when user is logged on selected?

Reply with quote

Advertisement

You can post new topics in this forum