Jump to content

Need help "For" batch command


Recommended Posts

results in isofile[1] NOT DEFINED if the iso has not been found.

 

What is the problem? :dubbio:

If you want to set initially Counter to 0 (or to 42) is good as well, as long as you make the check for isofile[0] or for isofile[42].

 

 

Your code work fine if there is not ISO was found. But i want to ask you that in case we have 1 (or 2, 3 ...) then if we set counter=1, then if 1 ISO found it will return counter=2, if 2 ISO found it will return =3. I tested it before ask you. Tks

Link to comment
Share on other sites


Well, again, what's the problem?  You seem to think that "counter" is the number of ISOs found.  Don't think of it that way.  Counter is just a sort of placeholder that will be used for the index of the NEXT ISO that might be found.  So, if I understand the batch correctly, after the script has been run, counter-1 is the number of ISOs that have actually been found.

 

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

Tks for your reply, bhplpt.

 

counter-1 is the number of ISOs that have actually been found

 

 

 of course. Because from we begin, we set counter=1.

 

But since you say,

You seem to think that "counter" is the number of ISOs found. Counter is just a sort of placeholder that will be used for the index of the NEXT ISO that might be found

 

you're right. I think that (from my above post)  and i'm wrong.

echo there is/are %counter% ISO found on %path%

May Jaclaz was try to talk to me as you say. But he choose other way to explain to me and it make me quite difficult to understand

 

If you want to set initially Counter to 0 (or to 42) is good as well, as long as you make the check for isofile[0] or for isofile[42].

 

I'm stupid  and I must say sorry Jaclaz.

 

----

 

So i must add a line,

set /a counter2 =%counter% - 1

and then full code is

@echo offSETLOCAL ENABLEDELAYEDEXPANSIONSET /A Counter=1for %%b in (c d e f g h i j k l m n o p q r s t u v w x y z) do (dir %%b:\ToolsforPE.ISO >nul 2>nul && (set path=!path! %%b:set isofile[!Counter!]=%%b:\ToolsforPE.ISO&&SET /A Counter+=1)IF NOT DEFINED isofile[1] echo ISO not found)set /A counter2=%counter% - 1echo there is/are %counter2% ISO found on %isopath%pause

Tested in 3 cases:

- case 1: 0 ISO put on any partition.

.bat return ISO not found but quite much lines. must to improve it.
 

ISO not found
ISO not found
.....
ISO not found
there is/are 0 ISO found on

 

 

- case 2: 1 ISO on C:

.bat return

there is/are 1 ISO found on  c:      <<<<< perfect.

 

 

- case 3: 1 ISO (or even 2, 3 ...) but put on D: (and E: F:) not C:

.bat return

 

ISO not found   <<< Why ? and How can I improve .bat
there is/are 1 ISO found on  d:

 

 

Conclution:

0 ISO: .bat return very many lines say that 0 ISO found

1 ISO (or 2, 3 ...) but put it on from D: (pass C:) then .bat count ISO right but still has a line say that ISO not found

1 ISO (or 2 , 3 ...) and put on from C: then .bat work perfect.

 

Can you explain me why?? Tks

Link to comment
Share on other sites

I see three things, but like jaclaz I'm not going to write your script for you.

 

1) jaclaz told you, and you say you understand, that "path" is a system variable with a defined use, so you need to use a different variable name in your script. and use that different variable name in ALL places you need that variable.  Did you do that?

 

2) If the line of the script that echoes the "ISO not found" happens every time through the FOR loop iteration, then what do you think might happen if you moved that line of the script outside the FOR loop?  What is the purpose of that line?  Do you need both that line and the "there is/are ..." line?  You might want to think about combining them in an IF .. THEN ... ELSE type structure.

 

3) Even if you fix your "path" issue, the way your code is now the "there is/are ... " line will have the count correct but only list the location of the last one found, which is all that variable is capable of doing.  If you want to list all the locations that multiple ISOs were found you will have to make your code more complicated, both to store the multiple locations in an isopath[x] type variable, like you are doing with isofile[x], and to print them all out.  Only then will you be able to give the user a choice on which one to use.

 

