Jump to content

Batch nested for loop trouble


Recommended Posts

Quick query, I know exactly what I want to achieve and I feel like i'm only one step away from doing that. Essentially my code is trying to find all the terminal.exe and for each terminal.exe load and pass the variable %b only once.

For example it should basically go if there is three terminal.exe's found and 10 directories found in the folder called 'profiles'

\pa\terminal.exe "55-'pony1'

\mo\terminal.exe "55-'turtle1'

\ma\terminal.exe "55-'horse1'

and then leave it at that.

So far I have this

for /F "tokens=*" %%a in ('dir /b /s terminal.exe') do (for /F "tokens=*" %%b in ('dir /ad /b profiles') do echo "%%a" "55-'%%b'")

Which loads EACH terminal.exe 10 times with that parameter, and then goes on to open the next terminal.exe another 10 times. I assume I need to do something with delayedexpansion but I don't know in this instance.

Link to comment
Share on other sites


Quick query, I know exactly what I want to achieve and I feel like i'm only one step away from doing that.

I have NO doubt you know exactly what you would like to achieve :), problem is that you seemingly completely fail in explaining it to us. :ph34r:

WHY do you need to load terminal.exe 10 (or any other number of) times?

WHAT is terminal.exe?

jaclaz

Link to comment
Share on other sites

Both the number of exe's and profiles vary, hence the need to make a list out of them.

With the code I have it loads each exe by the number of profiles, so I end up with 10 instances of the same profile on each exe.

terminal.exe1 profile1

terminal.exe1 profile2

terminal.exe1 profile3

terminal.exe1 profile4

terminal.exe2 profile1

terminal.exe2 profile2

terminal.exe2 profile3

terminal.exe2 profile4

That is not the behaviour I want, I need it to be this.

terminal1.exe1 profile1

terminal1.exe2 profile2

terminal1.exe3 profile3

Does that help explain it?

Link to comment
Share on other sites

It helps a little, but in order to provide a real world solution we require real world script location, real world paths, real world file names and real world operating systems etc.

I'd also like to see the entire script you intend running before I provide a full example for you to attempt to break down and insert into something else. This is one of those questions which I see often where somebody expects simple solution to a what they believe is a simple problem, but that solution is invariably far from simple!

Link to comment
Share on other sites

What i've pasted is the 'entirety' of the script. It isn't anything fancy, all it does is take a list of all the terminal.exe's found in a certain directory and then runs it using a list of paramaters scraped from folder names from another directory.

I just need them to pair up ONLY once, not every permutation possible which is what it is currently doing.

I have no problem with the making of the list, locating them or passing them to the variables. I am having trouble specifically with each variable iteration being paired up multiple times as I said in my previous post.

If it helps in general the number of exe will always match the number of profiles. the profile scraping is passed to the terminal.exe as a paramater, that's all it does nothing else. All it does is tell terminal.exe to start up with a specific profile is all. I need however to run ALL terminal.exe's for testing purposes.

I just need each FOR loop to iterate once before looping again according to the number of terminal.exe's, as that is how many times I need the for loop run

Link to comment
Share on other sites

I understand what you require, however you have failed to attempt to answer any of the questions I asked!

It should be obvious to you that nesting a loop within a loop is your problem, (each instance of terminal.exe is being run against each each profile).

Suggesting an equal number of executables with profiles will not really help make any difference when designing a solution

Link to comment
Share on other sites

I'll try again.

terminal.exe1 profile1
\pa\terminal.exe "55-'pony1'

Are "vague".

This:

'dir /b /s terminal.exe'

is vague (because WE DO NOT KNOW what is current directory when the script is executed).

Example of a real world info (faked):

  • I have a directory in drive C:\ called "profiles", i.e. C:\profiles\.
  • Inside it there are a number of directories, one for each profile, like C:\profiles\profile1\, C:\profiles\profile2\ ... C:\profiles\profilen\
  • I have in another drive, drive D:\, another directory called "whatever", i.e. D:\whatever\.
  • Inside it there are a number of directories, one for each flabbergasty :w00t:, like D:\whatever\F1\, D:\whatever\F2\,... D:\whatever\Fn\
  • Inside these latter directories there may be (or there may be not) a program called "terminal.exe" (which I won't reveal what is used for).
  • I want to write a cmd, that will reside in C:\profiles and that once double clicked (or if run from command prompt once set current dir to ... ) should .....
  • etc.

You should have got the idea. :)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

