1

I am tring to use variables within a batch file, where I have a config file that has multiple configurations, an example of my configuration file format is:

PROCESS=<ftpSiteURL> <username> <rd> <lcd>

where the data may be:

FTP=ftp.siteA.com asmith ftpFolder localFolder
FTP=ftp.siteB.com asmith ftpFolder localFolder
FTP=ftp.siteC.com asmith ftpFolder localFolder

From the question How to read file contents into a variable, I have determined that I can read each line from the file (ftp.txt), into a parameter, using:

for /f "delims== tokens=1,2" %%X in (ftp.txt) do (
    SET %%X=%%Y
)

I have also read How to split a string by spaces, from which I can split a string into the required variables:

for /f "tokens=1,2,3,4" %%a in ("ftp.one.com asmith ftpFolder localFolder") do (
    SET SITE=%%a
    SET USER=%%b
    SET REMOTE=%%c
    SET LOCAL=%%d
)

My questions:

I get nothing, if I ECHO the created variable FTP; trying:

%FTP
%%FTP
%FTP%
%%FTP%%

What is the correct syntax to ECHO the content of the FTP variable, as this needs to replace the hard-coded string in the second FOR LOOP?

Sean
  • 862
  • 8
  • 28
  • 1
    the correct way to reference a variable is `%FTP%` - unless you set *and* use it within a codeblock (enclosed in parentheses), then you have to use [delayed expansion](https://stackoverflow.com/a/30284028/2152082) and `!FTP!` (like Compo already suggested) – Stephan Oct 10 '19 at 18:25
  • 1
    Environment variables are (usually) referenced and expanded by `%FTP%`. Anyway, what do you want to achieve? do you want to execute `PROCESS` using the arguments ` ` one after another? I am asking because I have a suspicion this question might constitute an [XY Problem](http://xyproblem.info/)... – aschipfl Oct 10 '19 at 18:26
  • For simplicity sake, assume that I will connect to the FTP site (from a command line) using the USER and download the files from the FTPFOLDER to the LOCALFOLDER. We have a lot of FTP sources to download from and are looking to have one script, referencing one parameter file to loop the lot, rather than having loads of separate scripts. – Sean Oct 10 '19 at 19:32

1 Answers1

0

Thank you @Compo and @aschipfl for the comments. Indeed, swiyching to !var! and addding EnableDelayedExpansion has fixed it all.

Working code:

setlocal EnableDelayedExpansion

ECHO ***READ THE CONFIG FILE**
for /f "delims== tokens=1,2" %%X in (ftp.txt) do (
    set %%X=%%Y
    ECHO CONFIG LINE is !FTP!

    ECHO **SPLIT THE LINE INTO PARAMETERS**
    for /f "tokens=1,2,3,4" %%a in ("!FTP!") do (
        SET SITE=%%a
        SET USER=%%b
        SET REMOTE=%%c
        SET LOCAL=%%d
        ECHO Connect to !SITE! as !USER! to download from !REMOTE! to !LOCAL!
    )
)
Sean
  • 862
  • 8
  • 28
  • You know, you can use the `%%X` variables directly? `echo Connect to %%a as %%b to download from %%c to %%d` – Stephan Oct 10 '19 at 20:18
  • For the first `for` you can also use `for /f "delims=" %%x in (ftp.txt) do set "%%x"` – Stephan Oct 10 '19 at 20:19
  • For the "connect" one, I preferred to SET the variables, as I have a whole bunch of code/logic to write for the FTP connections and variables will make is easier to read/maintain, but I agree about simplifying the first FOR as that's only being used the once. Also,once completed, the script and config will be maintained by people who may be even less technical than me, so clarity will help them. – Sean Oct 10 '19 at 20:31
  • sounds like a bunch of [REM](https://ss64.com/nt/rem.html) commands would be helpful to them. – Stephan Oct 10 '19 at 20:48