Windows command processor cmd.exe
processing a batch file is not designed for file contents modification tasks, especially not for UTF-8 encoded XML files. There are other script interpreters installed by default on Windows like Windows Script Host interpreting Visual Basic and JScript scripts and PowerShell which are much better for such a task.
However, here is a batch file for this task.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ConfigFile=%LOCALAPPDATA%\Accenda_Limited\Accenda.ICG.EndPoint.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config"
if exist "%ConfigFile%" goto ScanForConfig
echo ERROR: File "%ConfigFile%" not found.
exit /B 4
:ScanForConfig
set "LineNumber="
for /F "delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /L /N /C:"<setting name=\"WatchPath\" serializeAs=\"String\">" "%ConfigFile%" 2^>nul') do set /A LineNumber=%%I + 1
if defined LineNumber goto DesktopFolder
echo ERROR: Could not find ^<setting name="WatchPath" serializeAs="String"^>
echo in file "%ConfigFile%".
exit /B 3
:DesktopFolder
rem Determine the common desktop folder directly from Windows registry.
rem The first FOR loop is usually enough to get the common desktop folder.
set "CommonDesktop="
for /F "skip=1 tokens=1-3*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Common Desktop" 2^>nul') do if /I "%%I %%J" == "Common Desktop" if not "%%~L" == "" if "%%K" == "REG_SZ" (set "CommonDesktop=%%~L") else if "%%K" == "REG_EXPAND_SZ" call set "CommonDesktop=%%~L"
if not defined CommonDesktop for /F "skip=1 tokens=1-3*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop" 2^>nul') do if /I "%%I %%J" == "Common Desktop" if not "%%~L" == "" if "%%K" == "REG_SZ" (set "CommonDesktop=%%~L") else if "%%K" == "REG_EXPAND_SZ" call set "CommonDesktop=%%~L"
if not defined CommonDesktop set "CommonDesktop=\"
if "%CommonDesktop:~-1%" == "\" set "CommonDesktop=%CommonDesktop:~0,-1%"
if defined CommonDesktop if exist "%CommonDesktop%\" goto ScanForValue
if defined PUBLIC if exist "%PUBLIC%\Desktop\" set "CommonDesktop=%PUBLIC%\Desktop" & goto ScanForValue
if exist "%SystemDrive%\Users\Public\Desktop\" set "CommonDesktop=%SystemDrive%\Users\Public\Desktop" & goto ScanForValue
if defined ALLUSERSPROFILE if exist "%ALLUSERSPROFILE%\Desktop\" set "CommonDesktop=%ALLUSERSPROFILE%\Desktop" & goto ScanForValue
echo ERROR: Cannot determine the all users desktop folder.
exit /B 2
:ScanForValue
set "NewValue=<value>%CommonDesktop%\RSS Referrals</value>"
for /F "delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /L /N /C:"%NewValue%" "%ConfigFile%" 2^>nul') do if %LineNumber% == %%I goto NoUpdate
rem It is really necessary to update the value in the configuration file.
set "TempFile=%ConfigFile%.tmp"
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%ConfigFile%" 2^>nul') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=:<>/" %%J in ("!Line!") do if not %%J == %LineNumber% (
echo(!Line:*:=!
) else if "%%K" == "value " (
echo(!NewValue!
) else if "%%K" == "value" (
echo(!NewValue!
) else echo(%%K!NewValue!
endlocal
))>"%TempFile%"
move /Y "%TempFile%" "%ConfigFile%"
if not exist "%TempFile%" goto UpdateDone
del "%TempFile%"
echo ERROR: Failed to modify file "%ConfigFile%".
exit /B 1
:NoUpdate
echo There is nothing to update.
exit /B 0
:UpdateDone
echo Configuration update done successfully.
endlocal
ATTENTION:
This batch file works only if the XML file user.config
is as posted with always having a line with tag value
below the line with <setting name="WatchPath" serializeAs="String">
. Any other variant of XML file contents results could result in a damaged XML file after execution of the batch file.
Please read my answer on How to read and print contents of text file line by line? for information on how the line replacement is done.
I recommend to read also the Wikipedia article which describes the predefined Windows Environment Variables.
My answer on Making a directory not in the current directory describes the command lines uses to get the desktop folder whereby in this case the code is modified to get the common desktop folder for all users instead of the user's desktop folder.
The batch file does not modify the configuration file if the value to modify is already present in the line below the line with <setting name="WatchPath" serializeAs="String">
.
I recommend once again coding a PowerShell script for this task instead of using a batch file as it would be safer and much faster. I cannot be made responsible on an XML file damage caused by using this batch script on changed XML file contents.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
del /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
goto /?
if /?
move /?
rem /?
set /?
setlocal /?
PS: This batch file works even on Windows XP as long as the file to update is found which is not the case by default with %LOCALAPPDATA%
because of the environment variable LOCALAPPDATA
is not defined by default on Windows XP, just on Windows Vista and newer Windows versions.