You are not logged in.

#1 29 Mar 2015 23:24

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

A2U.cmd [updated] convert ascii file to unicode file

Hello, time ago I wrote a script for convert a ascii file to unicode, using the type command, and generating the bom (byte order mark) using a trick with some oem  characteres and translating using the codepage 1251. But recently, because that characters are extended characters and can be corrupted for example if you copy it in some editor, I'm updated the script for not use that trick, instead it create the bom with the genchr procedure.

A2U.CMD: Convert ascii text file to unicode text file.
Usage: A2U.CMD source.txt [destination.txt]
If you don't specify a destination A2U.CMD will overwrite source.txt

A2U.CMD

@ECHO OFF
:: A2U
:: AUTHOR:  CARLOS MONTIERS AGUILERA
:: DATE:    10-MAY-2011
:: UPDATED: 17-MAR-2015
:: This script is provided 'as-is', without any express or implied
:: warranty.  In no event will the authors be held liable for any damages
:: arising from the use of this script.

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET ASCIIFile=%~f1
IF NOT DEFINED ASCIIFile GOTO :_A2UHLP_
IF "%~1"=="/?" GOTO :_A2UHLP_
IF /I "%~1"=="-H" GOTO :_A2UHLP_

IF NOT EXIST "!ASCIIFile!" (
	ECHO Error: file "!ASCIIFile!" not exist
	ENDLOCAL
	EXIT /B 1
)

FOR %%# IN ("!ASCIIFile!") DO IF ""=="%%~z#" (
	ECHO Error: could not read file "!ASCIIFile!"
	ENDLOCAL
	EXIT /B 1
)

SET UnicodeFile=%~f2
IF NOT DEFINED UnicodeFile SET UnicodeFile=%~f1
IF EXIST "!UnicodeFile!\" (
	ECHO Error: could not write to file "!UnicodeFile!"
	ENDLOCAL
	EXIT /B 1
)

FOR %%# IN ("!ASCIIFile!") DO SET A_SIZE=%%~z#
CMD /C EXIT /B %A_SIZE%
IF "%=ExitCode%" GTR "3FFFFFFE" (
	ECHO Error: File too big
	ENDLOCAL
	EXIT /B 1
)
SET /A E_SIZE=2+%A_SIZE%*2
SET "RANDOM="
:_A2U_TMP
SET TempFile=_%RANDOM%_.t_
IF EXIST "!TempFile!" GOTO :_A2U_TMP
CALL :GENCHR 255
CALL :GENCHR 254
COPY 255.CHR /B + 254.CHR /B "!TempFile!" >NUL 2>&1
CMD.EXE /D /U /C TYPE "!ASCIIFile!" >> "!TempFile!"
DEL /F /Q /A "!UnicodeFile!" >NUL 2>&1
MOVE /Y "!TempFile!" "!UnicodeFile!" >NUL 2>&1
IF ERRORLEVEL 1 (
	ECHO Error: could not write to file "!UnicodeFile!"
	ENDLOCAL
	EXIT /B 1
)
DEL 255.CHR 254.CHR
FOR %%# IN ("!UnicodeFile!") DO IF %%~z# NEQ %E_SIZE% (
	ECHO Error: "!UnicodeFile!" created with errors
	ENDLOCAL
	EXIT /B 1
)

EXIT /B 0

:_A2UHLP_
ECHO %~nx0 : Convert ascii text file to unicode text file.
ECHO Usage: %~nx0 source.txt [destination.txt]
ECHO If you don't specify a destination %~nx0 will overwrite source.txt
ENDLOCAL
EXIT /B 1

:GENCHR
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)

Last edited by carlos (29 Mar 2015 23:31)

Offline

#2 01 Apr 2015 22:43

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 446

Re: A2U.cmd [updated] convert ascii file to unicode file

Interesting task.
Will try to solve it with CERTUTIL.

Offline

Board footer

Powered by