0

Is there a way I can port this:

printf '=%.0s' {1..100} > file.txt

from bash to the cmd console?

To see what the code above does look here.

I can think of a few workarounds with variables and for-loops but I hoped for a quick and easy solution that doesn't require an the "-v" option set for the cmd in question (the shell is opened by make so as far as I know you can't specify the "-v" option).

Community
  • 1
  • 1
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
  • I'm ashamed to say I'm not terribly familiar with bash. What does the code do? – SomethingDark Dec 22 '14 at 18:03
  • I added what the explanation what the code should do. – WorldSEnder Dec 22 '14 at 18:13
  • Alright, string of one hundred equals signs with no terminating newline character. Got it. There's definitely a way to do this in batch, I just need to find the snippet... – SomethingDark Dec 22 '14 at 18:14
  • @WorldSEnder Does it absolutely **have** to be the = symbol? For some reason, the snippet I'm using doesn't like displaying them, but I can do a - if that's acceptable. – SomethingDark Dec 22 '14 at 18:28
  • It does not have to be displayed as long as it is written to the file, but yes, it has to be an `=`-symbol. Don't mind and submit it though, I may find a workaround myself. – WorldSEnder Dec 22 '14 at 18:30
  • 1
    You could `>file.txt echo ====================================================================================================` – rojo Dec 22 '14 at 18:45
  • @WorldSEnder What is this being used for? I've managed to display 100 = symbols to the command line, but if you open the file in a text editor, you can see that I achieved it by printing `.=` 100 times. – SomethingDark Dec 22 '14 at 20:24

5 Answers5

1

There's no easy way to simulate printf with batch. PowerShell does offer similar functionality using composite strings though. So, invoking PowerShell, your printf command becomes this:

powershell -command "'='*100" > file.txt
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Not quite what I want, but easily modifiable to that. You may want to read through bash's printf again :D – WorldSEnder Dec 22 '14 at 18:28
  • lol OK. Yeah, I just ssh'd into my Linux box to test your command and discovered how embarrassingly wrong that was. :) Thank you for the edit. – rojo Dec 22 '14 at 18:38
1

For some reason, I'm having a hard time printing the = symbol, but otherwise, this prints 100 - characters and no terminating newline character. I'll keep looking to see if there's a workaround.

@echo off

(
    for /L %%A in (1,1,100) do (
        <nul set/p =-
    )
)>file.txt
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
1

In the interest of thoroughness, here's another possibility. It's a two-liner, though. Still, if you're benchmarking, I think it'll have a speed advantage over the powershell solution.

set /p "=for(i=100;i--;)WSH.StdOut.Write('=')"<NUL>tmp.js
>file.txt cscript /nologo tmp.js && del tmp.js

Or, in for a penny, in for a pound I guess. Create repeat.js as follows:

for (i=WSH.Arguments(1); i--; ) {
    WSH.StdOut.Write(WSH.Arguments(0));
}

Then call it as cscript /nologo repeat.js "=" 100 >file.txt whenever needed.

rojo
  • 24,000
  • 5
  • 55
  • 101
0

with a little trick, FBnerFP's answer does, what you want:

setlocal enabledelayedexpansion
set "x="
for /l %%i in (1,1,100) do set x=!x!=
echo %x%

Note: a forvariable (x is a one-digit-name. %variable won't work.

Note: for use at the command prompt write %i instead of %%i in batch files

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • That requires having started the cmd with cmd -v, as explained this is not possible. Or I would have to create a bashscript to call, which seems like too much overhead for such a simple task... I'll consider it. – WorldSEnder Dec 22 '14 at 18:29
  • 2
    ahhmmm - no. Either `/v` or `setlocal enabledelayedexpansion` – Stephan Dec 22 '14 at 18:48
-1

See For /l.

FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers So (1,1,5) would generate the sequence (5 4 3 2 1)

Nick Dickinson-Wilde
  • 1,015
  • 2
  • 15
  • 21
  • How does that help? echo will append a newline even when I append that to the file. Please explain a bit more – WorldSEnder Dec 22 '14 at 18:09
  • `>>` appends while `>` overwrites. If you don't specify your actual problem you won't get useful answers. – FBnerFP Dec 22 '14 at 18:11