Create nul and all ascii characters with only batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Create nul and all ascii characters with only batch

#1 Post by carlos » 27 Jan 2014 12:40

Edit: This post begin for post code for create only nul charater. After the work was for create all 256 ascii characters from 0 to 255.

Final simple code for create a byte character (.chr file) using only batch:
You need pass the ascii number that you desire as parameter.
For example, for create the tab character:
genchr.cmd 9
rem 9.chr will be created

Edit Begin: 2018-may-06
genchr.cmd (with fix for run with native double byte codepage)
viewtopic.php?f=3&t=5326&p=56708#p56709
End Edit

genchr.cmd

Code: Select all

REM This code creates one single byte. Parameter: <int>0-255
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8

@echo off

set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26  (type nul >%~1.tmp
makecab %options% /d reserveperfoldersize=%~1 %~1.tmp %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">%~1.tmp)
>nul copy /y %~1.tmp /a %~1.chr /b
del %~1.tmp
) else (copy /y nul + nul /a 26.chr /a >nul)

genchr.cmd also is available in a version with syntax:

Code: Select all

http://ss64.com/nt/syntax-genchr.html
Link to code for create all the ascii characters using only batch:
speedy stable carlos' version: http://www.dostips.com/forum/viewtopic. ... 495#p32495
speedy stable dbenham' version: http://www.dostips.com/forum/viewtopic. ... 485#p32485
first stable code:

Code: Select all

http://www.dostips.com/forum/viewtopic.php?p=32184#p32184
For only create the nul character:
A really improved and better version is found in this link:

Code: Select all

http://www.dostips.com/forum/viewtopic.php?p=32120#p32120
Original title of post: createnul.cmd Create a file with the nul character

Hello.
Tested on win8.
Edit: Modification N° 3.

Code: Select all

@Echo Off

::This routine create the file zero.chr that only have the nul character
::Author: Carlos

Set "p=Pause>Nul"
Set "q=%p%&%p%&%p%&%p%&%p%&%p%&%p%&%p%&%p%"
Set "r=%q%%q%%q%%q%%q%%q%%q%%q%%q%"
Set "s=%r%&%p%&%p%"

Copy /Y Nul+Nul /A sub.chr /A >Nul
Makecab /D Compress=OFF sub.chr tmp.cab >Nul
Type tmp.cab |(%s%&Findstr "^") >wnul.tmp

Copy /Y wnul.tmp /A zero.chr /B >Nul
Del sub.chr wnul.tmp tmp.cab

Rem zero.chr have the nul character

Last edited by carlos on 06 May 2018 14:59, edited 17 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: createnul.cmd Create a file with the nul character

#2 Post by aGerman » 27 Jan 2014 14:36

Nice find carlos!

Basically the same but using FORFILES for creating the SUB:

Code: Select all

@echo off &setlocal

>"crlfX0sub0.tmp" forfiles /p "%~dp0." /m "%~nx0" /c "cmd /u /c \"<nul set /p \"=X0x1A\"\""
type "crlfX0sub0.tmp" | >"0sub0crlf.tmp" (pause>nul &pause>nul &pause>nul &findstr "^")
>nul copy /y "0sub0crlf.tmp" /a "zero.chr" /b

del "crlfX0sub0.tmp" "0sub0crlf.tmp"


Regards
aGerman

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#3 Post by carlos » 27 Jan 2014 14:42

How it Works?:

We create a cabinet file without compression with a file that only have the ascii sub character (1A (hex) or 26 (dec)). This character is used as end of file for copy files in ascii mode. Also it stop the type command.

creation of sub.chr that only have the sub character:
Copy /Y Nul+Nul /A sub.chr /A >Nul


creation of the cabinet file without compression:
Makecab /D Compress=OFF sub.chr tmp


The cabinet file have this content:

Image

Now we need ignore the first 75 bytes until 00 1A (in this case are 75 bytes because the name of the file is 7 characters: sub.chr)

