Reputation: 15
Currently I am trying to read the variable value from different text files available in my directly folder and use those variable to execute commands like uploading the content to FTP server.
I have different FTP servers on which I need to upload the same set of files. So what I want is to read the hostname, userID and password of different FTP servers which are saved in different text files and use those values to execute same set of commands for all.
Here is my FileUpload.bat looks like :-
@echo off
setlocal EnableDelayedExpansion
for %%x in (C:\Users\xxxx\Desktop\Anivirus_Upload\text_*.txt)
do (
for /f "usebackq delims=" %%A in ("%%~Fx")
do (
if "%%A"="user" set user=%%B
if "%%A"="password" set password=%%B
if "%%A"="localPath" set localPath=%%B
if "%%A"="serverpath" set serverpath=%%B
if "%%A"="Hostname" set Hostname=%%B
echo user %user%> ftpcmd.dat
echo %password%>> ftpcmd.dat
echo lcd %localPath%>>ftpcmd.dat
echo cd %serverpath%/>>ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat %Hostname%
del ftpcmd.dat
)
)
In the folder I have different text files namely
text_1
text_2
etc and each text file have values like :-
user=XXXX
Hostname=10.164.XXXXX
password=XXXXX
localPath=C:\Users\XXXX\Desktop\Service_Pack_74
serverpath=Temp/XXXX/Service_Pack_74/
I don't know where I am messing with the code while reading multiple text files.
I checked my batch file with single text file and I am able to upload the content correctly so seems the issue is with the external for loop to read multiple files from the directory.
Upvotes: 0
Views: 2604
Reputation: 79983
Many things amiss here.
First, the do (
must be on the same physical line as the for
.
Next, the delims
clause of the second for
loop should be delims=="
. With only 1 = delimiters are turned off, hence %%A
will be assigned the contents of the entire line and %%B
will be interpreted as the literal string %B
. AND to assign value to the metavariables, you need to add a tokens
clause specifying which tokens to assign.
Next, within a block (a parenthesised series of commands) any %var%
is replaced by the contents of the variable var
at the time the block is entered, not to any new value it adopts as the block is executed.
Next, the entire output and ftp section will be executed for each line of the file. Any output would depend on the value originally assigned to the variables you are using - possibly from a previous run.
@echo off
setlocal EnableDelayedExpansion
for %%x in (C:\Users\xxxx\Desktop\Anivirus_Upload\text_*.txt) do (
for /f "usebackq tokens=1* delims==" %%A in ("%%~Fx") do (
if "%%A"=="user" set user=%%B
if "%%A"=="password" set password=%%B
if "%%A"=="localPath" set localPath=%%B
if "%%A"=="Hostname" set Hostname=%%B
if "%%A"=="serverpath" (
set serverpath=%%B
echo user !user!> ftpcmd.dat
echo !password!>> ftpcmd.dat
echo lcd !localPath!>>ftpcmd.dat
echo cd !serverpath!/>>ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat !Hostname!
del ftpcmd.dat
)
)
)
Note here that the ftp
command file is built and executed using !var!
which accesses the changed value of the variables when delayedexpansion
is in effect.
I have no idea what you want in your ftp-put
command. As it is, the routine will substitute whatever is the first parameter to the batch file for %1
I'm also assuming that each variable is guaranteed to appear in each file, and that the serverpath
will be last in that file.
Upvotes: 1