Why is this batch file looping?

Please help:
I only wan this action to happen once but it echos Hi mutiple times then movesback to then next line in the firs file. I know I need to do something w/ and end statement or :eof but i cannot get themin the right place.


for /f %%A in (.\comp.txt) do set path=%%A & call :step1
pause
end

:Step1
for /f %%B in (.\user.txt) do set dir=%%B & call :step2

:step2
echo Hi
dpoussonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
You need a goto :eof at the end of your subroutine and before the start of any subs, i.e.

for /f %%A in (.\comp.txt) do set path=%%A & call :step1
pause
REM This is the end of the batch file, goto the end of file now
goto :eof

:Step1
for /f %%B in (.\user.txt) do set dir=%%B & call :step2
REM this is the end of Step1 sub
goto :eof

:step2
echo Hi
REM this is the end of the step 2 sub -- or it would drop to the end of the batch and work anyway
goto :eof

Not sure what you are trying to do specifically with the for loops but ask if not sure on any of it.

hth

Steve
0
dpoussonAuthor Commented:
thanks that is very close to what I think I need but it is still executing the commands in step 2 or :IQ2 2 times before it continues to the next line in the first file. That first script was just a simple example....here is the actual script:

set serverpath=D:\Users\Home
set folder=ArchiveMail
set pcprofile=c$\Documents and Settings
set pst=Local Settings\Application Data\Microsoft\Outlook\*.pst
set NK2=Application Data\Microsoft\Outlook\*.NK2
set DesktopPST=Desktop\*.pst
set MyDocsPST=My Documents\*.pst

FOR /F %%A IN (.\compnames.txt) DO set pcpath=%%A& Call :IQ1
pause
goto :eof


:IQ1
FOR /F %%B IN (.\users.txt) DO set sharepath=%%B& Call :IQ2
goto :eof
:IQ2
xcopy /y /v "\\%pcpath%\%pcprofile%\%sharepath%\%pst%" "%serverpath%\%sharepath%\%folder%\"
xcopy /y /v "\\%pcpath%\%pcprofile%\%sharepath%\%NK2%" "%serverpath%\%sharepath%\%folder%\"
xcopy /y /v "\\%pcpath%\%pcprofile%\%sharepath%\%DesktopPST%" "%serverpath%\%sharepath%\%folder%\"
xcopy /y /v "\\%pcpath%\%pcprofile%\%sharepath%\%MyDocsPST%" "%serverpath%\%sharepath%\%folder%\"

goto :eof
0
t0t0Commented:
There are a couple of things I ought to point out to you..

Firstly, you're setting the variable 'path'. If you are CERTAIN this is what you intend to do then fine however, you should be aware PATH is a system-wide environment variable and is best left alone. For this reason, it might be better to use PTH instead as in the code below.

Secondly, merely ECHOing 'Hi' is a pretty poor way to test your batch file. For this reason I have changed this to: 'ECHO %pth% %dir%' at least this way we can see what's happening with the variables too - and not just the logical flow of the batch file.

Thirdly, there is much contention over the use of 'GOTO :EOF' as opposed to 'EXIT /B'. It may help you to read the following article: http://www.robvanderwoude.com/exit.php

I prefer to use 'EXIT /B' as it enable me to return an ERRORLEVEL value.

In you code, you must end each sub-routine otherwise program control runs on to the next line of code - and quite possibly into another sub-routine as in your case.

Finally, the 'PAUSE' has no effect whatsoever other than to pause exiting the actual batch file. In your code, the 'PAUSE' will NOT pause inbetween iterations of the first FOR loop because it lies outside of the FOR's body.

To pause  iterations of the first FOR loop you would need to do the following:

   for /f %%A in (.\comp.txt) do (
      set pth=%%A
      call :step1
      pause
   )
   exit /b

or, if you're hell-bent on using compound statements then the following is the equivilent:

   for /f %%A in (.\comp.txt) do set pth=%%A & call :step1 & pause
   exit /b

