Unable to upload to FTP using batch script - batch-file

I have problems to upload files to an ftp using command mput*. When I run the batch file it shows no file found. The filename have spaces.
#echo off
for /F "skip=1 delims=" %%F in ('
wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
') do (
for /F "tokens=1-3" %%L in ("%%F") do (
set CurrDay=0%%L
set CurrMonth=0%%M
set CurrYear=%%N
)
)
set CurrDay=%CurrDay:~-2%
set CurrMonth=%CurrMonth:~-2%
echo Current day : %CurrDay%
echo Current month: %CurrMonth%
echo Current year :%CurrYear%
set LOG=%CurrYear%%CurrMonth%.log
set FTPFolder=D:\FTP
echo %FTPFolder%
cd /D %FTPFolder%
ECHO.
#ECHO OFF
ftp -s:TryFTP.txt
ECHO.
ECHO ***** FTP transfer complete successfully! *****
pause
The TryFTP.txt
open Servername
user
password
lcd D:\FTP
cd /HTML
binary
mput *
bye
Error I get:
binary
invalid command
mput*
file not found
bye
invalid command
but if I try it manually using the same command it works. Can you guys please advise what wrong with it?

Related

ftp command to download file by date

I am using .bat script to access sftp and download file. The ftp folder consist of a lot of file which can be uniquely identify by the file name in format YYYYMMDD. eg: 20180307.csv, 20180306.csv etc. Currently I am using mget to get all the file, then at later filter it locally. How can I modify the script to get the file on today only?
This is my .bat script.
D:
CD\myTargetFolder
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
exit
This is my ftp script.
cd /myFTPSourceFolder
mget *.csv
bye
Thank you.
If you don't mind dynamically creating the ftp script then perhaps your .bat script could look like this:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined csv Set "csv=%%A%%B%%C.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
I've commented out the last line, remove the :: from it if you wish to delete the dynamically created ftp script.
Edit
To get yesterday's date csv then something like this, leveraging PowerShell, would be my recommendation:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('yyyMMdd')"`
) Do Set "csv=%%A.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"

remote pc>mapped drives to txt

