Hi, I'm trying to write a batch file to login to a customer's FTP site and rename the first file. So far, I thought I'd rename all files and then work out how to just amend the first one, if possible. However, the routine gives varying results dependent on how many times I run it. The 3rd time of running, it gives the right text string for the RENAME but it has no effect. If I type the exact same command, it works. This is driving me mad. Pleased help.
Script is this:-
@Echo Off
REM - Important preparations
rem SETLOCAL ENABLEEXTENSIONS
rem SETLOCAL ENABLEDELAYEDEXPANSION
REM -- Define File Filter, i.e. files with extension .XML
Set FindStrArgs=/E /C:".XML"
REM -- Extract Ftp Script to create List of Files
Set "FtpCommand=ls"
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
rem Notepad "%temp%\%~n0.ftp"
REM -- Execute Ftp Script, collect File Names
Set "FileList="
For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%~n0.ftp"|Findstr %FindStrArgs%"') Do (
Call Set "FileList=%%FileList%% "%%A""
)
rem Notepad "%temp%\%~n0.ftp"
REM - Rename files
Set "FtpCommand=rename"
For %%A In (%FileList%) Do (
echo "A is "
echo %%A
echo %%~nxA
echo "oldname"
set "oldname=%%~nxA"
echo %oldname%
REM set newname=%oldname:XML=xml%
set "newname=A%oldname%"
echo "newname"
echo %newname%
Call Set "FtpCommand=%%FtpCommand%% "%%~A" "%newname%" "
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
)
pause
Notepad "%temp%\%~n0.ftp"
GOTO:EOF
:extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark
:: -- [IN] StartMark - start mark, use '...:S' mark to allow variable substitution
:: -- [IN,OPT] EndMark - optional end mark, default is first empty line
:: -- [IN,OPT] FileName - optional source file, default is THIS file
SETLOCAL Disabledelayedexpansion
set "bmk=%~1"
set "emk=%~2"
set "src=%~3"
set "bExtr="
set "bSubs="
if "%src%"=="" set src=%~f0& rem if no source file then assume THIS file
for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do (
if /i "%%B"=="%emk%" set "bExtr="&set "bSubs="
if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B)
if /i "%%B"=="%bmk%" set "bExtr=Y"
if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y"
)
EXIT /b
[Ftp Script 1]:S
!Title Connecting...
open xxx.xxx.xxx.xxx
xxxx
xxxx
!Title Preparing...
REM cd invoices
binary
hash
!Title Processing... %FtpCommand%
%FtpCommand%
!Title Disconnecting...
disconnect
bye
Results show this
1st run 2nd run 3rd run
-------- ----------- -----------
"A is "
"TEST1.XML"
TEST1.XML
"oldname"
ECHO is off. TEST1.XML TEST1.XML
"newname"
ECHO is off. A ATEST1.XML
Press and key etc...
FTP command
1st run rename "TEST1.XML" ""
2nd run rename "TEST1.XML" "A"
3rd run rename "TEST1.XML" "ATEST1.XML"
When I look in the directory, TEST1.XML is unchanged. There is no ATEST1.XML
Batch FTP rename
Moderator: DosItHelp
Re: Batch FTP rename
Try a script like this - http://www.biterscripting.com/helppages ... pload.html .
It doesn't do exactly what you are attempting to do. But it will give an idea of how to loop thru files within an FTP session from a batch script.
It doesn't do exactly what you are attempting to do. But it will give an idea of how to loop thru files within an FTP session from a batch script.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch FTP rename
try not to use batch to do such FTP tasks. Learn to use a programming language with an good FTP module, eg Perl and Net::FTP or Python and ftplib.