Running the script and telling us what it does wrong is good and is the first step in debugging your code.  Now you need to start trying to find out how to fix it on your own.  This includes looking at other code examples until you know how and WHY they work, and also changing things in your script, multiple times with a small change each time, and rerunning it and observing the different outputs until you understand why it changed.  Reading about code is good, but the best way I've found to learn and remember it for later use is through trial and error.

 

Good luck!

 

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

1) jaclaz told you, and you say you understand, that "path" is a system variable with a defined use

 

I change is to isopath but there are some typos on above post. But in my .bat from my computer, all was change to isopath.

 

3) Even if you fix your "path" issue, the way your code is now the "there is/are ... " line will have the count correct but only list the location of the last one found

 

 

No, isopath work fine for me. It return that there is/are 2 ISO found on  c: d:

 

Just only problems is if ISO not put to C:, then it still echo no ISO found and then echo there is/are 1 ISO found on d:

 

I repost my .bat again

@echo offSETLOCAL ENABLEDELAYEDEXPANSIONSET /A Counter=1for %%b in (c d e f g h i j k l m n o p q r s t u v w x y z) do (dir %%b:\ToolsforPE.ISO >nul 2>nul && (set isopath=!isopath! %%b:set isofile[!Counter!]=%%b:\ToolsforPE.ISO&&SET /A Counter+=1)IF NOT DEFINED isofile[1] echo ISO not found)set /A counter2=%counter%-1echo there is/are %counter2% ISO found on %isopath%pause

Tks for your hint, I will try to find out and then report.

Edited by congnt92
Link to comment
Share on other sites

 

No, isopath work fine for me. It return that there is/are 2 ISO found on  c: d:

You're right, I missed that. Sorry. :)

Good luck fixing the rest of the issues.

Cheers and Regards

 

 

Yeah, i got it. It work fine but i'm not sure if it's not logical. Full code:

No, i'm wrong

@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET /A Counter=1for %%b in (c d e f g h i j k l m n o p q r s t u v w x y z) do (dir %%b:\ToolsforPE.ISO >nul 2>nul && (set isopath=!isopath! %%b:set isofile[!Counter!]=%%b:\ToolsforPE.ISO&&SET /A Counter+=1goto :found)):notfoundecho ISO not foundpauseexit:foundset /A counter2=%counter%-1echo there is/are %counter2% ISO found on%isopath%pauseexit

EDIT:

Oh, i'm sorry, it not return the true counter (always return is 1)

I'll try again.

 

EDIT2: seem it work fine. I will test more case to make sure.

@echo offSETLOCAL ENABLEDELAYEDEXPANSIONSET /A Counter=0for %%b in (c d e f g h i j k l m n o p q r s t u v w x y z) do (dir %%b:\ToolsforPE.ISO >nul 2>nul && (set isopath=!isopath! %%b:set isofile[!Counter!]=%%b:\ToolsforPE.ISO&&SET /A Counter+=1)IF NOT DEFINED isofile[0] echo >nul)if "%counter%"=="0" echo ISO not found && pause && exitif "%counter%"=="1" echo there is %counter% ISO file found on %isopath% && pause && exitecho there are %counter% ISO files found on %isopath%pauseexit

EDIT3:

try to remove IF NOT DEFINED since we not use it. It's work.

@echo offSETLOCAL ENABLEDELAYEDEXPANSIONSET /A Counter=0for %%b in (c d e f g h i j k l m n o p q r s t u v w x y z) do (dir %%b:\ToolsforPE.ISO >nul 2>nul && (set isopath=!isopath!%%b:set isofile[!Counter!]=%%b:\ToolsforPE.ISO&&SET /A Counter+=1))if "%counter%"=="0" echo ISO not found && pause && exitif "%counter%"=="1" echo there is %counter% ISO file found on %isopath% && pause && exitecho there are %counter% ISO files found on %isopath%pauseexit
Edited by congnt92
Link to comment
Share on other sites

