Page 1 of 1

string concatenation in a for loop

Posted: 09 Jun 2010 12:48
by yarnseeker
I'm trying to build an FTP script where I don't have to store the userid and password. The below worked just fine EXCEPT that I now want to be able to transmit like 20+ files.

viewtopic.php?f=3&t=1078&p=3695&e=3695

I thought I'd save the file commands separately and use the -s option but that won't work as it doesn't log me on before it executes the script.

So then I thought I'd create a string of echo commands into a varaible and pass that but this isn't working. The echo that is inside of the for loop doesn't work. I wanted to do "set xx=%xx% %%G" so that I could concatenate a bunch of commands together but it doesn't appear to be retaining the value of xx between executions inside of the for loop

for /f "tokens=* delims=" %%G in (sourceftp.bat) do (
set xx=%%G
echo %xx% >>file.txt (this just gives me ECHO is on so that must mean xx is empty)
)
echo %xx% >>file.txt (this does give me the very last line of my input file)
pause

Is there another way to write a file w/o redirection? Is there a better way to concatenate my ftp statements into 1 long command string?

Re: string concatenation in a for loop

Posted: 09 Jun 2010 13:19
by aGerman
Regular variables never change into the same (multiline) command line.
Two (of some more) possibilities:

- setlocal EnableDelayedExpansion (!...!)

Code: Select all

setlocal EnableDelayedExpansion

for /f "tokens=* delims=" %%G in (sourceftp.bat) do (
  set xx=%%G
  echo !xx! >>file.txt
)



CALL trick (call ... %%...%%)

Code: Select all

for /f "tokens=* delims=" %%G in (sourceftp.bat) do (
  set xx=%%G
  call echo %%xx%% >>file.txt
)


Regards
aGerman

Re: string concatenation in a for loop

Posted: 09 Jun 2010 13:42
by yarnseeker
Well the call version worked except that I still cannot concatenate the text.

ascii
GET 'HLQ.TEST.SOURCE(QAHX5301)' QAHX5301.TXT
GET 'HLQ.TEST.SOURCE(QAHX5302)' QAHX5302.TXT
...

My file has the above. I wanted to concatenate the ftp commands into 1 variable. I tried sending a big bunch of commands using the & and it worked. I figured out how to read each line into a variable in a for loop but again, I cannot get those to concatenate together like I did in the below Set ZZ statement but I don't understand why.

---so this does not work
set xx=ASCII
set "yy=get 'hlq.download.data' "download.txt" "
set zz=echo %xx% & echo %yy%
(
echo open systemc
echo user userid
echo %zz%
)|ftp -n

-- but this does
set xx=ASCII
set "yy=get 'hlq.download.data' "download.txt" "

pause
(
echo open systemc
echo user userid
echo %xx% & echo %yy%
)|ftp -n


I guess I could put the commands into variables and use the delayed expansion and then on my echo command allow for a ton of variables. It just seems like there should be some way of concatenating all of this together instead of having to specify 1:n variables when I don't even know how many there will be.

for /f "tokens=* delims=" %%G in (sourceftp.bat) do (
Set /a n+=1
Set _var!n!=%%G
)

Re: string concatenation in a for loop

Posted: 10 Jun 2010 11:04
by jeb
Hi,

the problem is the line

Code: Select all

set zz=echo %xx% & echo %yy%


The & doesn't concatenate it will split the line into two commands
so it is equal to

Code: Select all

set zz=echo %xx%
echo %yy%


But the next problem is that in %zz% is now "echo ASCII", I suppose you only want the "ASCII" command.

Your next try with

Code: Select all

echo %xx% & echo %yy%

Expands also to two line

Code: Select all

echo %xx%
echo %yy%


You can concatenate your strings with

Code: Select all

set zz=%xx% %yy%

But then you have it on one line like <ASCII get 'hlq.download.data' "download.txt" >
I don't believe, that this work with the ftp.

Your idea with "many" (array like) variables seems to be a good way.
But...
You can't use DelayedExpansion in blocks, when you pipe the block.
Then the DelayedExpansion is disabled and you get something like

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set x[1]=ASCII
set x[2]=get something
(
  for /L %%c in (1 1 2) DO echo !x[%%c]!
 
) | more

result:
!x[1]!
!x[2]!


But with a workaround it could be use

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set x[1]=ASCII
set x[2]=get something
(
  for /L %%c in (1 1 2) DO @call echo %%x[%%c]%%
) | more

result:
ASCII
get something


jeb

Re: string concatenation in a for loop

Posted: 10 Jun 2010 12:01
by yarnseeker
the concatenation doesn't appear to work in a for loop.

this also doesn't appear to work. I want to be able to spool out any number of items but lordy dos doesn't appear to be very flexible and neither does ftp. fine for really simple things but looping constructs etc are very lacking.

@echo on
setlocal EnableDelayedExpansion
set x[1]=open sysb
set x[2]=userid
set x[3]=lcd "C:\Documents and Settings\%username%\Desktop"
set x[4]=get download.data "download.txt"
set x[5]=put "upload.txt" upload.data

(
for /L %%c in (1 1 5) DO @call echo %%x[%%c]%%
)|ftp -n

if think what's going to end up happening is that I would have to do something like the below and then x6 and x7 will just end up telling me that echo is on or off

set x[1]=open sysb
set x[2]=userid
set x[3]=lcd "C:\Documents and Settings\%username%\Desktop"
set x[4]=get download.data "download.txt"
set x[5]=put "upload.txt" upload.data
set x[6]=
set x[7]=

(
echo x[1]
echo x[2]
echo x[3]
echo x[4]
echo x[5]
echo x[6]
echo x[7]
)|ftp -n


Or the other choice is that I just give up and
1. I make the people put in all of the darn echo commands themselves
2. make a script that will combine the 'logon' aspect and the file list and spit out a new script and execute that puppy.

I don't understand why I can't tell the darn thing to prompt me for my userid and password before I run the script with the -s option but I guess that's just how ftp works :?

Re: string concatenation in a for loop

Posted: 10 Jun 2010 22:58
by amel27
via file:

Code: Select all

> "%~n0.tmp" echo open sysb
>>"%~n0.tmp" echo user id
>>"%~n0.tmp" echo lcd "C:\Documents and Settings\%username%\Desktop"
>>"%~n0.tmp" echo get download.data "download.txt"
>>"%~n0.tmp" echo put "upload.txt" upload.data

type "%~n0.tmp"|ftp -n

Re: string concatenation in a for loop

Posted: 11 Jun 2010 08:52
by yarnseeker
awesome! that worked perfectly. thanks so much ya'll :) :mrgreen: