Manipulate FOR command variables?












0















It works.



:: %%a is output of FOR loop in batch:  
set _string=%%a
:: First two chars:
set "_two=!_string:~0,2!"


I would like to get the two characters by operating directly on %%a,

i.e. without creating the intermediate variable _string.

Possible?










share|improve this question




















  • 1





    The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

    – Squashman
    Nov 24 '18 at 2:49








  • 2





    To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

    – Squashman
    Nov 24 '18 at 3:05











  • @Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

    – displayname
    Nov 25 '18 at 18:14
















0















It works.



:: %%a is output of FOR loop in batch:  
set _string=%%a
:: First two chars:
set "_two=!_string:~0,2!"


I would like to get the two characters by operating directly on %%a,

i.e. without creating the intermediate variable _string.

Possible?










share|improve this question




















  • 1





    The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

    – Squashman
    Nov 24 '18 at 2:49








  • 2





    To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

    – Squashman
    Nov 24 '18 at 3:05











  • @Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

    – displayname
    Nov 25 '18 at 18:14














0












0








0








It works.



:: %%a is output of FOR loop in batch:  
set _string=%%a
:: First two chars:
set "_two=!_string:~0,2!"


I would like to get the two characters by operating directly on %%a,

i.e. without creating the intermediate variable _string.

Possible?










share|improve this question
















It works.



:: %%a is output of FOR loop in batch:  
set _string=%%a
:: First two chars:
set "_two=!_string:~0,2!"


I would like to get the two characters by operating directly on %%a,

i.e. without creating the intermediate variable _string.

Possible?







batch-file for-loop variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 2:46









ImaginaryHuman072889

3,4183732




3,4183732










asked Nov 24 '18 at 2:33









displaynamedisplayname

103




103








  • 1





    The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

    – Squashman
    Nov 24 '18 at 2:49








  • 2





    To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

    – Squashman
    Nov 24 '18 at 3:05











  • @Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

    – displayname
    Nov 25 '18 at 18:14














  • 1





    The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

    – Squashman
    Nov 24 '18 at 2:49








  • 2





    To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

    – Squashman
    Nov 24 '18 at 3:05











  • @Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

    – displayname
    Nov 25 '18 at 18:14








1




1





The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

– Squashman
Nov 24 '18 at 2:49







The FOR variable only works within the scope of the FOR command. You have no FOR command in your code. Please take the Tour. Learn how to ask a good question. And also read, How to create a Minimal, Complete, and Verifiable example.

– Squashman
Nov 24 '18 at 2:49






2




2





To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

– Squashman
Nov 24 '18 at 3:05





To answer your question. You can only do string manipulation on Environmental Variables. Meta-Variables from the FOR command or command line arguments cannot be manipulated directly.

– Squashman
Nov 24 '18 at 3:05













@Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

– displayname
Nov 25 '18 at 18:14





@Squashman, you saw, upon reconsideration, that my question is a good question, then answered according, for which I thank you. Exactly what I wanted to know.

– displayname
Nov 25 '18 at 18:14












1 Answer
1






active

oldest

votes


















0














It is possible without creating an intermediate variable, but without putting an actual task to your request, the solution may not be as EZ as the examples below, which utilise PowerShell:



Batch file example:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause


…and at the command prompt:



For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"


You can even do other things, like forcing upper case output:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause





share|improve this answer
























  • Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

    – LotPings
    Nov 24 '18 at 15:35






  • 1





    @LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

    – Compo
    Nov 24 '18 at 15:57













  • @Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

    – displayname
    Nov 25 '18 at 18:14











  • @displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

    – Compo
    Nov 25 '18 at 19:17











  • Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

    – displayname
    Nov 26 '18 at 21:02













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%2f53454705%2fmanipulate-for-command-variables%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









0














It is possible without creating an intermediate variable, but without putting an actual task to your request, the solution may not be as EZ as the examples below, which utilise PowerShell:



Batch file example:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause


