Set a Value in a for loop












-1















I'm trying to search for a file on Window Path. So I've created a loop and for each directory I look for my File. If Found I Raise a flag.



My file sofar:



    @Echo OFF
SET path=%PATH%;C:Program Files7-Zip;C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0Bin
SET /A var=0

for %%G in ("%path:;=" "%") do IF EXIST 7x.exe (SET /A var = 1 )

IF /I "%var%" EQU "1" ECHO equality with 1
IF /I "%var%" EQU "0" ECHO equality with 0


But my variable are 0 after my loop even though I know for sure the file is there.



So this raises some questions:
1) How do I set my variable var inside the loop
2) Can I test for the value of var before checking for the existance of a file
Pseudo code: If (var == 0 && EXIST 7x.exe) set var = 1










share|improve this question























  • So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

    – Dominique
    Nov 23 '18 at 12:44











  • Because i want to display an error message if it doesn't

    – Jens Borrisholt
    Nov 23 '18 at 12:47











  • why not just do where 7zip.exe & echo %errorlevel%

    – Gerhard Barnard
    Nov 23 '18 at 12:50






  • 4





    You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

    – Aacini
    Nov 23 '18 at 13:05








  • 1





    (by the way: it's 7z.exe, not 7x.exe)

    – Stephan
    Nov 23 '18 at 13:19
















-1















I'm trying to search for a file on Window Path. So I've created a loop and for each directory I look for my File. If Found I Raise a flag.



My file sofar:



    @Echo OFF
SET path=%PATH%;C:Program Files7-Zip;C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0Bin
SET /A var=0

for %%G in ("%path:;=" "%") do IF EXIST 7x.exe (SET /A var = 1 )

IF /I "%var%" EQU "1" ECHO equality with 1
IF /I "%var%" EQU "0" ECHO equality with 0


But my variable are 0 after my loop even though I know for sure the file is there.



So this raises some questions:
1) How do I set my variable var inside the loop
2) Can I test for the value of var before checking for the existance of a file
Pseudo code: If (var == 0 && EXIST 7x.exe) set var = 1










share|improve this question























  • So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

    – Dominique
    Nov 23 '18 at 12:44











  • Because i want to display an error message if it doesn't

    – Jens Borrisholt
    Nov 23 '18 at 12:47











  • why not just do where 7zip.exe & echo %errorlevel%

    – Gerhard Barnard
    Nov 23 '18 at 12:50






  • 4





    You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

    – Aacini
    Nov 23 '18 at 13:05








  • 1





    (by the way: it's 7z.exe, not 7x.exe)

    – Stephan
    Nov 23 '18 at 13:19














-1












-1








-1


0






I'm trying to search for a file on Window Path. So I've created a loop and for each directory I look for my File. If Found I Raise a flag.



My file sofar:



    @Echo OFF
SET path=%PATH%;C:Program Files7-Zip;C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0Bin
SET /A var=0

for %%G in ("%path:;=" "%") do IF EXIST 7x.exe (SET /A var = 1 )

IF /I "%var%" EQU "1" ECHO equality with 1
IF /I "%var%" EQU "0" ECHO equality with 0


But my variable are 0 after my loop even though I know for sure the file is there.



So this raises some questions:
1) How do I set my variable var inside the loop
2) Can I test for the value of var before checking for the existance of a file
Pseudo code: If (var == 0 && EXIST 7x.exe) set var = 1










share|improve this question














I'm trying to search for a file on Window Path. So I've created a loop and for each directory I look for my File. If Found I Raise a flag.



My file sofar:



    @Echo OFF
SET path=%PATH%;C:Program Files7-Zip;C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0Bin
SET /A var=0

for %%G in ("%path:;=" "%") do IF EXIST 7x.exe (SET /A var = 1 )

IF /I "%var%" EQU "1" ECHO equality with 1
IF /I "%var%" EQU "0" ECHO equality with 0


But my variable are 0 after my loop even though I know for sure the file is there.



So this raises some questions:
1) How do I set my variable var inside the loop
2) Can I test for the value of var before checking for the existance of a file
Pseudo code: If (var == 0 && EXIST 7x.exe) set var = 1







batch-file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:41