I'm not a programmer of any sorts really, I write the odd thing up to automate some things I find repetitive and tedious so sometimes it is a struggle.

I assumed nesting for loops is what needed to be done here, if you are saying that is instantly the wrong way to go about it then I don't really know how else to achieve it.

I'm not sure why the 'current directory' is important.

I don't use the full path in my script but can easily give it to you, recursing subfolders is a preference.

dir /b /s D:\Desktop\MT4 Testing\New folder\terminal.exe

dir /b /ad D:\Desktop\MT4 Testing\Profiles

Link to comment
Share on other sites

I'm sure that either myself or jaclaz will be capable of providing you with help on yuor specific problem.

However it is not a generic problem requiring a generic solution, it is a specific one which requires your specific information in order to formulate a specific solution.

Link to comment
Share on other sites

For reference sake I found a workaround, the script will basically change the directory to where it found terminal.exe and extract the actual folder name to %b

I just wrote a different script where I md %%a to a folder so I don't have to rename everything and it is done for me. Perhaps one too many steps but at least it is 'done'.

Also I got the "extract current folder name" from one of Yzowl's old posts. Just wish I could get it to work based off a list of names taken from the directory names instead of this.

for /F "tokens=*" %%a in ('dir /b /s "D:\Desktop\Forex\MT4 Testing\terminal.exe"') do (cd %%~dpa & FOR %%b IN (.) DO @echo "%%a" "55-'%%~nb'")

Link to comment
Share on other sites

As stated before, we can only help you if you provide actual details.

That means bt providing us with real world file/folder tree structures, (not by creating made up folders on the desktop), informing us of the running location of the script and by giving us the operating system etc.

If you fail to provide this information, on your next post, I will close the Topic.

Link to comment
Share on other sites

The script is is run from the MT4 Testing folder, I just either run it from the command line or a batch file but it is based in the MT4 Testing folder.

The other folders are as above, they aren't "made up" they are what is there as I said. I use my desktop as a working folder.

Windows 7 is the OS.

A thought occured, if you are wondering about the D:\Desktop it is because I registry edited the desktop location to point there, I like to have as little on C: as humanly possible.

Link to comment
Share on other sites

You should have got the idea. :)

No, you haven't. :no:

We are NOT (yet) mind readers, we also have a limited amount of time of our life dedicated to helping other people on boards.

We are very keen to try and contribute to solutions to problems that are EXPLAINED and DETAILED, as opposed to be forced to GUESS:

  • your directory structure
  • your intended GOAL (as opposed to the way you think it should be reached)
  • what the heck are the parameters you wish to pass to the called app, the "vague"
    "55-'horse1'
    including the "queer" quotres you put around them

You are likely on the verge of falling into a chocolate covered banana situation:

http://homepages.tesco.net/J.deBoynePollard/FGA/put-down-the-chocolate-covered-banana.html

Though you are actually in the "I'm ill, doctor. Help!" one, having failed to follow the standard lithany:

http://homepages.tesco.net/J.deBoynePollard/FGA/problem-report-standard-litany.html

Put some logic at work.

By your own admission you are not a Batch programmer.

Here - at least one of us - is one (maybe two ;))

Now, what you initially posted:

For example it should basically go if there is three terminal.exe's found and 10 directories found in the folder called 'profiles'

\pa\terminal.exe "55-'pony1'

\mo\terminal.exe "55-'turtle1'

\ma\terminal.exe "55-'horse1'

and then leave it at that.

makes NO sense, even reading attentively between the lines .

