Overwrite files of ftp server by batch - batch-file

I have this script to send files to my ftp:
#echo off
#setlocal enableextensions
#cd /d "%~dp0"
color 0a
mode 34,12
title Enviar Vendas Eco
echo.
echo.
echo.
echo.
echo Sending ...
echo.
echo.
echo Wait ...
( echo open ftp.xxxxxxxxxxxxx.com
echo xxxxxxx
echo xxxxxx
echo ascii
echo prompt
echo cd "place1"
echo cd "place2"
echo lcd "%userprofile%\Desktop\test"
echo mput *.txt -overwrite
echo bye
)> %temp%\ftpsend.dat
ftp -s:%temp%/ftpsend.dat >nul 2>nul
del /f /s /q %temp%\ftpsend.dat >nul 2>nul
exit
By default, the script would must overwrite the files if already exists on .ftp. But it isn't happening.
What is wrong on my script?
When i run appears the file was suceffully transfered, but it didn't replace the file that already exists on ftp.
Look the image:

Related

Delete files of ftp server in two or more different directories

#echo on
setlocal EnableExtensions DisableDelayedExpansion
#cd /d "%~dp0"
( echo open ftp.xxxxxxxx.com
echo kaxxxxx
echo kaxxxxx
echo ascii
echo cd "dir1/TUN/"
echo mdelete *.txt
echo cd -
echo cd "dir2/TUN2/"
echo mdelete *.txt
echo bye
)> %temp%\ftpdel.dat
ftp -i -s:%temp%/ftpdel.dat
del /f /s /q %temp%\ftprecive.dat >nul 2>nul
pause
I was trying to delete all *.txt files of these both directories, but the script says that i can't change to "dir2/TUN2/"
What's wrong?

Send files to a ftp server and then check if it was really sent using .bat

