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
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
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
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