HOW do you think we can imagine:

  1. HOW the "terminal.exe" in \pa\ has to be linked to profile "pony1" (if "pony1 is an actual "profile")
  2. HOW are the OTHER 7 profiles named ( since the 3 you listed are NOT in alphabetical order)
  3. HOW are the \pa\ \mo\ \ma\ ordered (seems reverse alphabetic)
  4. WHY CANNOT a single instance of terminal.exe (any of the three ones) be run once for each profile?

jaclaz

P.S.: Were cross-posting.

I'll try again.

Batch processing is automating something that you do manually.

If you do not give us the DETAILS of the SOURCE and the DETAILS of the RESULTS expected, it is VERY difficult to follow you.

In your video example, post one or more files AS THEY ARE and the corresponding result YOU WOULD LIKE THEM TO BECOME.

WHENEVER using a third party file/app post a link to where it can be found, please.

Edited by jaclaz
Link to comment
Share on other sites

Your 'workaround post' makes even less sense, it is suggesting that your actual profiles are the names of the directories containing each found terminal.exe.

This would mean that you would be looking at results such as:

<pathto>\pa\terminal.exe "55-'pa'"

<pathto>\mo\terminal.exe "55-'mo'"

<pathto>\ma\terminal.exe "55-'ma'"

not the provided pony1, turtle1 and horse1

Link to comment
Share on other sites

The very first post you guys classified as confusing because I gave made up folders, so I gave clearer examples and my actual folders in a later post. Also I missed out a double quotation mark in the first post. The single quotes are what terminal.exe requires, nothing to do with anything else except how it handles paramters.

The profile names are based on the folder name, it isn't a .ini or any sort of file. It is just how this program functions. Since my initial problem was nesting for loops that recursed each other, I just made it so I had one for loop instead because using both I didn't know how.

To see the other bit of code, which is a separate batch file

for /F "tokens=*" %a in ('dir /ad /b "MT4 Profiles"') do xcopy /c /e /h /i /y "D:\Desktop\Forex\MT4 Testing\Multi Profile Testing" D:\Desktop\"MT4 Testing"\"%a"

Where "D:\Desktop\Forex\MT4 Testing\Multi Profile Testing" is where all the terminal.exe program files are, each mt4 folder is identical to the other in terms of folder content

So my first post would now look like this <pathto>\pony 1\terminal.exe "55-'pony 1'" however all examples given below omit the extra single quotes and the 55- because that only has stuff to do with terminal and I know is fine.

I don't know how else to explain my first post other than for loop 1 results get paired to for loop 2 results only once

so the very first result in for loop 1 gets paired with the very first result in for loop 2.

The second result in for loop 1 gets paired to the second result in for loop 2

The profiles folder for loop results are

horse 1

pony 1

turtle 1

horse 2

pony 2

turtle 2

The terminal.exe folder for loop results are subdirectories found in D:\Desktop\Forex\MT4 Testing\ so look like

D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe

D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe

D:\Desktop\Forex\MT4 Testing\testing 3\terminal.exe

D:\Desktop\Forex\MT4 Testing\testing 4\terminal.exe

D:\Desktop\Forex\MT4 Testing\testing 5\terminal.exe

D:\Desktop\Forex\MT4 Testing\testing 6\terminal.exe

My desired results from this piece of code is

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "horse 1"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "pony 1"

"D:\Desktop\Forex\MT4 Testing\testing 3\terminal.exe" "turtle 1"

"D:\Desktop\Forex\MT4 Testing\testing 4\terminal.exe" "horse 2"

"D:\Desktop\Forex\MT4 Testing\testing 5\terminal.exe" "pony 2"

"D:\Desktop\Forex\MT4 Testing\testing 6\terminal.exe" "turtle 2"

What I get

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "horse 1"

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "pony 1"

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "turtle 1"

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "horse 2"

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "pony 2"

"D:\Desktop\Forex\MT4 Testing\testing 1\terminal.exe" "turtle 2"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "horse 1"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "pony 1"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "turtle 1"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "horse 2"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "pony 2"

"D:\Desktop\Forex\MT4 Testing\testing 2\terminal.exe" "turtle 2"

Results from my workaround, using the current directory as what is looked in

"D:\Desktop\Forex\MT4 Testing\horse 1\terminal.exe" "horse 1"

"D:\Desktop\Forex\MT4 Testing\pony 1\terminal.exe" "pony 1"

"D:\Desktop\Forex\MT4 Testing\turtle 1\terminal.exe" "turtle 1"

so on and so forth. The results I get with the for loops by themselves is accurate, just how they pair up is the behaviour I am having trouble with.

ok, to give another example of the same problem i'm having. Let's assume I have a folder with 3 files in it called file1 file2 file3 and three folders called dir1 dir2 dir3 and i'm running the below code from that folder

for /F "tokens=*" %%a in ('dir /b') do (for /F "tokens=*" %%b in ('dir /b /ad') do @echo "%%a" "%%b'")

What I get is

file1 dir1

file1 dir2

file1 dir3

file2 dir1

file2 dir2

file2 dir3

file3 dir1

file3 dir2

file3 dir3

When all I want is

file1 dir1

file2 dir2

file3 dir3

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...