Download the file and call second script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Download the file and call second script

#1 Post by zagix » 07 Dec 2013 04:46

Hello,

This script works but i need help to resolve my issues.
The codes downloads the file from FTP Server when it is delivered in the directory, but it does not stop looping till i close the command window. I want the script to search for file repeatedly and as soon as it is downloaded it will call the second batch file for further processing.
Thanks in advance.

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%"

 >file.tmp echo open 192.168.1.2
>>file.tmp echo zagix
>>file.tmp echo password
>>file.tmp echo lcd c:\downloads
>>file.tmp echo cd  /stations
>>file.tmp echo binary
>>file.tmp echo mget "1_%datestamp%.eos"
>>file.tmp echo disconnect
>>file.tmp echo bye

:retry
ftp -i -s:"file.tmp"
if not exist "%~1" timeout /t 180 & goto :retry
echo file has downloaded
del file.tmp
exit


Code: Select all

"rem2loc.bat"
WinSCP.exe /console /script=remote2local.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Download the file and call second script

#2 Post by foxidrive » 07 Dec 2013 06:07

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%"

 >file.tmp echo open 192.168.1.2
>>file.tmp echo zagix
>>file.tmp echo password
>>file.tmp echo lcd c:\downloads
>>file.tmp echo cd  /stations
>>file.tmp echo binary
>>file.tmp echo mget "1_%datestamp%.eos"
>>file.tmp echo disconnect
>>file.tmp echo bye

ftp -i -s:"file.tmp"
echo file has downloaded
del file.tmp
call "rem2loc.bat"


The FTP script will exit when the file has downloaded and then it will call the second batch file.

The FTP file is not guaranteed to download completely 100% of the time, but that is the nature of FTP.

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Download the file and call second script

#3 Post by zagix » 07 Dec 2013 07:48

Thanks Foxidrive for the response.

Sorry I think I was not able to question properly,

Code: Select all

:retry
if not exist "%~1" timeout /t 180 & goto :retry

I want this part of the script to do search repeatedly for file, and once it is found at the server directory to download it and then call the second batch file.

One more thing is it possible to run this script in hidden window.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Download the file and call second script

#4 Post by foxidrive » 07 Dec 2013 18:47

zagix wrote:Sorry I think I was not able to question properly,

Code: Select all

:retry
if not exist "%~1" timeout /t 180 & goto :retry

I want this part of the script to do search repeatedly for file, and once it is found at the server directory to download it and then call the second batch file.


Is this to check on the FTP server if the file is there?

One more thing is it possible to run this script in hidden window.


There are third party tools to hide the window, once it is working reliably. CMDOW is one such tool. VBS can do it as well I think.

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Download the file and call second script

#5 Post by zagix » 08 Dec 2013 00:31

foxidrive wrote:

Is this to check on the FTP server if the file is there?

Thanks for the reply.

YES it will recursively/repeately search the FTP Server directory for the file and if found will download it and then only call the second bat file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Download the file and call second script

#6 Post by foxidrive » 08 Dec 2013 03:06

zagix wrote:
foxidrive wrote:

Is this to check on the FTP server if the file is there?

YES it will recursively/repeately search the FTP Server directory for the file and if found will download it and then only call the second bat file.


You need to log into the FTP server every time and just try and download the file - see the change below. It retries every 30 seconds and will execute the batch file when the file has downloaded.



Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%"

 >file.tmp echo open 192.168.1.2
>>file.tmp echo zagix
>>file.tmp echo password
>>file.tmp echo lcd c:\downloads
>>file.tmp echo cd  /stations
>>file.tmp echo binary
>>file.tmp echo mget "1_%datestamp%.eos"
>>file.tmp echo disconnect
>>file.tmp echo bye

:loop
ftp -i -s:"file.tmp"
if not exist "1_%datestamp%.eos" (
   timeout /t 30 /nobreak
   goto :loop
)
echo file has downloaded
del "file.tmp"
call "rem2loc.bat"

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Download the file and call second script

#7 Post by zagix » 08 Dec 2013 06:52

foxidrive wrote:

It retries every 30 seconds and will execute the batch file when the file has downloaded.


Foxidrive, it does not break out of loop after downloading the file and does not call the second batch file for further action.

Please help.
Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Download the file and call second script

#8 Post by foxidrive » 08 Dec 2013 19:05

The file is in c:\downloads which is why. I added the path to where it checks for the file.


Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%"

 >file.tmp echo open 192.168.1.2
>>file.tmp echo zagix
>>file.tmp echo password
>>file.tmp echo lcd c:\downloads
>>file.tmp echo cd  /stations
>>file.tmp echo binary
>>file.tmp echo mget "1_%datestamp%.eos"
>>file.tmp echo disconnect
>>file.tmp echo bye

:loop
ftp -i -s:"file.tmp"
if not exist "c:\downloads\1_%datestamp%.eos" (
   timeout /t 30 /nobreak
   goto :loop
)
echo file has downloaded
del "file.tmp"
call "rem2loc.bat"

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Download the file and call second script

#9 Post by zagix » 08 Dec 2013 22:35

Hi Foxidrive,

Thank you. Its working perfectly.

Please add a log file feature if its possible.

Thanks a lot.
Bye

Post Reply