Jens BorrisholtJens Borrisholt

4,33911944




4,33911944













  • So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

    – Dominique
    Nov 23 '18 at 12:44











  • Because i want to display an error message if it doesn't

    – Jens Borrisholt
    Nov 23 '18 at 12:47











  • why not just do where 7zip.exe & echo %errorlevel%

    – Gerhard Barnard
    Nov 23 '18 at 12:50






  • 4





    You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

    – Aacini
    Nov 23 '18 at 13:05








  • 1





    (by the way: it's 7z.exe, not 7x.exe)

    – Stephan
    Nov 23 '18 at 13:19



















  • So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

    – Dominique
    Nov 23 '18 at 12:44











  • Because i want to display an error message if it doesn't

    – Jens Borrisholt
    Nov 23 '18 at 12:47











  • why not just do where 7zip.exe & echo %errorlevel%

    – Gerhard Barnard
    Nov 23 '18 at 12:50






  • 4





    You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

    – Aacini
    Nov 23 '18 at 13:05








  • 1





    (by the way: it's 7z.exe, not 7x.exe)

    – Stephan
    Nov 23 '18 at 13:19

















So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

– Dominique
Nov 23 '18 at 12:44





So you want to check if 7z.exe is present in your path? Why don't you simply try to run it?

– Dominique
Nov 23 '18 at 12:44













Because i want to display an error message if it doesn't

– Jens Borrisholt
Nov 23 '18 at 12:47





Because i want to display an error message if it doesn't

– Jens Borrisholt
Nov 23 '18 at 12:47













why not just do where 7zip.exe & echo %errorlevel%

– Gerhard Barnard
Nov 23 '18 at 12:50





why not just do where 7zip.exe & echo %errorlevel%

– Gerhard Barnard
Nov 23 '18 at 12:50




4




4





You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

– Aacini
Nov 23 '18 at 13:05







You forgot to include the %%G FOR parameter in the file name: for %%G in ("%path:;=" "%") do IF EXIST "%%~G7x.exe" (SET /A var = 1 )

– Aacini
Nov 23 '18 at 13:05






1




1





(by the way: it's 7z.exe, not 7x.exe)

– Stephan
Nov 23 '18 at 13:19





(by the way: it's 7z.exe, not 7x.exe)

– Stephan
Nov 23 '18 at 13:19












1 Answer
1






active

oldest

votes


















2














the where command (by default) searches a file through the path (it can also be used to search a folder tree or even the whole disk with the /R switch, which you obviously don't need here). It returns an errorlevel of 0 if the file was found and 1 if not found. There is even a /Q (Quiet) switch. So this makes it quite easy:



where /q 7z.exe 
set var=%errorlevel%
echo equality with %var%


Instead of the set command, you can also directly echo equality with %errorlevel%, but keep in mind, using (nearly) any command between where and the use of %errorlevel% might change the errorlevel.






share|improve this answer


























  • Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

    – Jens Borrisholt
    Nov 23 '18 at 13:03











  • @Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

    – double-beep
    Nov 23 '18 at 13:06











  • I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

    – Jens Borrisholt
    Nov 23 '18 at 13:06











  • @double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

    – Stephan
    Nov 23 '18 at 13:13











  • @Stephan your solution doesn't work

    – Jens Borrisholt
    Nov 23 '18 at 13:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446914%2fset-a-value-in-a-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














the where command (by default) searches a file through the path (it can also be used to search a folder tree or even the whole disk with the /R switch, which you obviously don't need here). It returns an errorlevel of 0 if the file was found and 1 if not found. There is even a /Q (Quiet) switch. So this makes it quite easy:



where /q 7z.exe 
set var=%errorlevel%
echo equality with %var%


Instead of the set command, you can also directly echo equality with %errorlevel%, but keep in mind, using (nearly) any command between where and the use of %errorlevel% might change the errorlevel.






share|improve this answer


























  • Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

    – Jens Borrisholt
    Nov 23 '18 at 13:03











  • @Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

    – double-beep
    Nov 23 '18 at 13:06











  • I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

    – Jens Borrisholt
    Nov 23 '18 at 13:06











  • @double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

    – Stephan
    Nov 23 '18 at 13:13











  • @Stephan your solution doesn't work

    – Jens Borrisholt
    Nov 23 '18 at 13:15
















2














the where command (by default) searches a file through the path (it can also be used to search a folder tree or even the whole disk with the /R switch, which you obviously don't need here). It returns an errorlevel of 0 if the file was found and 1 if not found. There is even a /Q (Quiet) switch. So this makes it quite easy:



where /q 7z.exe 
set var=%errorlevel%
echo equality with %var%


Instead of the set command, you can also directly echo equality with %errorlevel%, but keep in mind, using (nearly) any command between where and the use of %errorlevel% might change the errorlevel.






share|improve this answer


























  • Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

    – Jens Borrisholt
    Nov 23 '18 at 13:03











  • @Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

    – double-beep
    Nov 23 '18 at 13:06











  • I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

    – Jens Borrisholt
    Nov 23 '18 at 13:06











  • @double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

    – Stephan
    Nov 23 '18 at 13:13











  • @Stephan your solution doesn't work

    – Jens Borrisholt
    Nov 23 '18 at 13:15














2












2








2







the where command (by default) searches a file through the path (it can also be used to search a folder tree or even the whole disk with the /R switch, which you obviously don't need here). It returns an errorlevel of 0 if the file was found and 1 if not found. There is even a /Q (Quiet) switch. So this makes it quite easy:



where /q 7z.exe 
set var=%errorlevel%
echo equality with %var%


Instead of the set command, you can also directly echo equality with %errorlevel%, but keep in mind, using (nearly) any command between where and the use of %errorlevel% might change the errorlevel.






share|improve this answer















the where command (by default) searches a file through the path (it can also be used to search a folder tree or even the whole disk with the /R switch, which you obviously don't need here). It returns an errorlevel of 0 if the file was found and 1 if not found. There is even a /Q (Quiet) switch. So this makes it quite easy:



where /q 7z.exe 
set var=%errorlevel%
echo equality with %var%


Instead of the set command, you can also directly echo equality with %errorlevel%, but keep in mind, using (nearly) any command between where and the use of %errorlevel% might change the errorlevel.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 13:15

























answered Nov 23 '18 at 13:02









StephanStephan

35.5k43256




35.5k43256













  • Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

    – Jens Borrisholt
    Nov 23 '18 at 13:03











  • @Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

    – double-beep
    Nov 23 '18 at 13:06











  • I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

    – Jens Borrisholt
    Nov 23 '18 at 13:06











  • @double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

    – Stephan
    Nov 23 '18 at 13:13











  • @Stephan your solution doesn't work

    – Jens Borrisholt
    Nov 23 '18 at 13:15



















  • Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

    – Jens Borrisholt
    Nov 23 '18 at 13:03











  • @Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

    – double-beep
    Nov 23 '18 at 13:06











  • I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

    – Jens Borrisholt
    Nov 23 '18 at 13:06











  • @double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

    – Stephan
    Nov 23 '18 at 13:13











  • @Stephan your solution doesn't work

    – Jens Borrisholt
    Nov 23 '18 at 13:15

















Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

– Jens Borrisholt
Nov 23 '18 at 13:03





Thx I'll Try it out. If you have the time then for educational purposes. Can you create a solution "my! way

– Jens Borrisholt
Nov 23 '18 at 13:03













@Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

– double-beep
Nov 23 '18 at 13:06





@Stephan I think you should put an if here which would check errorlevel variable. For example: if "%errorlevel%" EQU "0" do something.

– double-beep
Nov 23 '18 at 13:06













I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

– Jens Borrisholt
Nov 23 '18 at 13:06





I dont understand your solution %var% allways equals tio 1 nomatter what I'm searching for

– Jens Borrisholt
Nov 23 '18 at 13:06













@double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

– Stephan
Nov 23 '18 at 13:13





@double-beep: OP does obviously know, how to build an if statement - his question was how to check, if the file exists

– Stephan
Nov 23 '18 at 13:13













@Stephan your solution doesn't work

– Jens Borrisholt
Nov 23 '18 at 13:15





@Stephan your solution doesn't work

– Jens Borrisholt
Nov 23 '18 at 13:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446914%2fset-a-value-in-a-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen