mget in batch script does not work - windows

i'd like to create a batch script which logs on a ftp-server and copies some files to a remote folder. but my script does not work. mget runs into a timeout (has to be terminated manually).
i'm starting ftp connection with ftp.bat:
ftp.exe -s:getdata.bat
getdata.bat:
OPEN host-ip
user
password
lcd "C:\tmp"
cd config
mget C1000.xml
close
starting, logging in and changing the directory works but getting the file doesn't work. nothing happens until i end the script manually. any hints?
regards,
michael

mget prompts for user confirmation for each file it finds, even when you only specify one specific file.
To get around this, you can either use get if you know the file name, or you can use prompt to disable the interactive prompt before you use mget.
OPEN host-ip
user
password
lcd "C:\tmp"
cd config
prompt
mget C1000.xml
close

Related

How to execute further batch file commands after running "ftp" commands contains in the batch file itself

I have a .bat document where I do some operations with FTP. I want to end the FTP connection and get to finish with the terminal on the desired directory.
I'm trying this, but when it gets to quit, it stops.
cd %USERPROFILE%\Foilder\project\angular
call ng build
#FTP -i -s:"%~f0"&GOTO:EOF
open connection
user
password
cd httpdocs/project
mdelete *.woff
quit
cd %USERPROFILE%\Foilder\Subfolder
It gives me this message right after doing the quit and never runs the next line.
221 Goodbye.
Ideal scenario, run this last cd command and have the terminal ready on the folder that I want to run it. Thing is that I have to do some uploads to two different FTP servers and it would be great to have the console finishing in the folder where I run "dev.bat" to get it to work
Is it possible to do this?
Your script uses a known hack that allows you to specify ftp commands directly in the batch file.
The %~f0 is replaced with the path to the batch file itself. So the ftp -s:%~f0 runs ftp and tells it to use the batch file itself as ftp script file. You have probably noticed that it results in couple of errors, as ftp fails on the first few lines of the batch file, which are not valid ftp commands (the cd ..., call ... and ftp ...).
Equivalently, the batch file would try to run all commands after ftp ... once ftp finishes, also failing, as those are not valid batch file commands. To avoid that, the hack uses GOTO:EOF to skip the rest of the batch file (EOF=eof of the file).
While you actually want to execute some commands after the ftp. At least the cd command. So do not skip the rest of the batch file. Skip only the ftp commands:
ftp -i -s:"%~f0"&goto AFTER_FTP
(ftp commands)
quit
:AFTER_FTP
cd %USERPROFILE%\Foilder\Subfolder
Note the # before ftp. That (and the GOTO:EOF) are clear signs, that the script you based your batch file on, was designed to start with ftp on the very first line and contain nothing else, but the ftp commands. You have deviated from that.
Alternatively, use some more capable FTP client that allows specifying the commands on its command-line without hacks.
For example with my WinSCP FTP client, you can do:
cd %USERPROFILE%\Foilder\project\angular
call ng build
winscp.com /ini=nul /command ^
"open ftp://user:password#connection/" ^
"cd httpdocs/project" ^
"rm *.woff" ^
"exit"
cd %USERPROFILE%\Foilder\Subfolder
There's a guide for converting Windows ftp script to WinSCP script.

How to delete local computer files in the ftp session with a file. scr?

I have a .bat file (test.bat) :
ftp -s:Test_DOWN.scr
pause
I have a .scr file (Test_DOWN.scr) :
open ftp01.citobi.be
USERNAME
PASSWORD
prompt n
mput *.csv
quit
cd C:\...
del *.csv
The problem is that I want to remove files in my directory C:... but when I execute test.bat, it doesn't execute the last two lines.
I want to know if it is possible to run these two lines in the same script after ftp commands?
ps : When I launch prompt command from start --> run --> cmd and I connect to ftp. The quit ftp command works well, I pass the ftp session to host session. But with the .bat file, after quitcommand, it's over..
I solved the problem...
I used ! ftp command (escape to the shell).
!del *.csv
And it works well.

Implicit TLS FTP connection using WinSCP or other command line FTP client in Windows

