FtpMoveHere

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fethelix
Posts: 2
Joined: 19 Apr 2017 01:27

FtpMoveHere

#1 Post by fethelix » 19 Apr 2017 02:21

Hey guys,

I'm having a problem with a batch script that I found on the internet which suits really good. When running from my Windows 7 Professional computer the script works like a charm. It opens a ftp connection to a server, copies all the files contained in the specified folder and deletes the files after copying.

But when running from Windows Server 2012 R2 Standard it won't work anymore.

The output with echo on is listed below.

Can anybody see why this is working on Windows 7 but not on Windows Server 2012?

Thanks in advance!!

Felix


Code: Select all

::FtpMoveHere.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
set "Fts=%~n0.Fts"   &:: name ftp-script like this file extension=fts
set "Ftl=%~n0.Ftl"   &:: name ftp-log like this file extension=log   
set "Ftf=%~n0.now"   &:: temp folder for files of this run
set "Lcd=%USERPROFILE%\Desktop\movehere" &:: local destination
set "Rcd=movehere"   &:: Remote Dir
set "Host=host"
set "User=user"
::set /P Pass=Enter password for Host %Host% User %User% :
set "Pass=pass"
if exist "%Ftf%" RD /Q "%Ftf% >NUL 2>&1
MkDir "%Ftf%"
DEl /Q %Fts% %Ftl% 1>NUL 2>&1

:: create and execute Ftp script to get files
Call :GenScript Get & FTP -v -i -s:"%Fts%" >"%Ftl%"
if errorlevel 1 echo Problem errorlevel %errorlevel% &goto :eof
Type NUL>%FTS%

:: create and execute Ftp script to delete remote files
Call :GenScript Del & FTP -v -i -s:"%Fts%" >>"%Ftl%"
Move "%Ftf%\*" "%Lcd%"
:: do cleanup
::Del /Q "%~n0.f*" >Nul 2>&1

::Pause

goto :eof
:GenScript Action ----------------------------------------
>>%Fts% echo open %Host%
>>%Fts% echo/%User%
>>%Fts% echo/%Pass%
>>%Fts% echo/
>>%Fts% echo cd %Rcd%
>>%Fts% echo/
if /I %1==Get (
>>%Fts% echo lcd %Ftf%
>>%Fts% echo/mget *.*
>>%Fts% echo/
) Else (
for %%A in (%Ftf%\*) do >>%Fts% echo Delete %%~nxA
>>%Fts% echo/
)
>>%Fts% echo bye
>>%Fts% echo/
:: For documentation append script to log file
>>"%Ftl%" echo %time% %1-Script ------------------------------------
Type "%Fts%" >> "%Ftl%"
Goto :eof
::FtpMoveHere.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::





Code: Select all

C:\Users\Administrator\Desktop\movehere>echo on  & setlocal EnableDelayedExpansion

C:\Users\Administrator\Desktop\movehere>set "Fts=FtpMoveHere2.Fts"

C:\Users\Administrator\Desktop\movehere>set "Ftl=FtpMoveHere2.Ftl"

C:\Users\Administrator\Desktop\movehere>set "Ftf=FtpMoveHere2.now"

C:\Users\Administrator\Desktop\movehere>set "Lcd=C:\Users\Administrator\Desktop\movehere"

C:\Users\Administrator\Desktop\movehere>set "Rcd=movehere"

C:\Users\Administrator\Desktop\movehere>set "Host=host"

C:\Users\Administrator\Desktop\movehere>set "User=user"

C:\Users\Administrator\Desktop\movehere>set "Pass=pass"

C:\Users\Administrator\Desktop\movehere>if exist "FtpMoveHere2.now" RD /Q "FtpMoveHere2.now >NUL 2>&1
Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnungist falsch.

C:\Users\Administrator\Desktop\movehere>MkDir "FtpMoveHere2.now"
Ein Unterverzeichnis oder eine Datei mit dem Namen "FtpMoveHere2.now" existiert bereits.

C:\Users\Administrator\Desktop\movehere>DEl /Q FtpMoveHere2.Fts FtpMoveHere2.Ftl  1>NUL 2>&1

C:\Users\Administrator\Desktop\movehere>Call :GenScript Get   & FTP -v -i -s:"FtpMoveHere2.Fts"  1>"FtpMoveHere2.Ftl"

C:\Users\Administrator\Desktop\movehere>echo open host 1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo/user>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo/pass>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo/1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo cd movehere 1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo/1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>if /I Get == Get (
echo lcd FtpMoveHere2.now 1>>FtpMoveHere2.Fts
 echo/mget *.* 1>>FtpMoveHere2.Fts
 echo/1>>FtpMoveHere2.Fts
)  Else (
for %A in (FtpMoveHere2.now\*) do echo Delete %~nxA 1>>FtpMoveHere2.Fts
 echo/1>>FtpMoveHere2.Fts
)

