Remove gargage characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Remove gargage characters

#1 Post by Docfxit » 11 Nov 2020 12:02

I have a bat file that I would like to find a solution remove the garbage characters after Diskpart runs.
" ÿþ"

The bat file:

Code: Select all

bcdedit /enum all >"%~dp0FixBootBCD.txt"
bcdedit | find "osdevice" >>"%~dp0FixBootBCD.txt"

(echo SELECT DISK 0
echo SELECT PARTITION 1
echo GPT ATTRIBUTES=0x0000000000000001 
echo active 
echo exit  
)  | diskpart >>"%~dp0FixBootBCD.txt"

bootrec /rebuildbcd >>"%~dp0FixBootBCD.txt"
bootrec /fixmbr >>"%~dp0FixBootBCD.txt"
bootrec /fixboot >>"%~dp0FixBootBCD.txt"
bootrec /scanos >>"%~dp0FixBootBCD.txt"
bootsect /nt60 C: >>"%~dp0FixBootBCD.txt"
bcdboot C:\Windows >>"%~dp0FixBootBCD.txt"

attrib -h C:\EFI\Microsoft\Boot\*.* >>"%~dp0FixBootBCD.txt"
dir C:\EFI\Microsoft\Boot\*.* >>"%~dp0FixBootBCD.txt"
@cmd /k
The output file looks like this:

Code: Select all


DISKPART> 
Leaving DiskPart...
ÿþS c a n n i n g   a l l   d i s k s   f o r   W i n d o w s   i n s t a l l a t i o n s . 
 
 P l e a s e   w a i t ,   s i n c e   t h i s   m a y   t a k e   a   w h i l e . . . 
 
 S u c c e s s f u l l y   s c a n n e d   W i n d o w s   i n s t a l l a t i o n s . 
 T o t a l   i d e n t i f i e d   W i n d o w s   i n s t a l l a t i o n s :   0 
 T h e   o p e r a t i o n   c o m p l e t e d   s u c c e s s f u l l y . 
 ÿþT h e   o p e r a t i o n   c o m p l e t e d   s u c c e s s f u l l y . 
 ÿþT h e   o p e r a t i o n   c o m p l e t e d   s u c c e s s f u l l y . 
 ÿþS c a n n i n g   a l l   d i s k s   f o r   W i n d o w s   i n s t a l l a t i o n s . 
 
 P l e a s e   w a i t ,   s i n c e   t h i s   m a y   t a k e   a   w h i l e . . . 
 
 S u c c e s s f u l l y   s c a n n e d   W i n d o w s   i n s t a l l a t i o n s . 
 T o t a l   i d e n t i f i e d   W i n d o w s   i n s t a l l a t i o n s :   0 
 T h e   o p e r a t i o n   c o m p l e t e d   s u c c e s s f u l l y . 
 Target volumes will be updated with BOOTMGR compatible bootcode.

C: (\\?\Volume{1ddbcde8-60bc-4d00-aacf-9efdb7621e4e})

    Successfully updated NTFS filesystem bootcode.

Bootcode was successfully updated on all targeted volumes.
Failure when attempting to copy boot files.
Path not found - C:\EFI\Microsoft\Boot

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

Re: Remove gargage characters

#2 Post by aGerman » 11 Nov 2020 12:24

Diskpart doesnt redirect UTF-16 to the file. But I guess bootrec does. The ÿþ is the textual representation of the UTF-16 byte order mark. And luckily your text editor likes you and displays null characters like spaces.

At a first thought, try to redirect the output to a temporary file. Then convert it using TYPE and append it to your log. You would have to do this subsequently for each bootrec command.

Untested:

Code: Select all

:: ...
>"%~dp0FixBootBCD.~tmp" bootrec /rebuildbcd
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /fixmbr
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /fixboot
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /scanos
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
del "%~dp0FixBootBCD.~tmp"
:: ...
Steffen

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Remove gargage characters

#3 Post by Docfxit » 11 Nov 2020 13:03

You are a genius. That worked really great.

I wanted to add the command to the output txt file so I could make the output clearer.
Do you have any suggestions as to how I can make it look nicer?

Code: Select all

Echo Run bootrec /rebuildbcd >>"%~dp0FixBootBCD.txt" 
>"%~dp0FixBootBCD.~tmp" bootrec /rebuildbcd
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
Echo Run bootrec /fixmbr >>"%~dp0FixBootBCD.txt" 
>"%~dp0FixBootBCD.~tmp" bootrec /fixmbr
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /fixboot
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /scanos
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootsect /nt60 C:
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bcdboot C:\Windows
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
del "%~dp0FixBootBCD.~tmp"
This is what the output is looking like:

Code: Select all

Leaving DiskPart...
Run bootrec /rebuildbcd  
Scanning all disks for Windows installations.

Please wait, since this may take a while...

Successfully scanned Windows installations.
Total identified Windows installations: 0
The operation completed successfully.
Run bootrec /fixmbr  
The operation completed successfully.
The operation completed successfully.
Scanning all disks for Windows installations.

Please wait, since this may take a while...

Successfully scanned Windows installations.
Total identified Windows installations: 0
The operation completed successfully.
Target volumes will be updated with BOOTMGR compatible bootcode.

C: (\\?\Volume{1ddbcde8-60bc-4d00-aacf-9efdb7621e4e})

    Successfully updated NTFS filesystem bootcode.

Bootcode was successfully updated on all targeted volumes.
Failure when attempting to copy boot files.
Path not found - C:\EFI\Microsoft\Boot

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: Remove gargage characters

#4 Post by T3RRY » 11 Nov 2020 13:37

Docfxit wrote:
11 Nov 2020 13:03
You are a genius. That worked really great.

I wanted to add the command to the output txt file so I could make the output clearer.
Do you have any suggestions as to how I can make it look nicer?

It really is up to you to choose how to format the output. It would probably be simpler to read a temp output file after it's written and use conditional testing or findstr to write the lines present to the final output file formatted as you see fit.

An example of a similar output formatting end goal can be seen here: https://stackoverflow.com/a/59482949/12343998

In which the scripts help file is supplemented with the relevent section of the Findstr commands Help file.

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

Re: Remove gargage characters

#5 Post by aGerman » 11 Nov 2020 13:48

I always recommend to write redirections at the beginning of the line. It results in a better alignment for the actual command (better readability), it prevents you from redirecting the space between the last letter and >> in your Echo commands, and it protects you from a couple of other side effects.

Code: Select all

>>"%~dp0FixBootBCD.txt" Echo Run bootrec /rebuildbcd
>"%~dp0FixBootBCD.~tmp" bootrec /rebuildbcd
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>>"%~dp0FixBootBCD.txt" Echo Run bootrec /fixmbr
>"%~dp0FixBootBCD.~tmp" bootrec /fixmbr
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /fixboot
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootrec /scanos
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bootsect /nt60 C:
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
>"%~dp0FixBootBCD.~tmp" bcdboot C:\Windows
>>"%~dp0FixBootBCD.txt" type "%~dp0FixBootBCD.~tmp"
del "%~dp0FixBootBCD.~tmp"

You could even insert more spaces between the file name and the command to make it more distinct.

FWIW Based on your log content in the initial post, I think you don't need the TYPE conversion for the output of bootsect and bcdboot.

Steffen

Post Reply