The modified code below now runs correctly:

   @echo off
   for /f %%A in (.\comp.txt) do set pth=%%A & call :step1 & pause
   exit /b

   :Step1
   for /f %%B in (.\user.txt) do set dir=%%B & call :step2
   exit /b

   :step2
   echo %pth% %dir%
   exit /b

To close this discussion, be warned about using compound statements. Their behaviour can sometimes be undesirable. An example of this is in the following two lines of code. Try to predict what the output will be, then try it out for yourself:

   set name=dpousson
   set name=t0t0 & echo %name%
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

t0t0Commented:
See the correct method to do what you're trying to do:

@ECHO OFF
SET ServerPath=D:\Users\Home
SET Folder=ArchiveMail
SET PcProfile=c$\Documents and Settings
SET Pst=Local Settings\Application Data\Microsoft\Outlook\*.pst
SET NK2=Application Data\Microsoft\Outlook\*.NK2
SET DesktopPST=Desktop\*.pst
SET MyDocsPST=My Documents\*.pst

FOR /F %%a IN (.\CompNames.txt) DO (
   FOR /F %%b IN (.\Users.txt) DO (
      XCOPY /Y /V "\\%%a\%PcProfile%\%%b\%Pst%" "%ServerPath%\%%b\%Folder%\"
      XCOPY /Y /V "\\%%a\%PcProfile%\%%b\%NK2%" "%ServerPath%\%%b\%Folder%\"
      XCOPY /Y /V "\\%%a\%PcProfile%\%%b\%DesktopPST%" "%ServerPath%\%%b\%Folder%\"
      XCOPY /Y /V "\\%%a\%PcProfile%\%%b\%MyDocsPST%" "%ServerPath%\%%b\%Folder%\"
   )
)
PAUSE
0
dpoussonAuthor Commented:
very very nice, thank you so much. I learned a ton form u as you can tell I am a beginner and just do things the only way I know how.

This still does not quite work right. It is mixing the files instead of keeping coorelating lines of the 2 .txt files together. I wan tit to pull files from a supplied pc name from the users profile then put it on in the destination directory then go the next pc and username and keep it all separte.

Thanks again
0
dpoussonAuthor Commented:
here is an example to test. I zipped up my files. it is copingy file1 and file2 to user1 and user2, but I want it to copy file1 to user1 and file2 to user2 andd not both to both

rename Copytest.cmd.txt to Copytest.cmd
thanks
Dev.zip
0
Steve KnightIT ConsultancyCommented:
<<See the correct method to do what you're trying to do:>>

There is NO correct way to do programming.  A correct way is one that works and is within the experience of the person writing it.  There are better ways or worse ways but if it works, it works.

Anyway you have some nice solutions there so will leave you with t0t0.

Steve
0
t0t0Commented:
dragon-it

The 'correct' method (above) refers to the nesting of FOR loops to achieve what the asker was attempting to do using CALLs. I wasn't implying it's the 'correct' solution to the asker's question.



0
t0t0Commented:
I ran the code from your zip file. Below is the results. Please state if this is not what you want, and if possible please state (given the BEFORE state) how you want the AFTER state to look like.
   BEFORE     |
              |----DOCS
              |     |----DIR1
              |     |     '----FILE1.TXT
              |     '----DIR2
              |           '----FILE2.TXT
              '----HOME
 
 
 
   AFTER      |
              |----DOCS
              |     |----DIR1
              |     |     '----FILE1.TXT
              |     '----DIR2
              |           '----FILE2.TXT
              '----HOME
                    |----USER1
                    |     |----FILE1.TXT
                    |     '----FILE2.TXT
                    '----USER2
                          |----FILE1.TXT
                          '----FILE2.TXT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t0t0Commented:
Please tend to this question,

Thank you.
0
t0t0Commented:
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.