For it we use a block of code like this where each Pause>Nul will remove one character:

Code: Select all

(
Pause >Nul
Pause >Nul
...
Findstr "^"
)


The Findstr will output the input and also add
0D 0A
.

Then we generate the input with Type command to the previous block of code and we redirect this to a file.
This file will have these characters:
00 1A 0D 0A


Then copying the file in ascii mode we only copy the file until 1A character. Then we have the file with 00 character content.

The way of remove first characters of a file I learned from walid2mi technique mentions here:

Code: Select all

http://www.dostips.com/forum/viewtopic.php?p=25225#p25225
and copy the files on ascii mode with sub character for remove the sub and next characters from jeb here:

Code: Select all

http://www.dostips.com/forum/viewtopic.php?p=23564#p23564

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: createnul.cmd Create a file with the nul character

#4 Post by penpen » 27 Jan 2014 14:46

I changed your version to not need makecab anymore (only "external tools": cmd.exe, and findstr.exe :D ).
It works at least using my win xp; maybe test it on other windows versions:

Code: Select all

@Echo Off

::This routine create the file zero.chr that only have the nul character
::Author: Carlos

Copy /Y Nul+Nul sub.chr >Nul
cmd /U /C set /P "=a" < nul > a.chr
copy /Y /B a.chr + /B sub.chr /B asub.txt > nul
type asub.txt | (pause > nul &Findstr "^") > wnul.tmp
Copy /Y wnul.tmp /A zero.chr /B >Nul
Del a.chr sub.chr asub.txt wnul.tmp

Rem zero.chr have the nul character

penpen

Edit1+2: corrected some flaws.
Edit3: added /y to the copy commands, as you suggested, carlos
Edit4+5+6: removed the "set q...", "set p..." lines (now the "set p.." line is really removed...)
Last edited by penpen on 27 Jan 2014 15:34, edited 6 times in total.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#5 Post by carlos » 27 Jan 2014 14:51

Nice penpen, it Works on my windows 8. Your code improved my original code.
I only added the /y to copy command (as on my modification 3) because if the file with sub character exists copy command will prompt for overwriting and because the redirection is to nul you not view the message.
@aGerman: forfiles is not present on all Windows versions, using copy is more portable for create the file with sub character.

Thanks to aGerman and penpen for improve the code with ideas. Both use a Unicode string where each character are two bytes, the last ended with 00. With it makecab is unnecesary.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: createnul.cmd Create a file with the nul character

#6 Post by aGerman » 27 Jan 2014 15:17

carlos wrote:@aGerman: forfiles is not present on all Windows versions, using copy is more portable for create the file with sub character.

I wasn't aware. Anyway it may come in handy. So I improved my code to avoid the FOR /F loop and added the /Y switch as you suggested.

Regards
aGerman

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#7 Post by carlos » 27 Jan 2014 15:43

penpen.
I optimized the code for only use two temp files.
Last edited by carlos on 27 Jan 2014 16:41, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: createnul.cmd Create a file with the nul character

#8 Post by penpen » 27 Jan 2014 16:03

Now it only needs one temporary file :D .

Code: Select all

@Echo Off

::This routine create the file zero.chr that only have the nul character
::Tested on windows 8, winxp 32 home, win xp 32 prof, windows 7 32bit
::Authors: carlos, aGerman, penpen

Cmd /U /C Set /P "=a" <Nul > zero.chr
Copy /Y zero.chr+Nul zero.chr >Nul
Type zero.chr |(Pause>Nul &Findstr "^") > wnul.tmp
Copy /Y wnul.tmp /A zero.chr /B >Nul
Del wnul.tmp

Rem zero.chr have the nul character

penpen

Edit1-2: Changed authors 8) , added test systems.
Last edited by penpen on 27 Jan 2014 17:18, edited 2 times in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: createnul.cmd Create a file with the nul character

#9 Post by Squashman » 27 Jan 2014 16:04

