0

suppose a file asimplefile.txt contains

hi have a nice day

how can i replace everything with 0 (or any character). the file should look like "

000000000000000000

using batch command or using windows command line directly

Aritra Hazra
  • 426
  • 2
  • 7
  • 17

2 Answers2

3

Very simple using REPL.BAT - a hybrid JScript/batch utility that performs a regular expression search/replace on stdin and writes the result to stdout. REPL.BAT is pure script that will run natively on any modern Windows machine from XP onward.

type asimplefile.txt | repl . 0 >asimplefile.txt.new
move /y asimplefile.txt.new asimplefile.txt >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

Replace the third line with the path to your file.Have on mind that strings that contain ! will fail and FOR /F does not process empty or filled only with delimiters lines.Not sure if you want to replace the new lines too?

@echo off
setlocal

 set "file_tp=c:\some_file.txt"

 break>"%tmp%\zeroes"
 for /f "delims=" %%a in ('findstr /n /r "^" "%file_tp%" ') do (
    setlocal enableDelayedExpansion
    set "zero_string="
    set "line=%%a"
    set "line=!line:~2!"
    call :strlen0.3 line len

    for /l %%# in (1,1,!len!) do (
        set "zero_ztring=!zero_ztring!0"
    )
    echo !zero_ztring!>>"%tmp%\zeroes"
    endlocal

)

type "%tmp%\zeroes">"%file_tp%"
del /q /f "%tmp%\zeroes"

endlocal
goto :eof


:strlen0.3  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%A in ( 6561 2187 729 243 81 27 9 3 1) do (
    set /A mod=2*%%A
    for %%Z in (!mod!) do (
        if !mod! GTR 8190 (
            set mod=8190
        )
        if "!s:~%%Z,1!" neq "" (
            set /a "len+=%%Z"
            set "s=!s:~%%Z!"

        ) else (
            if "!s:~%%A,1!" neq "" (
                set /a "len+=%%A"
                set "s=!s:~%%A!"
            )
        )
    )
  )
  endlocal & if "%~2" neq "" (set %~2=%len%) else echo **%len%**
exit /b
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 2
    Interesting take on strlen using power of 3. But it fails with length greater than 6560. The [DosTips strlen](http://www.dostips.com/forum/viewtopic.php?p=6267#p6267) using power of 2 method is much simpler, it works up to nearly max string length, and it is anywhere from 17% to 35% faster. – dbenham Sep 21 '14 at 14:49
  • 2
    Also, this solution will strip empty lines, or lines that begin with `;`. Using FINDSTR to prefix each line with line number can fix this problem. – dbenham Sep 21 '14 at 14:51
  • @dbenham - forgot about the `;` now it uses `FINDSTR`. What bugles me is the bug in strlen. I've used the powers of 3 to reduce the iterations , but not sure if this helps for the speed :-).Now thinking how to increase the lenght that it can calculate. – npocmaka Sep 21 '14 at 15:18
  • It is an interesting academic exercise, but not practical. I'm confident the binary search will always be faster. – dbenham Sep 21 '14 at 15:22
  • 1
    Btw. Your way of using the `findstr /n` with `delims=:` fails when a line begins with one or more `:` [SO:How to read a file?](http://stackoverflow.com/a/4531090/463115) – jeb Sep 21 '14 at 16:13
  • As always there are some long discussions about strlen at dostips [strLen boosted](http://www.dostips.com/forum/viewtopic.php?f=3&t=1429&start=0), there are better threads even, but I can't find them – jeb Sep 21 '14 at 16:17
  • @jeb - I know I can use your [strlen macro](http://www.dostips.com/forum/viewtopic.php?p=25302#p25302) , but all the fun of scripting will go away if I'm not trying to do something by myself :) – npocmaka Sep 21 '14 at 16:23
  • @jeb @dbenham - strlen fixed (I think) and the lines that starts with `;` or `:` .Still is not bulletproof but should be better. – npocmaka Sep 21 '14 at 16:33