C:\Users\Administrator\Desktop\movehere>echo bye 1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo/1>>FtpMoveHere2.Fts

C:\Users\Administrator\Desktop\movehere>echo 10:01:41,34 Get-Script ------------------------------------ 1>>"FtpMoveHere2.Ftl"

C:\Users\Administrator\Desktop\movehere>Type "FtpMoveHere2.Fts"  1>>"FtpMoveHere2.Ftl"

C:\Users\Administrator\Desktop\movehere>Goto :eof

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FtpMoveHere

#2 Post by aGerman » 19 Apr 2017 04:19

There's at least a missing quotation mark
if exist "%Ftf%" RD /Q "%Ftf%" >NUL 2>&1

Steffen

fethelix
Posts: 2
Joined: 19 Apr 2017 01:27

Re: FtpMoveHere

#3 Post by fethelix » 19 Apr 2017 04:30

Hey Steffen,

thank you for your answer. Yoe are right, but sadly this did not convince the server to run the script correctly :(

Felix

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: FtpMoveHere

#4 Post by SIMMS7400 » 19 Apr 2017 05:31

Looks like your command file needs a tweak. Here's one of my FTP processes:

Code: Select all

@ECHO OFF
:: ------------------------------------------------------------------------
::-- Script Name: EXEC_FTP.cmd
::--
::-- Description: This script sets all required FTP commands in a temp
::                script file and passes it to FTP.exe for execution         
::--
::-- Parameters:  N^/A
::--       
::--       
::-- Author:      Joe Shmo   
::-- Date:        09/20/16
:: ------------------------------------------------------------------------

::-- Set Variables for FTP Process --::

   ::::::::::::::::::::::::::::::::FTP LEGEND::::::::::::::::::::::::::::::::
   :: FTP_SRVR   = Target Server                                           ::
   :: FTP_USER   = Target Server User Logon                                ::
   :: FTP_PSW    = Target Server Password                                  ::
   :: FTP_LCD    = Directory process starts or ends                        ::
   :: FTP_CD1    = Target directory the action will be executed on         ::
   :: FTP_CD2    = Target directory the action will be executed on         ::
   :: FTP_ACTION = FTP Command ^(i.e. get or put etc^)                     ::
   :: FTP_ITEM   = Object the action will be acting upon                   ::
   :: FTP_TEMPF  = Temporary FTP Command file - Deleted after execution    ::
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET FTP_SRVR=TZPDEV
SET FTP_USER=admin
SET FTP_PSWD=XParty0714
SET FTP_LCD=C:\Hyperion_Batch\Scripts\batch\FTP\
SET FTP_CD1=/home/essadmin/Hyperion_Batch/Logs/%date:~-4,4%_%date:~-10,2%%date:~-7,2%/
SET FTP_CD2=/home/essadmin/Hyperion_Batch/Errors/%date:~-4,4%_%date:~-10,2%%date:~-7,2%/
SET FTP_ACTION=mget
SET FTP_ITEM=*.*
SET FTP_TEMPF=%~n0.txt

::-- Variables are then used to create an FTP script file that is passed to FTP.exe for execution --::

FOR %%A in (%FTP_CD1% %FTP_CD2%) DO (
CALL :EXEC_FTP %FTP_SRVR% %FTP_USER% %FTP_PSWD% %FTP_LCD% %%A %FTP_ACTION% %FTP_ITEM% %FTP_TEMPF%
)

EXIT /B

:EXEC_FTP
::-- Generate FTP Commands File --::
::-- File needs to be passed to FTP.exe using -s switch --::
::-- Thus the reason for redirecting to a text file temporarily --::

ECHO open %1>%8
ECHO %2>>%8
ECHO %3>>%8
ECHO lcd /D "%4">>%8
ECHO cd "%5">>%8
ECHO binary>>%8
ECHO hash>>%8
ECHO %6 "%7">>%8
ECHO quit>>%8

::-- Launch FTP.exe and pass script file to it --::

FTP.exe -v -i -s:%8

::-- Delete FTP Commands File on Exit --::

DEL %8

GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- SAMPLE FTP Commands File that is generated above --::
::                                         ::
:: open TZPDEV                              ::
:: admin                                    ::
:: XParty0714                                                                                  ::
:: lcd /D "C:\Hyperion_Batch\Scripts\batch\FTP\"                            ::
:: cd  /home/essadmin/Hyperion_Batch/Errors/201609/                 ::
:: binary                                    ::
:: hash                                                                                             ::
:: get 20160922_RESTART_ESSBASE_SRVR.err                                ::
:: quit                                                                                               ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: FtpMoveHere

#5 Post by Compo » 19 Apr 2017 10:30

My assumption is that line 12 should read:

Code: Select all

IF EXIST "%Ftf%\" RD/S/Q "%Ftf%">NUL 2>&1
A directory specification should be followed by a back slash; especially if the said directory name has a . as it will look like a file name specification. I added the /S option to the RD command, for additional robustness.

Post Reply