This is another thing that needs to go into the Library. I have seen a few things over the last few months that I think should be in there. Is Peter the only one who can update that?

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: createnul.cmd Create a file with the nul character

#10 Post by einstein1969 » 27 Jan 2014 16:08

Carlos, aGerman, penpen, You are great!

I like this tricks! Very good job!

tested on windows 7 32bit. all works!

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#11 Post by carlos » 27 Jan 2014 16:10

@penpen: Nice last modification, only use a file :o
Please, you can use this header:

Code: Select all

::Authors: carlos, aGerman, penpen
and please add on wich Windows you test it. I test your version using only one temp file and works on windows 8. I post a link in the first post to your last modification.

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: createnul.cmd Create a file with the nul character

#12 Post by Sponge Belly » 27 Jan 2014 16:14

Hello All! :-)

Taking from DT: Another Way to Create a Linefeed Variable and SO: How to Write a Single Space to a Text File, I came up with this little snippet:

Code: Select all

@echo off & setlocal enableextensions
(set lf=^

)
set nl=^^^%lf%%lf%^%lf%%lf%

set "lfFile=%tmp%\lf.tmp"
set "lfUTF=%tmp%\lfUTF.tmp"
set "nulFile=%tmp%\null.tmp"

echo(|(pause >nul&findstr "^" >"%lfFile%")
cmd /d /u /c type "%lfFile%" >"%lfUTF%"
findstr /v %nl% "%lfUTF%" >"%nulFile%"

fc /b "%lfFile%" "%nulFile%"
del "%lfFile%" "%lfUTF%"
endlocal & goto :EOF


Kudos to Walid2mi and Dave Benham without whom the above wouldn’t have been possible. I just put the pieces together. ;-)

- SB

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: createnul.cmd Create a file with the nul character

#13 Post by einstein1969 » 27 Jan 2014 16:37

Sponge Belly wrote:Hello All! :-)

Taking from DT: Another Way to Create a Linefeed Variable and SO: How to Write a Single Space to a Text File, I came up with this little snippet:

Code: Select all

@echo off & setlocal enableextensions
(set lf=^

)
set nl=^^^%lf%%lf%^%lf%%lf%

set "lfFile=%tmp%\lf.tmp"
set "lfUTF=%tmp%\lfUTF.tmp"
set "nulFile=%tmp%\null.tmp"

echo(|(pause >nul&findstr "^" >"%lfFile%")
cmd /d /u /c type "%lfFile%" >"%lfUTF%"
findstr /v %nl% "%lfUTF%" >"%nulFile%"

fc /b "%lfFile%" "%nulFile%"
del "%lfFile%" "%lfUTF%"
endlocal & goto :EOF


Kudos to Walid2mi and Dave Benham without whom the above wouldn’t have been possible. I just put the pieces together. ;-)

- SB


Hi Sponge Belly, I'm melting the brain! :D Thanks!

einstein1969

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#14 Post by carlos » 27 Jan 2014 16:50

thanks Sponge Belly for share your code (it show another way of create the file with nul).
In my case, I prefer the improved version linked on the first post, because it not needs environments variables and only use one temp file. But you code also can be useful.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: createnul.cmd Create a file with the nul character

#15 Post by carlos » 27 Jan 2014 18:19

I optimized the Sponge Belly code to this next:

Code: Select all

@Echo Off
::Authors: Sponge Belly, carlos
Echo(|(Pause >Nul &Findstr "^" >zero.chr)
(Cmd /u /c Type zero.chr ) >wnul.tmp
Set /P "=#" >>zero.chr <Nul
Set /P LF=<zero.chr
Findstr /v ^%LF:~0,1%%LF:~0,1% wnul.tmp > zero.chr
Del wnul.tmp


It use 4 write to disk operations like the code linked on the first post. The difference is that it set a environment variable unlike the linked on the first post.

The first column is the code linked on the first post, and the second column is the code in the current post.
Image

Post Reply