Echo inside For Do loop?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Echo inside For Do loop?

Post by MigrationUser »

20 Nov 2007 23:46
zammalabe

why the second echo is not echoing???

Code: Select all

@echo off
for %%a in ("*.rar") do (
    set Folder1=%%~na
    title Installing the package of: %%a
    @echo computer name is: %computername%
    @echo catalog name is: %Folder1%
    pause
    )
set
pause
----------------------------

#2 21 Nov 2007 01:47
Simon Sheppard


From the FOR page...
https://ss64.com/nt/for.html

Environment variables within a FOR loop are expanded at the beginning of the loop and won't change until AFTER the end of the DO section.
The following example counts the files in the current folder, but %count% always returns 1:

Code: Select all

@echo off
SET count=1
 FOR /f "tokens=*" %%G IN ('dir /b') DO (
 echo %count%:%%G
 set /a count+=1)
To make this work correctly we must force the variable %count% to be evaluated during each iteration, using the CALL :subroutine mechanism:

Code: Select all

@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :s_do_sums "%%G")
GOTO :eof

:s_do_sums
 echo %count%:%1
 set /a count+=1
 GOTO :eof
----------------------------

#3 21 Nov 2007 01:57
zammalabe


ookei, i understand the problem and reformulate my question: how to use variable "Folder1" with the final value outside the for ... do ... loop?

Code: Select all

@echo off
for %%a in ("*.rar") do (
    set Folder1=%%~na
    title Installing the package of: %%a
    @echo computer name is: %computername%
    call :puux
    pause
    )

:puux
@echo catalog name is: %Folder1%
set Folder1_Current_Value=%Folder1%
goto :eof

:eof
i.e. can i now use %Folder1% outside the loop wiht it current value?

Last edited by zammalabe (21 Nov 2007 02:16)

----------------------------

#4 21 Nov 2007 02:02
Simon Sheppard


just change what you pass to the subroutine

so instead of
DO (call :s_do_sums "%%G")

you would have
DO (call :mysub "%%~na")

then define the variable in the mysub subroutine
set Folder1=%1

----------------------------

#5 21 Nov 2007 02:43
zammalabe


hehh ;) this code looks much better ;) thanks ;)

Code: Select all

@echo off
for /R %%a in (*.rar) do (call :puux "%%~na" & pause)

:puux
    set Folder1=%1
    title Installing the package of: %Folder1%
    @echo computer name is: %computername%
    @echo catalog name is: %Folder1%
    goto :eof

:eof
but, as filenames may contain spaces, i should use:

call :puux "%%~na"

but how to remove the quotes from output??? without quotes outside the passed wariable (> %%~na) there is shown only part of the filename (left of first space), with quotes outside the passed variable (> "%%~na") filename surrounded with quotes .... :/

Last edited by zammalabe (21 Nov 2007 02:44)

----------------------------

#6 21 Nov 2007 16:02
bluesxman

Code: Select all

set Folder1=%~1
would remove the quotes.

cmd | *sh | ruby | chef

----------------------------

#7 21 Nov 2007 23:05
zammalabe


thanks, bluesxman
Post Reply