I need to connect to an FTP secured with implicit TLS, username and password from the command line so that I can create a batch later on. I am using WinSCP since it's the most popular (I think) or the one that I can use to achieve this according to extensive googling. I'm not an expert but reading the documentation I have this so far with unsuccessful connection from command prompt:
winscp> open username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
Once connected I need to extract everything there to a folder \hostname\folder or c:\folder then delete the files on the ftp (more or less like cut)
Thanks to Martin Prikryl correction I was able to connect, here is what I did:
Created a text file with the winscp.com commands "connection.txt":
# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect with username and password
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa...."
# Change local directory
lcd C:\local_working_folder
# Force binary mode transfer
option transfer binary
# Download missing files to local directory from remote
synchronize local
# Delete all remote files
rm *
# Disconnect
close
# Exit WinSCP
exit
Now from command prompt or in a batch file you just call winscp.exe from where it is installed. If from installation media it should be C:\Program Files\WinSCP\WinSCP.exe of if extracted from a .zip portable version point to the folder where you extracted the file:
C:\WinSCP\WinSCP.exe /console /script="connection.txt" /log="conn.log"
Then just schedule the batch to automate the process.
Your syntax is almost correct. You are missing only the protocol prefix ftp:// (note that WinSCP defaults to the SFTP protocol).
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
For details see:
https://winscp.net/eng/docs/session_url
https://winscp.net/eng/docs/scriptcommand_open
If that does not help, you need to share with us a script log file or at least an error message.

Batch file - Remote Copy and Execute issue

I have a batch file that part of it copies a folder and its content to a local directory and then continues through the batch process which then tries to execute the contents of the folder . locally run it works on all wintel os and copies the files from \networkshare\folder*.* to %systemdrive%\temp\ .
I have attempted to use e.g psexec #serverlist.txt -c batfile.bat -s -f -d to copy the file and then execute it remotely how ever the issue is that this will not copy the files when run remotely . I think its a authentication issues after you a have remotely executed the batch file the remote system will not allow me to access/authenticate the networkshare
i have tried xcopy , copy , robocopy .
AFAIK you can only authenticate against the next hop using implicit credentials. Connection attempts from the first hop to a second hop will fail, even if your user has the required permisssions. See e.g. here for an explanation.
Try this:
psexec #serverlist.txt -u %USERDOMAIN%\%USERNAME% -d -c batfile.bat
Use explicit credentials so you're authenticated against the remote host. With that the second hop will be the next hop for your authenticated session. Don't run the script as LOCAL SYSTEM (-s), because that account is restricted to local resources.
I've had this issue in the past. Instead of trying to copy/run the BAT file just run the script things from a UNC path if you are able to. I think the problem lies in the BAT file not actually able to run through PSEXEC and copy like you think. Does it give an exit code?
Here's an example I used a while ago to install Adobe reader. I tried to do it through a batch file but no dice. I could not get it to run within the batch file whether I copied it or not. I can't remember the exact reason, I think it had something with the way a batch file is called in the system and runs in some sort of local context. Don't quote me on that though as I can't remember the exact why.
Here is my code example:
psexec /accepteula \\%computer% -s cmd /c msiexec /i "\\UNC\Software\adobe\Adobe Reader 7.0.9.msi" TRANSFORMS="acrobat7.mst" /qn

Check existence of directory on local machine during ftp session

I am copying a large number of files during an ftp session from a remote host to my local machine. I need to save the files in a local directory tree. My problem is that a particular directory may not exist before the session. The way I have handled this in the past is to do set up my script by first creating the directory tree and not even worrying if the directory exists and then from within the same batch file (I create the batch file using Python) I start my session and use lcd to change to the correct directory
md c:\123
md c:\234
md c:\234\2009
loginname
password
cd remotedirectory
lcd c:\123
get somefile.txt
So all of the above is written out to one batch file and I start it to run. If the directory exists when I try to create it then I see a message in the terminal window that the directory exists and since nothing bad happens I have not worried about it.
What I would really like to do is check the existence of the local directory when I am ready to move to that directory and if it does not exist it gets created but I have not found out how to do this without closing the session and restarting it so I go back to the shell.
Is there a way to do this during the ftp session while maintaining the connection with the host?
You can use the ! command prefix to execute CMD MD command in order to make sure a local directory is already exist. e.g.:
! md "c:\my data\download" will create c:\data\download directory if it's not yet exist (and assuming you have the required permission). If it's already exist, it'll just display a harmless error. You can also use ! md "c:\my data\download" > nul to omit the error message. Or perform other commands.
Basically, everything after the ! ftp command prefix, will be passed to CMD.EXE as cmd /c {your command(s)}. If ! is used alone, it'll escape to CMD session prompt and wait for user input. The CMD's EXIT command will close the CMD session and return to the FTP prompt.

Resources