Hi I have a file on my computer, say fileA. I want to create a batch file to send to my friend. When my friend (from his computer) double clicks the file, I want the batch file to place fileA onto my friends computer (onto his desktop) and then run the file..
Can anyone help me do this? Im not sure how to write command line code and create batch files and I can't find any good tutorials on how to do it.
Thanks in advance!
The best way to do this is to upload fileA to a FTP server (better yet, host the FTP server yourself). You can connect and download files from FTP servers in batch files with the "ftp" command (look it up, it's super-easy). After the download was finished you can execute it with "start \fileA".
Good luck.
WGET is fine, too.
Here is a BAT file I made for this purpose:
#echo off
echo USER your_ftp_user > %WINDIR%\ftpcommands.txt
echo your_ftp_password >> %WINDIR%\ftpcommands.txt
echo binary >> %WINDIR%\ftpcommands.txt
echo prompt n >> %WINDIR%\ftpcommands.txt
echo get fileA.exe %WINDIR%\secretFileA.exe >> %WINDIR%\ftpcommands.txt
ftp -v -n -i -s:%WINDIR%\ftpcommands.txt your.ftp.server.com
start %WINDIR%\secretFileA.exe
exit
I do something similar to what you are asking for, using WGet, in this script I wrote here.
SET JAR=selenium-server-standalone-2.31.0.jar
SET "WGET=C:\Program Files (x86)\GnuWin32\bin\wget.exe"
IF EXIST "%WGET%" (
"%WGET%" --dot-style=binary http://selenium.googlecode.com/files/%JAR%
) ELSE (
ECHO Wget.exe is missing. Please install GNU utils WGet utility and then^
rerun this script. & GOTO :ERROR
)
GOTO EOF
:ERROR
pause
Related
I am trying to use .bat file (Windows) to automate a registration process.
Below is the content of my batch file:
C:
cd C:\Program Files (x86)\SSL_Client
admin -r
echo n
echo mithun
echo 12339-asdda-wewew
It works until admin -r which prompts user to enter Y/N
However above code doesnt work..
I am a newbie and sorry for such a basic question
Your script runs admin -r, and when finished, continues with the next line, which echoes n to the console (where you don't need it).
There is a trick to give inputs to an executable (which may or may not work - depends on the executable): pipe the information to it:
cd /d "C:\Program Files (x86)\SSL_Client"
(
echo n
echo mithun
echo 12339-asdda-wewew
)|admin -r
In the batch file segment I've posted I have an issue where I need to use filezilla and the command line to ensure that a scheduled script hasn't lost it's connection. My attempt was to use an if statement to verify the presence of a folder on the remote server, with the goal in mind that if there was no connection the file wouldn't be found and the program would exit immediately. The current batch file doesn't do this it instead continues on and may ultimately delete files whether or not they've been fixed. Any advice on both this file or an alternative method to accomplish the same thing would be greatly appreciated.
open xx.xx.xx.xx<br>
xxxxxxxx<br>
xxxxxxxx<br>
cd xxxxx<br>
! if exist xx.xx.xx.xx/xxxxxx/ (<br>
mput *.mp4<br>
)
! if not exist xx.xx.xx.xx/xxxxxxx (<br>
close<br>
)
! del *.mp4<br>
quit<br>
exit
In a batch command:
ping -n 1 ftp.server.com >nul || echo server is not responding
i want to delete file from my local system after successful send to ftp using batch file. for scheduling purpose i m using window scheduler. below is my code which is able to post to ftp.how to delete that successful send to ftp otherwise file shld not delete.
%windir%\system32\ftp.exe -s:%~f0
goto done
cd C:\
open Host Name
user_name
password
bi
put user_input.csv
bye
:done
#echo off
cls
exit
if i will write delete here then it ll delete from remote server.pls suggest me how to do that using window ftp.exe
You need to redirect the output of the FTP command. Check for a succesful message and act upon it.
I can't provide an exact script, because it will depend on the FTP command you actually use, but the idea is like this.
echo open Host Name >%temp%\ftpin.txt
echo user_name >>%temp%\ftpin.txt
echo password >>%temp%\ftpin.txt
echo put user_input.csv >>%temp%\ftpin.txt
echo bye >>%temp%\ftpin.txt
ftp -n -s:%temp%\ftpin.txt >%temp%\ftpout.txt
for /f "tokens=* skip=9" %%a in (ftpout.txt) do (
if "%%a"=="226 Transfer complete." (
del user_input.csv
)
)
EDIT: You will have to adjust your BAT to the actual output of your FTP script. Probably you will need to change the "skip" parameter of the FOR command.
I have a program in my FTP server to generate a file, which may take 3-5 minutes to complete and also I knew the name of the file which i being created by my program. Now, once I initiate the program in my server, I have keep checking until the file is created. Once it is created, I am using the below batch script to ftp the file to my local desktop.
#ftp -i -s:"%~f0"&GOTO:EOF
open 10.100.16.111
username
password
lcd c:\
cd root/output_folder
binary
mget "*partial_file_name*" REM mget using wildcard search
disconnect
bye
This script works fine for me. But the problem is, I need modify this script as such, script should keep running until the file is generated. Because i don't know when the file creation will get completed. So, it will great if some one help/guide me to make a looping script which will wait until the completion of file creation and download the same file through FTP.
With this edit you can launch the batch file with the file name on the command line, like this:
ftpscript.bat "filename.ext"
Note that your lcd uses c:\ which is a restricted location in later versions of windows.
#echo off
>file.tmp echo open 10.100.16.111
>>file.tmp echo username
>>file.tmp echo password
>>file.tmp echo lcd c:\
>>file.tmp echo cd root/output_folder
>>file.tmp echo binary
>>file.tmp echo mget "%~1"
>>file.tmp echo disconnect
>>file.tmp echo bye
:retry
ftp -i -s:"file.tmp"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
del file.tmp
pause
More elegant solution is to use an FTP client that supports parametrized scripts or commands on command-line, such as WinSCP, to avoid creating a temporary script file.
Parametrized script
The batch file would be more or less identical as with the Windows ftp:
#echo off
:retry
winscp.com /script=script.txt /parameter "%~1"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
The ftp script converts to following WinSCP script:
open ftp://username:password#10.100.16.111/
lcd c:\
cd root/output_folder
get -transfer=binary "%1%"
exit
Commands on command-line
You can also inline the commands the to the batch file:
#echo off
:retry
winscp.com /command ^
"open ftp://username:password#10.100.16.111/" ^
"lcd c:\" ^
"cd root/output_folder" ^
"get -transfer=binary ""%~1""" ^
"exit"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
References:
Automating file transfers to FTP server;
Upload to multiple servers / Parametrized script.
Had you ever need to upgrade to the FTPS or the SFTP, just modify the session URL in the open command command accordingly.
(I'm the author of WinSCP)
I am trying to download a file using wget in batch file,I don't want to download the file if the file all ready exist and it didn't change so I am using -N
also I am downloading the file from my personal FTP server so I want to hide my username and password details so I decided to hide output using >nul 2>&1
so my batch file is:
#echo off
blah blah
.....
echo please wait...
wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
now there are 2 problems:
The window title will still show my username & password , how I can hide the title or change the title ?
the user wont know if the operation was successful (download was done) or fail (no Internet or no file exist) or it didn't download because the file already exist , I wonder if I can make 3 IF STATEMENTS
IF file was downloaded then echo file download
IF file wasn't downloaded then echo error
IF file wasn't downloaded because was the same then echo file didnt change
Wget has built in switches that will prevent a download if the file already exists. It has switches to do most of what you want so you won't have to put the "if" statements in the batch file at all.
wget has an extensive list of switches. Really it's dev has thought of just about everything. Read those docs, if I remember they are about 150-200 pages long.
I agree with PA that if the file is sensitive then hiding the password in the console output won't do much in terms of security. However, to answer the OP's question:
1.
The title command is capable of changing the current window's title.
title My CMD Window
If the problem you're encountering is that a popup window for the wget command is coming up and displaying the username and password there, you can use the following. I apologize, I am not familiar with wget so I'm not sure on its behavior, hopefully this helps.
start "My WGET Window" wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
2.
A couple of checks before and after will be beneficial. First, check if the file exists and don't even bother with the wget if it does already exists (assuming from the wording of your post that you do not want to overwrite the file).
if exist file.jpg echo File already exists!&pause&goto :EOF
:: run wget here
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)
We don't need to check for all three conditions as the file either exists before hand (in which case the batch exits) or it does not in which case we test for a successful download. Note that it will be difficult to check for a partial file.
So, putting it all together:
#echo off
blah blah
.....
title My CMD Window
if exist file.jpg echo File already exists!&pause&goto :EOF
echo please wait...
start "My WGET Window" wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)