I test quite many cases and seem it work fine.

 

First, I want to say thanks to you, Jaclaz, not only this topic, almost topic which I created, you always help me. I know that i'm very stupid and sometimes I ask some stupid question. Please forgive me.

Without you, this .bat will never improve.

Let me say something, from begin, when i got trouble with fsutil in XP, then I must try

fsutil fsinfo drives > list.txt

then

type list.txt | findstr /r %type% and then output if user type a letter which already use.

Jaclaz, you help me with for /f token. It work very very perfect.

 

And then, I want to list if there are more than 1 ISO found, you, again help me with a greate code. It's not new for you, but it's very greate for me.

I know about "path" variable, about !, [ ] in .batch. Not know all, but a little :thumbup

 

Finally, with bphlpt's hint, i try to hidden echo output and successed.

Just want to say milion thanks to both of you. And again i'm sorry for my bad English, for my bad .bat, for all.

God bless you.

Edited by congnt92
Link to comment
Share on other sites

Only to show off a little bit ;) and to have an occasion to use some math in the batch :w00t: , how I would write the stuff:

 

 

@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET lookfor=ToolsforPE.ISOSET /A Counter=0FOR /F %%A in ('MOUNTVOL^|FIND ":\"') do (dir /b %%A%lookfor% >nul 2>&1 && (SET /A Counter+=1set isofile[!Counter!]=%%A%lookfor%))SET /A §=0x9EA*2538+0x44F1*17649IF NOT DEFINED isofile[1] ECHO NO iso file was found&GOTO :endECHO Found !counter! item(s):FOR /F "tokens=2,3 delims=[]=" %%A IN ('SET isofile') DO ECHO %%A - %%BIF !counter!.==1. SET chosen=1&GOTO :past_choice:choice_loopSET /P chosen=Please choose item number:FOR /F "delims=%§%" %%? IN ("%chosen%") DO ECHO Invalid input&GOTO :choice_loopIF %chosen% GTR %Counter% ECHO Invalid input&GOTO :choice_loop:past_choiceECHO You chose item #%chosen% which is isofile[%chosen%] i.e. !isofile[%chosen%]!:endECHO.ECHO End of batch
jaclaz
Link to comment
Share on other sites

Only to show off a little bit ;) and to have an occasion to use some math in the batch :w00t: , how I would write the stuff:

 

 

@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET lookfor=ToolsforPE.ISOSET /A Counter=0FOR /F %%A in ('MOUNTVOL^|FIND ":\"') do (dir /b %%A%lookfor% >nul 2>&1 && (SET /A Counter+=1set isofile[!Counter!]=%%A%lookfor%))SET /A §=0x9EA*2538+0x44F1*17649IF NOT DEFINED isofile[1] ECHO NO iso file was found&GOTO :endECHO Found !counter! item(s):FOR /F "tokens=2,3 delims=[]=" %%A IN ('SET isofile') DO ECHO %%A - %%BIF !counter!.==1. SET chosen=1&GOTO :past_choice:choice_loopSET /P chosen=Please choose item number:FOR /F "delims=%§%" %%? IN ("%chosen%") DO ECHO Invalid input&GOTO :choice_loopIF %chosen% GTR %Counter% ECHO Invalid input&GOTO :choice_loop:past_choiceECHO You chose item #%chosen% which is isofile[%chosen%] i.e. !isofile[%chosen%]!:endECHO.ECHO End of batch
jaclaz

 

Oh, very nice output. You are genius.

But mounvol not exist on LiveXP and i want try to change your code with fsutil. Tks.

Link to comment
Share on other sites

Hmm, I know a new thing. About 0x9EA*2538+0x44F1*17649. Very very smart way to detect if chosen is invalid or not.

Of course it is just a joke to show an example of zeroless pandigitals:

http://www.worldofnumbers.com/ninedig1.htm

How do you think about

 