I was making a .bat code to send .txt files to a specific folder on my ftp server. But I also wanted a way to check if these files were really uploaded. I searched a lot on the internet and unfortunately, I realized that it can't be done using .bat command.
So I tried using another way: The .bat will send the files and then will take it back to the folder, if the file already exists in the folder it will show a message that the file was successfully uploaded.
I did this script below, but the part of the "checking if was uploaded" is not working right.
Someone can help me?
#echo off
#setlocal enableextensions
#cd /d "%~dp0"
mode 34,12
color 0a
Ping www.google.nl -n 1 -w 1000 >nul 2>nul
if errorlevel 1 (set internet=Nao foi possivel se conectar ao servidor) else (set internet=Conectado) >nul 2>nul
echo %internet%
if "%internet%"=="No connection" goto 1
if "%internet%"=="Conected" goto 2
:1
echo No connection
echo.
echo Try later...
echo.
pause
exit
:2
( echo open ftp.xxxxxxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mput *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend.dat >nul 2>nul
del /f /s /q %temp%\ftpsend.dat >nul 2>nul
( echo open ftp.xxxxxxx.com
echo xxxxxxx
echo xxxxxxx
echo ascii
echo lcd "c:\Vendas Pay&Go\files"
echo cd "Vendas Cartões Pay&Go"
echo cd "ECO"
echo mget *.txt
echo bye
)> %temp%\ftpsend.dat
ftp -i -s:%temp%/ftpsend2.dat >nul 2>nul
del /f /s /q %temp%\ftpsend2.dat >nul 2>nul
if %*.txt% exist goto3
:4
echo File was not uploaded
pause
:3
echo File Uploaded.
del /s /f /q "c:\Vendas Pay&Go\files\*.txt"
If you want to conditional execution to display a message you can do something like this. The && means the previous command was successful. The || means the previous command was not successful.
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (echo File Successfully Transferred) || ( echo File not transferred. Tray again later.)
Updated version based on comment below
(ftp -i -s:%temp%/ftpsend.dat | find /I "file successfully transferred" >nul) && (
echo File successfully sent.
del /f /s /q "c:\Vendas Pay&Go\files\*.txt" >nul
) || (
cls
echo File not uploaded. Try later.
)

Batch File Commands to Remove File from Removable Drive (virus)

I don't know how to code myself, just managed to find some from Google and try to compile it. But it didn't work as I expected. Recently I've found that there is a virus spreading among my place without the user realizing it. The batch file is supposed to remove the files from the specified drive's %temp% folder with the extension .exe with exactly 138784 bytes. This the batch file I came up with, it works for removing it from the temporary folder but not for removing the file on their removable drive:
#ECHO OFF
ECHO "Enter Drive letter"
set /p letter=
for /r %%f in (*.exe) do if %%~zF EQU 138784 del %%F /f
attrib -s -h -a /s /d %letter%:*.*
c:
cd %temp%
Del wdr201.exe /f
ECHO "Process completed."
Pause
You can give a try for this code inspired from this one : Hackoo_Virus_Cleaner.bat
#echo off
Mode con cols=80 lines=6 & Color 9E
Title Searching the Drive letter of your USB Key and clean *.lnk files by Hackoo 2016
Set TmpLogFile=%tmp%\TmpLog.txt
Set "LogFile=%UserProfile%\Desktop\Hackoo_Virus_Clean_%UserName%_Log.txt"
If Exist %TmpLogFile% Del %TmpLogFile%
If Exist %LogFile% Del %LogFile%
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=2" ^|find /i ":"') do (Set MyUSB=%%i)
cls
setlocal ENABLEDELAYEDEXPANSION
set _drive=%MyUSB%
If Exist !_drive! (
cls
echo.
echo #########################################################
echo Your usb key is connected as !_drive!
echo #########################################################
echo Hit any key to remove malicious files and unhide files ...
pause>nul
Cls
echo(
Echo Removing malicious files/unhiding files... Please wait, this may take a while...
del /s /f /q !_drive!\*.lnk>>"%TmpLogFile%"2>&1
Cmd /U /C Type "%TmpLogFile%" > "%LogFile%"
Start "" %LogFile%
attrib -s -h -a -r /s /d !_drive!\*.*
Explorer "!_drive!\"
) ELSE (
cls
color 0C
echo.
echo #########################################################
echo Your usb key is not detected
echo #########################################################
echo.
)
pause & exit

Batch script : for looping on my local directory instead of my remote directory

I'm writing some Batch code and I'm in front of a problem.
I'm working on it since 2 days, I don't understand why my loop 'For' doing this.
The batch file is launch by C# process with 3 args
Here is my code :
set lastname=%1
set firstname=%2
set init=%3
echo open my_remote_add>info.ftp
echo user>>info.ftp
echo pwd>>info.ftp
echo cd "dir/subdir/subsubdir/subsubsubdir" >> info.ftp
echo lcd C:\DIR\SUBDIR>>info.ftp
echo prompt>>info.ftp
echo mput *.e*>>info.ftp
set directory=dir
echo %directory%>>info.ftp
::##My probleme is here ##
for /f %%f in ('%directory% /b *.e*') do echo %%f>>info.ftp
::## %directory% show my remote dir, but echo %%f show locals files name with extension .e* instead of remotes directory files name##
echo bye >> info.ftp
ftp -s:info.ftp
pause`
I've no idea why it's looping on my local directory instead of my remote directory.
If someone have some idea ?
Thanks.
Regards,
David
EDIT :: Problem solved
Hi.
I totally rebuild my mind.
The problem was : dir is the WorkingDirectory I set on my C# exe.
var proc = new ProcessStartInfo();
proc.FileName = #"C:\DIR\SUBDIR\SUBSUBDIR\exportFTP.bat";
proc.WorkingDirectory = #"C:\DIR\SUBDIR";
proc.Arguments = String.Format("{0} {1} {2}", str2, str3, str4);
Process.Start(proc);
After that, i thought it was easiest to copy files I wanted to transfert in a temp directory and run my script on it.
Here is my alternative code :
#echo off
set lastname=%1
set firstname=%2
set init=%3
set RemotePath=remotedir/remotesubdir/remotesubdir/remotesubdir
mkdir temp
set LocalPath=C:\DIR\SUBDIR\temp
for /f %%f in ('dir /b *.e0*') do copy %%f temp\%lastname%_%firstname%_%init%_%%f>>info.ftp
echo open remote_adress>info.ftp
echo user>>info.ftp
echo pwd>>info.ftp
echo lcd %LocalPath%>>info.ftp
echo cd %RemotePath% /d>>info.ftp
echo prompt>>info.ftp
echo mput *.e0*>>info.ftp
echo bye >> info.ftp
ftp -s:info.ftp
del info.ftp
rmdir /s /q temp
move /y "C:\DIR\SUBDIR\*.e0*" "C:\DIR\SUBDIR\SUBDIR\"
Thanks again #Compo
Just try changing CD to CD /D:
#Echo Off
set lastname=%1
set firstname=%2
set init=%3
set directory=dir
>info.ftp (
echo open my_remote_add
echo user
echo pwd
echo cd /d "dir/subdir/subsubdir/subsubsubdir"
echo lcd C:\DIR\SUBDIR
echo prompt
echo mput *.e*
echo %directory%
echo for /f "delims=" %%f in ('%directory% /b *.e*') do echo %%f
echo bye)
ftp -s:info.ftp
pause
Timeout -1

Batch file that creates another batch file

I am trying to make a batch file that will assist with mapping a drive at startup
Basically it will write a batch file to the startup folder of the computer it is run on.
#Echo Off
cls
title Set Network drive 1.0
color 0A
rem "rem" is a message that can only bee seeing when editing a batch file. It will not be includded when running a batch file.
rem =====saving log file to your desktop==========
set log="%USERPROFILE%\Desktop\Set Network Drive 1.0.log"
echo.>>%log%
echo Starting log at %date% %time%>>%log%
echo.>>%log%
rem ==================Detecting OS type===========================
echo.
echo =====================Detection OS type=================
echo =====================Detection OS type=================>>%log%
echo.
echo.>>%log%
color 0c
echo One second please.......
echo.
for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
for /f "tokens=3 delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
rem ===========Setting OS type to use as variable %variable%=====
if "%os_name%"=="XP" set verssion=xp
if "%os_name%"=="7" set version=7
if "%version%"=="XP" set desktop=xp
if "%version%"=="7" set desktop=7
cls
color 0A
echo.
echo Operating system detected.............. Windows %version%
echo Operating system detected.............. Windows %version% >>%log%
echo.>>%log%
rem ===============Gathering information=========================
echo ===============Drive information==============>>%log%
echo.
echo.>>%log%
set /p driveletter=Drive letter to assign to:
echo Drive Letter................................ %driveletter%>>%log%
echo.>>%log%
echo.
set /p IP=Ip address device:
echo IP address of remote device................. %IP%>>%log%
echo.>>%log%
echo.
set /p path=Path on remote machine:
echo Path on remote machine set to............... %path% >>%log%
echo.>>%log%
echo.
set /p Username=Username (will be viewable in the log file and batch file):
echo Username.................................... %username%>>%log%
echo.
echo.>>%log%
set /p pass=Password (will be viewable in the batch file):
echo Password.................................... **********>>%log%
echo. >>%log%
echo ============================================================>>%log%
echo.>>%
rem ====================================================================
rem ====Writing batch file to startup folder as Map %driveletter%.bat===
rem ========windows 7==================
:win7
set startup="C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\Map %driveletter%.bat"
pause
echo #echo off >>%startup%
echo title Mapping network drive %driveletter%:>>%startup%
echo color 0A>>%startup%
echo cls>>%startup%
echo echo.>>%startup%
echo echo Wating for connection to drive location.........>>%startup%
echo goto loop>>%startup%
echo.>>%startup%
echo :loop>>%startup%
echo.>>%startup%
echo echo Pinging %ip%.........>>%startup%
echo cd c:\windows\system32\>>%startup%
THIS IS WHERE I HAVE THE PROBLEM. This command will not output to the batch file its creating and it shuts down this batch file
echo ping %ip% -n 1 | find "TTL">>%startup%
echo if not errorlevel 1 goto up>>%startup%
echo if errorlevel 1 goto loop>>%startup%
echo.>>%startup%
echo.>>%startup%
echo :up>>%startup%
echo cls>>%startup%
echo echo.>>%startup%
echo echo Device is up>>%startup%
echo echo.>>%startup%
echo echo Mapping drive......>>%startup%
echo c:\windows\system32\net use %driveletter%: \\%ip%\%path% %pass% /USER:%username% >>%startup%
echo if not errorlevel 1 goto up>>%startup%
echo if errorlevel 1 goto end>>%startup%
echo echo.>>%startup%
echo :end>>%startup%
echo echo Drive was mapped>>%startup%
echo echo.>>%startup%
rem ==========REMOVE THE FOLOWING LINE TO HAVE THE BATCH FILE GO AWAY WHEN COMPLETE======
echo pause>>%startup%
start notepad %log%
pause
#echo off
echo #echo off>t.bat
echo echo test complete>>t.bat
echo quit>>t.bat
This basically is a batch code which creates another batch file t.bat in the present working directory. You can direct the output file ( batch file created) to the startup folder using cd command. The >> operator echoes text to the next line of the output file.

Resources