Activer l'UTF8 sans forcément écrire dans un fichier
Dans n'importe quel langage, lorsqu'on lit des variables et que ses variables ont une chaîne avec des caractères UTF8 :
- soit on déclare le charset quelquepart dans le fichier du code (ex: en MS DOS, on déclare chcp 65001 >nul )
- soit on enregistre le fichier du code lu dans un format particulier comme l'UTF8, et le langage comprend qu'il doit interpréter dans ce format.
Ca donne que dans un .BAT ou .CMD, on peut avoir ce code, qui simplement crée une variable et lui met une chaîne en UTF8 :
Code:
1 2 3
| chcp 65001 >nul
set "var=D:\mon_répertoire_avec_accents"
echo %var% |
Dans Vbscript :
- ?
Je cherche et je cherche, tout ce que je trouve c'est des manip lourdes à inclure dans des fonctions, pour jouer avec des fichiers (lire/écrire dedans).
Ex : https://stackoverflow.com/questions/...le-in-vbscript
Mais simplement exécuter un truc simple, pas possible.
Donc si vous jouer avec des chemins comportant des accents, aucune manière simple de faire comprendre à ce foutu langage que c'est de l'UTF8.
L'instruction stream.Charset = "utf-8" ne fonctionne pas (source), et je ne vois pas comment utiliser /source-charset directement dans le code (source).
Voici mon code, sensé mettre à jour des raccourcis (.LNK) avec l'aide d'un .BAT qui le lance en lui passant en argument#1 un chemin de dossier/fichier :
Code:
1 2 3 4 5 6 7
|
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objShortcut = WshShell.CreateShortcut(WScript.Arguments.Item(0))
If left(objShortcut.TargetPath,10) = "D:\Machin\" then objShortcut.TargetPath = "D:\Truc\" + right(objShortcut.TargetPath, len(objShortcut.TargetPath)-10)
objShortcut.Save |
J'en ai marre, à chaque fois que je touche à un langage, c'est la croix et la bannière.
Le fichier .VBS est enregistré en UTF8 mais ça comprend que dalle : dès qu'il reçoit un chemin avec accents ou caractères spéciaux, il plante (j'ai pu débuguer et confirmer ça).
En amont le .BAT est bien en UTF8 (fichier et charset) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| @echo off
setlocal EnableDelayedExpansion
chcp 65001 >nul
cd /d "%~dp0"
:INIT
set /a "total=0"
set "batch_path=%~dp0"
set "batch_name=%~n0"
set "icons_dir=%~1"
:ERROR1
if not defined icons_dir (
CLS
echo.
echo.
echo *** ERROR : NO DIRECTORY DROPPED ONTO THE BATCH ^^! ***
echo.
echo THIS PROGRAM IS INTENDED TO SCAN A FOLDER AND
echo ITS SUB-FOLDERS, IN WICH ALL THE SHORTCUTS
echo WILL BE UPDATED TO CHANGE THEIR PATH, ACCORDING
echo TO THE ASSOCIATED .VBS FILE.
echo.
echo PLEASE DRAG AND DROP A FOLDER ONTO THIS PROGRAM.
echo.
echo.
pause>nul
goto :ERROR1
)
:CHECK_FOLDER
if defined icons_dir if not exist "!icons_dir!\*" (
:ERROR2
CLS
echo.
echo.
echo ERROR : THE ITEM DROPPED ONTO THE BATCH IS NOT A FOLDER ^^!
echo.
echo.
pause>nul
goto :ERROR2
)
cd /d "!icons_dir!"
:COUNT
rem call :scan_lnk_in_subdirectories "!cd!" "show"
call :scan_lnk_in_subdirectories "!cd!" "count"
:MENU
CLS
echo.
echo.
echo Folder dropped : !cd!
echo Total of shortcuts found : !total!
echo.
echo Type [u] to start the update.
echo ^(Please edit paths in the .vbs script before^)
echo.
echo.
set "input="
set /p input=
if "!input!"=="u" (goto :PROCEED) else (goto :MENU)
:PROCEED
set /a "num=0"
call :scan_lnk_in_subdirectories "!cd!" "update"
:END
echo.
echo.
echo PROGRAM HAS FINISHED.
echo.
echo Close this window or push a key to exit.
Pause>nul
endlocal
exit
:: ---- INTERNAL FUNCTIONS ----
:scan_lnk_in_subdirectories
for /f "usebackq tokens=* delims=" %%d in (`dir /S /A-D /B "%~1\*.lnk"`) do (
set "item=%%d"
if "%~2"=="count" (set /a "total=!total!+1")
if "%~2"=="show" (
set /a "total=!total!+1"
echo !total!. !item!
)
if "%~2"=="update" (
set /a "num=!num!+1"
title !num!/!total!
echo.
echo *** Treating : !item! ***
echo.
!batch_path!!batch_name!.vbs "!item!"
)
)
goto :eof |