Passing Unicode parameters to Windows .bat file when rerunning it -
my .bat file looks this:
@echo off cd /d "%~dp0" if [%2]==[] ( set user=%username% ) else ( set user=%2% ) :getfile if [%1]==[] ( set /p file=enter file name : ) else ( set file=%~f1 echo file name: %~f1 ) :checkfile /f "useback tokens=*" %%a in ('%file%') set file=%%~a if not exist "%file%" ( echo error: not find file: %file% echo. ) :: check admin permissions >nul 2>&1 "%systemroot%\system32\cacls.exe" "%systemroot%\system32\config\system" if '%errorlevel%' == '0' ( goto gotadmin ) :: rerun batch admin rights echo set uac = createobject^("shell.application"^) > "%temp%\getadmin.vbs" echo uac.shellexecute "cmd", "/c """"%~f0"" ""%file%"" ""%user%""""", "%cd%", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit /b :gotadmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "%cd%" cd /d "%~dp0" echo. :eof pause exit /b
i have these 2 test files:
- c:\test\folder\ファイル.txt
- c:\test\フォルダ\file.txt
when run batch file above , drag 1 onto cmd window get:
, good.
when same 2, get:
when call uac.shellexecute, %file% isn't passed correctly.
how can around problem?
my preferred way of starting batch file administrator permissions create shortcut, , mark shortcut requiring administrator permissions.
first right-click foo.bat, create shortcut. open properties shortcut, click advanced… button , enable run administrator.
this has downside: can't drag file names onto resulting command prompt window. can drag file onto shortcut.
but if don't want or can't use shortcut?
you can avoid need write arbitrary unicode characters file passing file name argument script. if vbs file in ansi encoding, script host uses unicode internally.
so here how write vbs file , run it:
:: rerun batch admin rights echo set uac = createobject^("shell.application"^) > "%temp%\getadmin.vbs" echo uac.shellexecute "cmd", "/c """"%~f0"" """ + wscript.arguments.item(0) + """ ""%user%""""", "%cd%", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" "%file%" exit /b
Comments
Post a Comment