Question A batch script which disables explorer's folder localization

Oct 18, 2018
13
0
10
0
Hi,
I'm trying to create a batch script that searches through the computer for desktop.ini files, and deletes the line LocalizedResourceName=.*, to let Windows explorer show the true folder names instead of the translated ones. (On a non-english system)
I've started from the script written here and I copy-pasted the snippet found here to make the script able to ask for admin permissions on his own.
This is actually the second or third iteration of the script and I still can't get it to work properly; for example, that weird ampersand structure is my attempt to implement a "proceed if success" logic, but apparently it never reaches the echo at the end.
I'd like to build a definitive and "correct" version of the script that performs what described above, I'm looking for suggestion like "you should do that this way instead", or "you see that? that's wrong, do this instead" or "is usually best to do that this way".
I am no batch expert but I'd really like to have a working, "smart", version of this script.
This is the current version:
Code:
  ::::::::::::::::::::::::::::::::::::::::::::
  :: Elevate.cmd - Version 4
  :: Automatically check & get admin rights
  :: see "https://stackoverflow.com/a/12264592/1016343" for description
  ::::::::::::::::::::::::::::::::::::::::::::
  @echo off
  CLS
  ECHO.
  ECHO =============================
  ECHO Running Admin shell
  ECHO =============================

:init
  setlocal DisableDelayedExpansion
  set cmdInvoke=1
  set winSysFolder=System32
  set "batchPath=%~0"
  for %%k in (%0) do set batchName=%%~nk
  set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
  setlocal EnableDelayedExpansion

:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  ECHO.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  ECHO **************************************

  ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
  ECHO args = "ELEV " >> "%vbsGetPrivileges%"
  ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
  ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
  ECHO Next >> "%vbsGetPrivileges%"

  if '%cmdInvoke%'=='1' goto InvokeCmd

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
  "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
  exit /B

:gotPrivileges
  setlocal & cd /d %~dp0
  if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

  ::::::::::::::::::::::::::::
  ::START
  ::::::::::::::::::::::::::::
  REM Run shell as admin (example) - put here code as you like

  setlocal EnableDelayedExpansion

  echo SCANNING D:\
  pushd "D:\"
  for /f "delims=" %%G in ('dir /a /b /s desktop.ini') do (
    find /i "LocalizedResourceName=" "%%~G" >nul
    if !errorlevel! == 0 (
      (takeown /f "%%~G" /a >nul) && (icacls "%%~G" /grant:r *S-1-5-32-544:F /q >nul) && (attrib -h -s "%%~G") && (type "%%~G" | findstr /i /v /c:"LocalizedResourceName=" > "%%~G") && (attrib +h +s "%%~G") && (echo found %%G)
    )
  )
  popd

  echo SCANNING C:\
  pushd "%systemdrive%\"
  for /f "delims=" %%G in ('dir /a /b /s desktop.ini') do (
    find /i "LocalizedResourceName=" "%%~G" >nul
    if !errorlevel! == 0 (
      (takeown /f "%%~G" /a >nul) && (icacls "%%~G" /grant:r *S-1-5-32-544:F /q >nul) && (attrib -h -s "%%~G") && (type "%%~G" | findstr /i /v /c:"LocalizedResourceName=" > "%%~G") && (attrib +h +s "%%~G") && (echo found %%G)
    )
  )
  popd

  pause
  exit /B
As you can see I've doubled the loops, one for C: or whatever and the second one is hard-coded for D: this is ugly, also I'd like to exclude in an elegant way irrelevant folders like Windows etc, that cause the script to hang on kilometres long paths.
This version has problems reading a lot of the desktop.ini files, and never reaches the (print) ECHO instruction at the end of the ampersand chain.

I also have uploaded the code to a gitHub repo! here

Thank you!
 

ASK THE COMMUNITY

TRENDING THREADS