Nesting operators in Windows cmd (.bat)
I'm trying to tidy up some code. Right now I have:
SET VAR=%TIME:.=&rem.%
SET VAR=%VAR:,=&rem.%
SET VAR=%VAR::=_%
And it works, but I wuld like to turn it into one-liner. Something like this (doesn't work):
SET VAR=% % %TIME:.=&rem% :,=&rem.% ::=_%
Can I achieve something like this with concise, simple syntax?
batch-file cmd
|
show 2 more comments
I'm trying to tidy up some code. Right now I have:
SET VAR=%TIME:.=&rem.%
SET VAR=%VAR:,=&rem.%
SET VAR=%VAR::=_%
And it works, but I wuld like to turn it into one-liner. Something like this (doesn't work):
SET VAR=% % %TIME:.=&rem% :,=&rem.% ::=_%
Can I achieve something like this with concise, simple syntax?
batch-file cmd
2
why not just chain them using&&
likeset something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe likeSET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).
– franiis
Nov 14 '18 at 8:43
1
you seemed to have missed my comment..set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
1
The quick answer is: no, this is not possible; not even with nestedcall set
commands...
– aschipfl
Nov 14 '18 at 9:56
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02
|
show 2 more comments
I'm trying to tidy up some code. Right now I have:
SET VAR=%TIME:.=&rem.%
SET VAR=%VAR:,=&rem.%
SET VAR=%VAR::=_%
And it works, but I wuld like to turn it into one-liner. Something like this (doesn't work):
SET VAR=% % %TIME:.=&rem% :,=&rem.% ::=_%
Can I achieve something like this with concise, simple syntax?
batch-file cmd
I'm trying to tidy up some code. Right now I have:
SET VAR=%TIME:.=&rem.%
SET VAR=%VAR:,=&rem.%
SET VAR=%VAR::=_%
And it works, but I wuld like to turn it into one-liner. Something like this (doesn't work):
SET VAR=% % %TIME:.=&rem% :,=&rem.% ::=_%
Can I achieve something like this with concise, simple syntax?
batch-file cmd
batch-file cmd
edited Nov 14 '18 at 8:23
franiis
asked Nov 14 '18 at 8:17
franiisfraniis
945923
945923
2
why not just chain them using&&
likeset something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe likeSET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).
– franiis
Nov 14 '18 at 8:43
1
you seemed to have missed my comment..set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
1
The quick answer is: no, this is not possible; not even with nestedcall set
commands...
– aschipfl
Nov 14 '18 at 9:56
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02
|
show 2 more comments
2
why not just chain them using&&
likeset something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe likeSET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).
– franiis
Nov 14 '18 at 8:43
1
you seemed to have missed my comment..set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
1
The quick answer is: no, this is not possible; not even with nestedcall set
commands...
– aschipfl
Nov 14 '18 at 9:56
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02
2
2
why not just chain them using
&&
like set something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
why not just chain them using
&&
like set something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe like
SET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).– franiis
Nov 14 '18 at 8:43
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe like
SET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).– franiis
Nov 14 '18 at 8:43
1
1
you seemed to have missed my comment..
set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
you seemed to have missed my comment..
set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
1
1
The quick answer is: no, this is not possible; not even with nested
call set
commands...– aschipfl
Nov 14 '18 at 9:56
The quick answer is: no, this is not possible; not even with nested
call set
commands...– aschipfl
Nov 14 '18 at 9:56
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02
|
show 2 more comments
1 Answer
1
active
oldest
votes
Although it is possible to do it in one line (delayed expansion needed), I don't think, it serves readability:
@echo off
setlocal enabledelayedexpansion
set "var=%time:~0,8%" & set "var=!var::=_!"
echo %var%
I suggest moving the formatting to a subroutine. (Not exactly a one-liner, but keeps your main code clean and readable).
@echo off
call :FormatTime var="%time%"
echo %var%
goto :eof
:FormatTime
set "_=%~2"
set "_=%_:~0,8%" :: get 'HH:MM:SS'
set "_=%_: =0%" :: replace ' ' with '0'
set "%1=%_::=_%" :: replace ':' with '_'
goto :eof
The call
defines the variable name for the result (var
here) and the string to format (%time%
here). Usually, the two arguments are separated by a space, but as =
also acts as a standard delimiter, we can use that to make the line even more readable (intuitive).
Enclosing the string in quotes ("%time%
) enables us to catch the space (" 8:12:00,99"
) to be able to replace it with a zero.
I also used another method to strip the milliseconds part (language independent).
So do you think that a variable called_
(underscore) is readable?:/
– Aacini
Nov 14 '18 at 14:30
1
@Aacini I'm sure, I saidkeeps your *main code* ... readable
... I often use_
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.
– Stephan
Nov 14 '18 at 15:27
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295680%2fnesting-operators-in-windows-cmd-bat%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
Although it is possible to do it in one line (delayed expansion needed), I don't think, it serves readability:
@echo off
setlocal enabledelayedexpansion
set "var=%time:~0,8%" & set "var=!var::=_!"
echo %var%
I suggest moving the formatting to a subroutine. (Not exactly a one-liner, but keeps your main code clean and readable).
@echo off
call :FormatTime var="%time%"
echo %var%
goto :eof
:FormatTime
set "_=%~2"
set "_=%_:~0,8%" :: get 'HH:MM:SS'
set "_=%_: =0%" :: replace ' ' with '0'
set "%1=%_::=_%" :: replace ':' with '_'
goto :eof
The call
defines the variable name for the result (var
here) and the string to format (%time%
here). Usually, the two arguments are separated by a space, but as =
also acts as a standard delimiter, we can use that to make the line even more readable (intuitive).
Enclosing the string in quotes ("%time%
) enables us to catch the space (" 8:12:00,99"
) to be able to replace it with a zero.
I also used another method to strip the milliseconds part (language independent).
So do you think that a variable called_
(underscore) is readable?:/
– Aacini
Nov 14 '18 at 14:30
1
@Aacini I'm sure, I saidkeeps your *main code* ... readable
... I often use_
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.
– Stephan
Nov 14 '18 at 15:27
add a comment |
Although it is possible to do it in one line (delayed expansion needed), I don't think, it serves readability:
@echo off
setlocal enabledelayedexpansion
set "var=%time:~0,8%" & set "var=!var::=_!"
echo %var%
I suggest moving the formatting to a subroutine. (Not exactly a one-liner, but keeps your main code clean and readable).
@echo off
call :FormatTime var="%time%"
echo %var%
goto :eof
:FormatTime
set "_=%~2"
set "_=%_:~0,8%" :: get 'HH:MM:SS'
set "_=%_: =0%" :: replace ' ' with '0'
set "%1=%_::=_%" :: replace ':' with '_'
goto :eof
The call
defines the variable name for the result (var
here) and the string to format (%time%
here). Usually, the two arguments are separated by a space, but as =
also acts as a standard delimiter, we can use that to make the line even more readable (intuitive).
Enclosing the string in quotes ("%time%
) enables us to catch the space (" 8:12:00,99"
) to be able to replace it with a zero.
I also used another method to strip the milliseconds part (language independent).
So do you think that a variable called_
(underscore) is readable?:/
– Aacini
Nov 14 '18 at 14:30
1
@Aacini I'm sure, I saidkeeps your *main code* ... readable
... I often use_
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.
– Stephan
Nov 14 '18 at 15:27
add a comment |
Although it is possible to do it in one line (delayed expansion needed), I don't think, it serves readability:
@echo off
setlocal enabledelayedexpansion
set "var=%time:~0,8%" & set "var=!var::=_!"
echo %var%
I suggest moving the formatting to a subroutine. (Not exactly a one-liner, but keeps your main code clean and readable).
@echo off
call :FormatTime var="%time%"
echo %var%
goto :eof
:FormatTime
set "_=%~2"
set "_=%_:~0,8%" :: get 'HH:MM:SS'
set "_=%_: =0%" :: replace ' ' with '0'
set "%1=%_::=_%" :: replace ':' with '_'
goto :eof
The call
defines the variable name for the result (var
here) and the string to format (%time%
here). Usually, the two arguments are separated by a space, but as =
also acts as a standard delimiter, we can use that to make the line even more readable (intuitive).
Enclosing the string in quotes ("%time%
) enables us to catch the space (" 8:12:00,99"
) to be able to replace it with a zero.
I also used another method to strip the milliseconds part (language independent).
Although it is possible to do it in one line (delayed expansion needed), I don't think, it serves readability:
@echo off
setlocal enabledelayedexpansion
set "var=%time:~0,8%" & set "var=!var::=_!"
echo %var%
I suggest moving the formatting to a subroutine. (Not exactly a one-liner, but keeps your main code clean and readable).
@echo off
call :FormatTime var="%time%"
echo %var%
goto :eof
:FormatTime
set "_=%~2"
set "_=%_:~0,8%" :: get 'HH:MM:SS'
set "_=%_: =0%" :: replace ' ' with '0'
set "%1=%_::=_%" :: replace ':' with '_'
goto :eof
The call
defines the variable name for the result (var
here) and the string to format (%time%
here). Usually, the two arguments are separated by a space, but as =
also acts as a standard delimiter, we can use that to make the line even more readable (intuitive).
Enclosing the string in quotes ("%time%
) enables us to catch the space (" 8:12:00,99"
) to be able to replace it with a zero.
I also used another method to strip the milliseconds part (language independent).
edited Nov 14 '18 at 18:16
answered Nov 14 '18 at 11:35
StephanStephan
35.8k43357
35.8k43357
So do you think that a variable called_
(underscore) is readable?:/
– Aacini
Nov 14 '18 at 14:30
1
@Aacini I'm sure, I saidkeeps your *main code* ... readable
... I often use_
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.
– Stephan
Nov 14 '18 at 15:27
add a comment |
So do you think that a variable called_
(underscore) is readable?:/
– Aacini
Nov 14 '18 at 14:30
1
@Aacini I'm sure, I saidkeeps your *main code* ... readable
... I often use_
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.
– Stephan
Nov 14 '18 at 15:27
So do you think that a variable called
_
(underscore) is readable? :/
– Aacini
Nov 14 '18 at 14:30
So do you think that a variable called
_
(underscore) is readable? :/
– Aacini
Nov 14 '18 at 14:30
1
1
@Aacini I'm sure, I said
keeps your *main code* ... readable
... I often use _
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.– Stephan
Nov 14 '18 at 15:27
@Aacini I'm sure, I said
keeps your *main code* ... readable
... I often use _
as a temporary variable in subroutines, being sure, I don't overwrite any vital variables anywhere else.– Stephan
Nov 14 '18 at 15:27
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295680%2fnesting-operators-in-windows-cmd-bat%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
why not just chain them using
&&
likeset something && set another &&...
– Gerhard Barnard
Nov 14 '18 at 8:32
I don't want obfuscate code. I would like to have readable one line setting for each variable. That's why I'm looking for some shortened (yet readable) syntax. Maybe like
SET VAR = %.. :... :... :...%
or nesting. I don't see anything similar (possibly doesn't exist, but I'm not an expert).– franiis
Nov 14 '18 at 8:43
1
you seemed to have missed my comment..
set VAR=%TIME:.=&rem.% && set VAR=%VAR:,=&rem.% && set VAR=%VAR::=_%
– Gerhard Barnard
Nov 14 '18 at 9:48
1
The quick answer is: no, this is not possible; not even with nested
call set
commands...– aschipfl
Nov 14 '18 at 9:56
@GerhardBarnard Thank you, I saw it. I tried to do something "more pretty" (IMO) then using many comands in one line.
– franiis
Nov 14 '18 at 10:02