Miguel Bartelsman
Miguel Bartelsman

Reputation: 349

Batch file output unicode

I have the following batch file:

dir *.jpg /b /s >> out.txt

It outputs the path for all jpg file in the current directory to a text file, the problem is that the directory is not mine, but shared through network, and the path contains spanish special characters. So instead of outputting:

---------------v---------
...\Participación ciud...

It outputs

---------------v---------
...\Participaci¢n ciud...

Upvotes: 1

Views: 2379

Answers (1)

Hackoo
Hackoo

Reputation: 18857

You should use CMD.exe with this option /U

/U     Output UNICODE characters (UCS-2 le)
          These options will affect piping or redirecting to a file.
          Most common text files are ANSI, use these switches
          when you need to convert the character set.

This code can did the trick to output the unicode characters :

@echo off
Set "TmpLogFile=%Tmp%\%~n0.txt"
Set "LogFile=%~dp0%~n0.txt"
If Exist "%TmpLogFile%" Del "%TmpLogFile%"
dir *.jpg /b /s >> %TmpLogFile%
REM Formatting the output to unicode
CMD /U /C Type "%TmpLogFile%" > "%LogFile%"
Start "" "%LogFile%"

For more options of CMD.exe /?

I used this trick in a more elaborate code like this one :

@ECHO OFF
Title Scan a folder and store all files names in an array variables
SET "ROOT=%userprofile%\Desktop\"
SET "EXT=jpg"
SET "Count=0"
Set "TmpLogFile=%Tmp%\%~n0.txt"
Set "LogFile=%~dp0%~n0.txt"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For %%a in (%EXT%) Do ( 
    Call :Scanning "*.%%a" & timeout /T 2 /Nobreak>nul
    FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
        Call :Scanning "%%f"
        SET /a "Count+=1"
        set "list[!Count!]=%%~nxf"
        set "listpath[!Count!]=%%~dpFf"
    )
)
::***************************************************************
:Display_Results
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 15
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO  ********************************************************
ECHO  Folder:"%ROOT%"
ECHO  ********************************************************
If Exist "%TmpLogFile%" Del "%TmpLogFile%"
rem Display array elements and save results into the TmpLogFile
for /L %%i in (1,1,%Count%) do (
    echo [%%i] : !list[%%i]!
    echo [%%i] : !list[%%i]! -- "!listpath[%%i]!" >> "%TmpLogFile%"     
)

( ECHO. & ECHO Total of [%EXT%] files(s^) : %Count% file(s^) )>> "%TmpLogFile%"
REM Formatting the TmpLogFile to the unicode LogFile :
CMD /U /C Type "%TmpLogFile%" > "%LogFile%"
ECHO(
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo    Type the number of file did you want to explore ?
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Call :Explorer "!listpath[%%i]!"
    )
)   
Goto:Display_Results
::**************************************************************
:Scanning <file>
mode con cols=75 lines=3
Cls & Color 0E
echo(
echo Scanning for "%~1" ...
goto :eof
::*************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::*************************************************************

Upvotes: 3

Related Questions