Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 520 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to capture the ftp error code in batch scripts?

#1
I have a somewhat related, but different questions [here][1].

I have a batch script (`*.bat` file) such as this:

@ftp -i -s:"%~f0"&GOTO:EOF
open

[To see links please register here]

myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye

Basically this is a script that uploads a zip file to a ftp site. My question is that, the upload operation can fail from time to time ( the remote ftp is not available, "myfile.zip" is non-existent, upload operation interrupted and whatnot), and when such unfortunate things happen, I want my bat file return 1 ( `exit 1`).

It would be great if my upload wasn't successful, the ftp would throw an exception ( yes, like exception in C++), and I would have a catch-all exception that catches it and then `exit 1`, but I don' think this is available in batch script.

What is the best way to do what I need here?


[1]:

[To see links please register here]

Reply

#2
Your only option in batch files that I know of is to use the "IF ERRORLEVEL" syntax, which requires your ftp client to return a non-zero error code.

[To see links please register here]

is a good reference guide.

Unfortunately I do not if the standard Windows ftp client returns non-zero error codes, so you may have to code your own if this is a requirement. [This link][1] suggests that it does not return an error code, but provides a work around albeit clunky, by redirecting the output to a file and using the FIND command to return an error code.


[1]:

[To see links please register here]

Reply

#3
You can redirect the output to a log file and when the ftp session is finished the file can be parsed.

@ftp -i -s:"%~f0" > log.txt & GOTO :parse
open

[To see links please register here]

myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd /testdir
binary
put "myfile.zip"
disconnect
bye

:parse
for /F "delims=" %%L in (log.txt) Do (
... parse each line
)
Reply

#4
Windows FTP does not return any codes.

I suggest running a batch file that echo your ftp commands to a input response file, then use this file as input to the ftp command, redirecting stderr to a file and verifying the file size. something like this

echo open

[To see links please register here]

>ftpscript.txt
echo myuser >>ftpscript.txt
echo mypassword >>ftpscript.txt
echo lcd "C:\myfolder" >>ftpscript.txt
echo cd /testdir >>ftpscript.txt
echo binary >>ftpscript.txt
echo put "myfile.zip" >>ftpscript.txt
echo disconnect >>ftpscript.txt
echo bye >>ftpscript.txt

ftp -i -s:ftpscript.txt >ftpstdout.txt 2>ftpstderr.txt
rem check the ftp error file size, if 0 bytes in length then there was no erros
forfiles /p . /m ftpstderr.txt /c "cmd /c if @fsize EQU 0 del /q ftpstderr.txt"
if EXIST ftpstderr.txt (
exit 1
)

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through