FTP commands in a batch script does not working properly - batch-file

I've made a simple FTP upload script that should upload multiple files from a Windows 2008 Server to the FTP location. I've tried this manually by executing every command of the script directly in CMD and it works fine. However when I run script.bat it says that none of the commands are recognized as internal or external commands. I checked the ENV variables and there is a path to System32 so it should be fine. Can anyone please help with this. Thank you
open xx.xxx.xx.xx
user
pass
prompt
bin
lcd X:\test\test\
cd /tempTest/tempTest
binary
mput "*.*"
disconnect
quit

You can also try something like that with a batch file for multiple file Upload :
MultipleFileUpload.bat
#echo off
Title Multiple file Upload by Hackoo
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.xx.xxx.xx.xx.com
Set USER=UserName
Set Password=YourPassword
Set LocalFolder=X:\test\test
Set RemoteFolder=/tempTest/tempTest/
::***********************************
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo lcd %LocalFolder%
>> ft.do echo cd %RemoteFolder%
>> ft.do echo mput *.*
>> ft.do echo bye
ftp -s:ft.do
del ft.do
Pause

Place your script in a text file on your desktop called ftpscript.txt
Create a batch file called getftp.bat and inside it have this - then you can click the bat file.
#echo off
ftp -i -s:"%userprofile%\desktop\ftpscript.txt"
pause

Related

Batch Script for Download files from FTP server with Condition [duplicate]

This question already has answers here:
Batch file to download the latest file from FTP server
(4 answers)
Closed 12 months ago.
Please need your support to crack the batch script for downloading files from FTP server with below condition.
Requirement is need to get the current directory folder name basis on date format like "YYYY-MM-DD".
I have tried to use the SET command but same is not working, Pl find below complete script details for your reference. Anyone please suggest and provide me the solution.
ftp
open 11.111.13.11
username
password
***cd /data/ %Today% (Folder Name- Automatic date format required like "YYYY-MM-DD")***
lcd d:\
binary
prompt
mget *.csv
bye
script for Current Date folder
SET Today=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
echo %Today%
Please let me know if any more details required..
You'll need to build the script file dynamically and then call it.
#echo off
SET Today=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
SET ftpscript=%TEMP%\ftpscript.txt
echo open 11.111.13.11 > %ftpscript%
echo username >> %ftpscript%
echo password >> %ftpscript%
echo cd /data/%Today% >> %ftpscript%
echo lcd d:\ >> %ftpscript%
echo binary >> %ftpscript%
echo prompt >> %ftpscript%
echo mget *.csv >> %ftpscript%
echo bye >> %ftpscript%
ftp.exe -s:%ftpscript%
Note the first echo overwrites and the rest append.

Works properly, but not when running as administrator

My batch file makes another batch file. It works when you run it normally.
#echo off
type NUL > batchfile.bat
ECHO #echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit
However, when you run it as an administrator, it doesnt work. I need to make it run properly also when running as administrator. What is the correct way to do it ?
When you run as administrator, it changes the current context directory. I'm not sure where it changes to, but you can avoid that problem by specifying the full output path to the new batch file, like so:
#echo off
type NUL > "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO #echo off >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
ECHO exit >> "C:\Users\Troy\Documents\Software\batch files\batchfile.bat"
exit
UPDATE: I just discovered that there's a way to dynamically change the current directory to the same one as the currently executing batch file. So, the following is probably a cleaner solution. It just involves adding one line at the top of the original script:
cd %~dp0
#echo off
type NUL > batchfile.bat
ECHO #echo off >> batchfile.bat
ECHO set hostspath=%%windir%%\System32\drivers\etc\hosts >> batchfile.bat
ECHO exit >> batchfile.bat
exit

Batch Script to copy contents of one folder to the other under C:\Program Files (x86) location. Not working - Acess Denied Error

I have written simple batch script, which copies contents of one folder to the other. I have already logged into the machine as Admin. I am working on windows-7 machine.
Folder 1 and Folder 2
Location of folders :
C:\Program Files (x86)\Folder1
C:\Program Files (x86)\Folder2
If i am running my script by double clicking it, i am getting Access Denied. If I right click on file "Run as Administrators" it correctly copies.
Expected Behavior:
On directly double clicking the batch script it should copy the file.As i am already logged in as Admin.
Waiting for valuable feedback, from fellow coder's
Following script, helps to run your batch file as "Run As Admin".
#echo off
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
echo %~nx0: running payload with parameters:
echo %*
echo ---------------------------------------------------
echo ADM is launching. DO NOT CLOSE THIS WINDOW.
cd /d %2
shift
shift
echo Name of the batch file which you want to run as admin
CopyFiles.bat
goto :eof
exit
May be your users group does not have Read and Execute permissions to the command processor,
please have a look at the solution described here
http://support.microsoft.com/kb/867466

Batch script to wait for file to be generated on FTP server and download

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)

batch file creation pse

I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".
I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.
Here is the WindowsCommands batch file:
md %computername%
echo off
echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %cd%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %cd%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %cd%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %cd%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %cd%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt
exit
I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.
I also created another batch file name psexec for running a For loop.
Now my troubles and arising when trying to run the psexec batch file.
Here is my psexec file:
for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)
Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.
So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.
If anyone could point me in the right direction that would be great!
Thanks!
If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.
for /f %%a in (hosts.txt) do (
for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
if errorlevel 0 (
psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
)
)
)
Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:
#echo off
FOR /F "tokens=2" %%A IN (
'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A
if not exist "%dl%\%computername%" md "%dl%\%computername%"
echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %dl%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %dl%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %dl%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %dl%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %dl%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt
Net use %dl% /delete

Resources