my intent is to get this..
cls
setlocal enableextensions disabledelayedexpansion
echo Create backup txt file of mapped drives
echo -----
set /p PCname=Asset tag or IP address:
set /p UserID=UserID:
for /f "tokens=2,3 delims=," %%a in (
'wmic netuse get LocalName^, RemoteName^, Status /format:list ^| find ":"'
) do echo(%%a %%b>> \\%PCname%\c$\windows\temp\%PCname%_%UserID%_map.txt
xcopy /d/y \\%PCname%\c$\windows\temp\%PCname%_%UserID%_map.txt ".\remoteStats\"
pause
Goto :mdmenu
to map drives of a remote pc,%PCname%,to a text file with just the drive letter and path, copy that text file to the techs computer running the script into a .\remotestats\ folder
this way I can use that same text file to remap those drives automatically
:mdrestore
cls
echo Map Drives from backup txt
echo -----
echo Notes:
echo Needs a backup to restore from, inside
echo remoteStats folder
echo.
set /p PCname=Asset tag or IP address:
set /p UserID=Enter BH Username:
for /f %%a in (.\RemoteStats\%PCname%_%username%_map.txt) do Psexec \\%PCname% -d -s cmd /c net use %%a /p:Yes
psexec \\%PCname% -d cmd.exe /c net use S: \\mapped.drive\share /p:Yes
psexec \\%PCname% -d cmd.exe /c net use H: \\mappeddrive2\home /p:Yes
Print "Drives mapped"
Timeout /t 30 >nul
GOTO :mdmenu

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

FTP: get and delete a specific number of files

I'm trying to create a combination of batch files, with ftp commands to get and delete a specific number of files from a FTP directory.
Now I'm stuck and I get all the files but ofter, when there are more than 250 files in the directory, the batch stops and it's not possible to continue.
This is my actual situation, I have these 3 files:
FILE 01_FTP_GetFileList.bat
cd C:\folder
ftp -s:02_FTP_GetFileList.txt
C:\folder\03_FTP_MoveFiles.bat
FILE 02_FTP_GetFileList.txt
open xxx.xxx.xxx.xxx
user
password
hash
prompt off
cd folder
lcd folder
ls *.gz filelist.txt
bye
FILE 03_FTP_MoveFiles.bat
#echo off
setlocal enableextensions
setlocal enabledelayedexpansion
echo open xxx.xxx.xxx.xxx>>myscript
echo user user password>>myscript
echo prompt n>>myscript
echo ascii>>myscript
echo cd folder>>myscript
echo lcd C:\folder>>myscript
for /F "usebackq tokens=1,2* delims=," %%G IN ("C:\folder\filelist.txt") DO echo mget %%G>>myscript
for /F "usebackq tokens=1,2* delims=," %%G IN ("C:\folder\filelist.txt") DO echo mdelete %%G>>myscript
echo bye>>myscript
ftp -n -s:myscript
del filelist.txt
del myscript
To resume, the first file recall the 2nd and the 3rd and, as you can see in the code, I get and delete all the *.gz files in a specific directory after listing the files in that directory (this to avoid to delete file added after the copy).
Now I would like to copy only the first file of the list or... to list only 100 files in the folder, is the same by my point of view.
Thanks in advance!!
I do not see any counter variable in the post. The code below uses FILE_COUNT as the counter. It is incremented by the SET /A FILE_COUNTER+=1 statement. The FILE_COUNT is tested for being less than 100. See SET /?.
SETLOCAL ENABLEDELAYEDEXPANSION
SET MAX_FILES=100
SET /S FILE_COUNT=0
FOR /F "usebackq tokens=1,2 delims=," %%a IN (`TYPE "C:\folder\filelist.txt"`) DO (
IF !FILE_COUNT! LSS %MAX_FILES% (
echo mget %%a>>myscript
echo mdelete %%a>>myscript
)
SET /A FILE_COUNT+=1
)

Batch file (command line) to get target path of Internet Shortcut (.url) in the "URL:" field under "Web Document" tab

Batch file (command line) to get target path of Internet Shortcut (.url) in the "URL:" field under "Web Document" tab
I need the get the target from all shortcuts in a folder (desktop) and their respective destinations to help people at my computer repair shop defend against customers/clients re-infecting their computers by going to these web sites.
I have the code already to display (below) but I need to display the destination as well when it finds one (a .url shortcut file)
dir /b "%userprofile%\Desktop*.url"
-Thanks, -G
I think i understand your question, here is a batch file that will loop through the links in a given folder and then echo each of the destination URLs
Batch file:
echo off
setlocal enableextensions enabledelayedexpansion
pushd %1
for %%F in (%1\*.url) do (
echo %%~dpnxF
call :findurl "%%~dpnxF"
)
popd
goto end
:findurl inputfile
set url=
for /f "tokens=2 delims==" %%i in ('findstr URL %1') do set url=%%i
echo %url%
echo -----
:end
Usage:
MyBatchFile.bat C:\users\Username\Desktop
Expected Output:
d:\Users\Martyn\Desktop\test\link - Copy.URL
http://google.co.uk
-----
d:\Users\Martyn\Desktop\test\link.URL
http://stackoverflow.com/questions/17687358/batch-file-command-line-to-get-target-path-of-internet-s
hortcut-url-in-the
-----
[edit] Just a quick explanation on whats going on, .url files are basically just text files, open one in notepad to see. the URL is on the line URL=http://foo.com
this batch just reads each .url file in a directory and then echos everything on the URL line after "URL="
Hope that's what you meant!
Martyn
This format will also work.
#echo off
Setlocal Enableextensions
Setlocal Enabledelayedexpansion
for /r %%X in (*.url) do (
set shortcut="%%X"
echo SHORTCUT: !shortcut!
for /f "tokens=2 delims==" %%i in ('findstr URL !shortcut!') do (
set url=%%i
echo.
echo URL PATH: !url!
)
echo ----------------------------------------------------------------
echo.
)
:end

Resources