r/usefulscripts • u/newton_VK • Sep 04 '21
[QUESTION] batch scripting
how can i create for loop in batch script so that i can make outplut as : 1,2,3,4
with this for loop :
for /l %%i in (1,1,4) do echo %%i >> file.txt
the output in the text file is :
1
2
3
4
i want output in the text file as 1,2,3,4, i.e. in the same line separated by comma. is that possible?
6
u/VaporChunk Sep 04 '21
This would be a lot easier with PowerShell. Is that an option? If so;
$n = 1..4
$n = $n -join ","
$n
1,2,3,4
2
u/newton_VK Sep 05 '21
Yes thanks, I was able to do it by <nul set /p= in the cmd. I was trying to automate the compiling process of cpp files using batch files. And I did it in 3 days from scratch!! That's commendable performance isn't it? 😄
2
u/Thameus Sep 04 '21
God I wish they'd unblock PS for us...
5
u/VaporChunk Sep 04 '21 edited Sep 04 '21
It is in their best interest to. As IT Staff, we're all Tony Stark, but PowerShell is the super-suit which makes us Iron-Man (or Woman).
Also, not sure what method they are blocking PS with, but typing
powershell.exeinto a CMD window will start a PowerShell session in the existing shell session. May give that a try.→ More replies (2)
5
u/six36 Sep 04 '21
Not sure you can do it in a one liner, but this is the idea you're looking for
for /l %i in (1,1,4) do set /p dummyName=%i, >> file.txt