echo %chose%|findstr /r "[^1-9]"

 

TKs.

Sure :), that is a "normal" way, but findstr in some cases is extremely slow, and that way the user might input a "special" character like '"%&!()<>| that may make the batch fail, actually it should be:

FOR /F "delims=123456789" %%? IN ("!chosen!") DO ...

to take advantage of delayed expansion, see:

http://www.robvanderwoude.com/battech_inputvalidation_setp.php

About Mountvol, if I recall correctly, it is "self-standing", i.e. just adding mountvol.exe to the liveXP build it should work. :unsure:

jaclaz

Link to comment
Share on other sites

 

Hmm, I know a new thing. About 0x9EA*2538+0x44F1*17649. Very very smart way to detect if chosen is invalid or not.

Of course it is just a joke to show an example of zeroless pandigitals:

http://www.worldofnumbers.com/ninedig1.htm

How do you think about

 

echo %chose%|findstr /r "[^1-9]"

 

TKs.

Sure :), that is a "normal" way, but findstr in some cases is extremely slow, and that way the user might input a "special" character like '"%&!()<>| that may make the batch fail, actually it should be:

FOR /F "delims=123456789" %%? IN ("!chosen!") DO ...

to take advantage of delayed expansion, see:

http://www.robvanderwoude.com/battech_inputvalidation_setp.php

About Mountvol, if I recall correctly, it is "self-standing", i.e. just adding mountvol.exe to the liveXP build it should work. :unsure:

jaclaz

 

 

Ah, in .batch, some characters such as ^ ! will make .bat error.

About mountvol.exe, it's not my problems. I just want to avoid using mountvol in .bat, because if user run .bat in liveXP it will fail.

You help me with fsutil. It work and seem all rebuild of WinPE, fsutil is always exist. It's safe way to get letter.

Link to comment
Share on other sites

I have been using some other method to detect a specific VOLUME by reading the name of the label. So you could also look into adding a specific label to your ISO.

 

I use this method in a batch file to automatically create a SVN Depot on the volume with the label BACKUP:

@echo off::check if SVN depot existsfor /f "tokens=3,4 skip=9 delims= " %%i in ('cmd /c ^"echo list volume ^| diskpart^"') do (    if /i %%j==BACKUP set SVNPATH=%%i:\SVN depot)if not defined SVNPATH set SVNPATH=%SystemDrive%\SVN depotif not exist "%SVNPATH%" md "%SVNPATH%"::Create SVN servicesc create svnserve binpath= "\"%ProgramFiles%\TortoiseSVN\bin\svnserve.exe\" --service --root \"%SVNPATH%\"" displayname= "Subversion" depend= tcpip start= autonet start svnserve
Edited by Acheron
Link to comment
Share on other sites

 

I have been using some other method to detect a specific VOLUME by reading the name of the label. So you could also look into adding a specific label to your ISO.

 

I use this method in a batch file to automatically create a SVN Depot on the volume with the label BACKUP:

@echo off::check if SVN depot existsfor /f "tokens=3,4 skip=9 delims= " %%i in ('cmd /c ^"echo list volume ^| diskpart^"') do (    if /i %%j==BACKUP set SVNPATH=%%i:\SVN depot)if not defined SVNPATH set SVNPATH=%SystemDrive%\SVN depotif not exist "%SVNPATH%" md "%SVNPATH%"::Create SVN servicesc create svnserve binpath= "\"%ProgramFiles%\TortoiseSVN\bin\svnserve.exe\" --service --root \"%SVNPATH%\"" displayname= "Subversion" depend= tcpip start= autonet start svnserve

 

First, tks for your hint. I'll copy your code and save to my_bat_collection.txt file. I'm learning about token and delims. Your example is very helpful.

But i must say sorry to you, because your case is not same with mine, and i can't use it.

My .bat was create for user run to mount ISO file after boot to WinPE and it cannot find ISO file base on Label since Label is named by user.

But, even your code is not work for my case, it still is good example for me study .bat code.

Tks Acheron.

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...