…and at the command prompt:



For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"


You can even do other things, like forcing upper case output:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause





share|improve this answer
























  • Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

    – LotPings
    Nov 24 '18 at 15:35






  • 1





    @LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

    – Compo
    Nov 24 '18 at 15:57













  • @Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

    – displayname
    Nov 25 '18 at 18:14











  • @displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

    – Compo
    Nov 25 '18 at 19:17











  • Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

    – displayname
    Nov 26 '18 at 21:02


















0














It is possible without creating an intermediate variable, but without putting an actual task to your request, the solution may not be as EZ as the examples below, which utilise PowerShell:



Batch file example:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause


…and at the command prompt:



For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"


You can even do other things, like forcing upper case output:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause





share|improve this answer
























  • Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

    – LotPings
    Nov 24 '18 at 15:35






  • 1





    @LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

    – Compo
    Nov 24 '18 at 15:57













  • @Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

    – displayname
    Nov 25 '18 at 18:14











  • @displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

    – Compo
    Nov 25 '18 at 19:17











  • Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

    – displayname
    Nov 26 '18 at 21:02
















0












0








0







It is possible without creating an intermediate variable, but without putting an actual task to your request, the solution may not be as EZ as the examples below, which utilise PowerShell:



Batch file example:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause


…and at the command prompt:



For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"


You can even do other things, like forcing upper case output:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause





share|improve this answer













It is possible without creating an intermediate variable, but without putting an actual task to your request, the solution may not be as EZ as the examples below, which utilise PowerShell:



Batch file example:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2)"
@Pause


…and at the command prompt:



For %A In ("ezrine") Do @PowerShell "('%A').SubString(0,2)"


You can even do other things, like forcing upper case output:



@For %%A In ("ezrine") Do @PowerShell "('%%A').SubString(0,2).ToUpper()"
@Pause






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 12:25









CompoCompo

15.7k3926




15.7k3926













  • Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

    – LotPings
    Nov 24 '18 at 15:35






  • 1





    @LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

    – Compo
    Nov 24 '18 at 15:57













  • @Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

    – displayname
    Nov 25 '18 at 18:14











  • @displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

    – Compo
    Nov 25 '18 at 19:17











  • Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

    – displayname
    Nov 26 '18 at 21:02





















  • Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

    – LotPings
    Nov 24 '18 at 15:35






  • 1





    @LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

    – Compo
    Nov 24 '18 at 15:57













  • @Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

    – displayname
    Nov 25 '18 at 18:14











  • @displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

    – Compo
    Nov 25 '18 at 19:17











  • Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

    – displayname
    Nov 26 '18 at 21:02



















Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

– LotPings
Nov 24 '18 at 15:35





Without knowing the environment the batch runs in, I*d include -NoP -C for -NoProfile -Command. In higher PowerShell versions (admittedly PowerShell 6 core) the positional parameter 0 changes from -Command to -File.

– LotPings
Nov 24 '18 at 15:35




1




1





@LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

– Compo
Nov 24 '18 at 15:57







@LotPings, I would probably use -NoP too, but the question was about the possibility so I provided only what was necessary to prove it possible. Had the OP have provided more information, I would have considered doing so too!

– Compo
Nov 24 '18 at 15:57















@Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

– displayname
Nov 25 '18 at 18:14





@Compo, a great bonus to know that I can use PS commands in Batch shell. And I did apply your example to my script.

– displayname
Nov 25 '18 at 18:14













@displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

– Compo
Nov 25 '18 at 19:17





@displayname, it would be appreciated were you to mark my answer as accepted, by clicking on the large check mark to its left.

– Compo
Nov 25 '18 at 19:17













Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

– displayname
Nov 26 '18 at 21:02







Ah, so that's how it works. Ok, did it. Is there a way to give some kinda credit to Squashman?

– displayname
Nov 26 '18 at 21:02




















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%2f53454705%2fmanipulate-for-command-variables%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

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Berounka

I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...