Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 批处理文件用预定义文本替换文本文件中的特定行_Arrays_Batch File_Replace_Cmd - Fatal编程技术网

Arrays 批处理文件用预定义文本替换文本文件中的特定行

Arrays 批处理文件用预定义文本替换文本文件中的特定行,arrays,batch-file,replace,cmd,Arrays,Batch File,Replace,Cmd,我已经研究过类似的问题,但它们似乎对我不起作用,希望是一些简单的问题(对于专家来说) 这是一个配置文件,我正试图作为批处理文件的一部分进行编辑 <?xml version="1.0" encoding="utf-8"?> <configuration> <userSettings> <Accenda.ICG.EndPoint.Properties.Settings>

我已经研究过类似的问题,但它们似乎对我不起作用,希望是一些简单的问题(对于专家来说)

这是一个配置文件,我正试图作为批处理文件的一部分进行编辑

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <Accenda.ICG.EndPoint.Properties.Settings>
            <setting name="SiteURL" serializeAs="String">
                <value>https://nww.rss.scarboroughryedaleccg.nhs.uk/_vti_bin/Accenda.ICG.EndPoint.WCF</value>
            </setting>
            <setting name="FilePathToDelete" serializeAs="String">
                <value />
            </setting>
            <setting name="WatchPath" serializeAs="String">
                <value />
            </setting>
            <setting name="MonitorFolderChoice" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="UserName" serializeAs="String">
                <value />
            </setting>
            <setting name="Password" serializeAs="String">
                <value />
            </setting>
            <setting name="AutoLogin" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="PollingIntervalMinutes" serializeAs="String">
                <value>5</value>
            </setting>
            <setting name="RegistrySettingsImportedToAppConfig" serializeAs="String">
                <value>True</value>
            </setting>
        </Accenda.ICG.EndPoint.Properties.Settings>
    </userSettings>
</configuration>

https://nww.rss.scarboroughryedaleccg.nhs.uk/_vti_bin/Accenda.ICG.EndPoint.WCF
真的
真的
5.
真的
对于这一行(或2)这里


是否为空,或填充了“任意文件夹”的路径

我只希望它包含以下内容:-

        <setting name="WatchPath" serializeAs="String">
            <value>C:\Users\Public\Desktop\RSS Referrals</value>

C:\Users\Public\Desktop\RSS引用
如果该值包含路径,我可以组合使用findstr&FART.exe,但如果它为空,我就卡住了,非常感谢;-)

如果行包含路径,则为代码段:-

for /f "tokens=3 delims=>;<" %%a in ('findstr /C:"C:\Users" "C:\Users\%usr%\AppData\Local\Accenda_Limited\Accenda.ICG.EndPoint.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config"') do (
fart -i -c "C:\Users\%usr%\AppData\Local\Accenda_Limited\Accenda.ICG.EndPoint.exe_Url_m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config" "%%a" "C:\Users\Public\Desktop\RSS Referrals"
)

for/f“代币=3个delims=> Windows命令处理器
cmd.exe
处理批处理文件不是为文件内容修改任务而设计的,尤其不是为UTF-8编码的XML文件。默认情况下,Windows上还安装了其他脚本解释器,如解释Visual Basic和JScript脚本的Windows脚本主机和PowerShell,它们更适合执行此类任务

但是,这里有一个用于此任务的批处理文件

@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
@echo关闭
setlocal EnableExtensions DisableDelayedExpansion
设置“ConfigFile=%LOCALAPPDATA%\Accenda\u Limited\Accenda.ICG.EndPoint.exe\u Url\u m5l4ujc5t22f3qw0q5uz1dwlfcnrdaoh\1.0.0.15\user.config”
如果存在“%ConfigFile%”,请转到ScanForConfig
回显错误:找不到文件“%ConfigFile%”。
出口/B 4
:ScanForConfig
设置“行号=”
对于(“%SystemRoot%\System32\findstr.exe/L/N/C:“%ConfigFile%”2^>nul)中的/F“delims=:”%%I,请设置/A行号=%%I+1
如果定义了行号,则转到DesktopFolder
回显错误:找不到^
文件“%ConfigFile%”中的回显。
出口/B 3
:DesktopFolder
rem直接从Windows注册表确定公用桌面文件夹。
rem第一个FOR循环通常足以获取公共桌面文件夹。
设置“CommonDesktop=”
对于/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')如果/I“%%I%%J”==“Common Desktop”如果不是“%%L”==“if”%%K”==“reg\u SZ”(设置“CommonDesktop=%%L”)如果“%%K=”设置“%%K=”reg\u扩展“\u SZ”调用设置“CommonDesktop=%L”
如果未在(“%SystemRoot%\System32\reg.exe QUERY”HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders“/v”Common Desktop“2^>nul”)中为/F“skip=1 tokens=1-3*”%%I执行/I“%%I%%J”==“Common Desktop”如果不是“%%L”==“if”%%K”==“reg\u SZ”(设置“CommonDesktop=%%L”)如果“%%K”==“reg\u扩展”调用集“CommonDesktop=%%~L”
如果未定义CommonDesktop,则设置“CommonDesktop=\”
如果“%CommonDesktop:~-1%”==“\”设置“CommonDesktop=%CommonDesktop:~0,-1%”
如果定义了CommonDesktop如果存在“%CommonDesktop%\”转到ScanForValue
如果定义了PUBLIC如果存在“%PUBLIC%\Desktop\”设置“CommonDesktop=%PUBLIC%\Desktop”&转到ScanForValue
如果存在“%SystemDrive%\Users\Public\Desktop\”设置“CommonDesktop=%SystemDrive%\Users\Public\Desktop”&转到ScanForValue
如果定义了ALLUSERSPROFILE(如果存在)“%ALLUSERSPROFILE%\Desktop\”设置“CommonDesktop=%ALLUSERSPROFILE%\Desktop”&转到ScanForValue
回显错误:无法确定“所有用户”桌面文件夹。
出口/B 2
:ScanForValue
设置“NewValue=%CommonDesktop%\RSS引用”
对于/F“delims=:”(“%SystemRoot%\System32\findstr.exe/L/N/C:“%NewValue%”“%ConfigFile%”2^>nul)中的%%I,如果%LineNumber%==%I转到NoUpdate,则执行该操作
rem确实需要更新配置文件中的值。
设置“TempFile=%ConfigFile%.tmp”
(对于/F delims^=^eol^=%%I in(“%SystemRoot%\System32\findstr.exe/N”^“%ConfigFile%”2^>nul)是否执行(
设置“行=%%I”
setlocal EnableDelayedExpansion
对于(“!Line!”)中的/F“tokens=1,2 delims=:/”%%J,如果不是%%J==%LineNumber%(
回音(!行::=!
)如果“%%K”==“值”(
echo(!NewValue!
)如果“%%K”==“值”(
echo(!NewValue!
)else回显(%%K!NewValue!
端部
))>%TempFile%
移动/Y“%TempFile%”%ConfigFile%
如果不存在“%TempFile%”,则转到UpdateOne
删除“%TempFile%”
回显错误:修改文件“%ConfigFile%”失败。
退出/B 1
:NoUpdate
没有什么要更新的。
退出/b0
:UpdateOne
已成功完成回显配置更新。
端部
注意:
此批处理文件仅在XML文件
user.config
与始终在
行下方有一行标记
value
的情况下工作。XML文件内容的任何其他变体都可能导致批处理文件执行后XML文件损坏

请阅读我的答案,了解如何更换线路

我还建议阅读维基百科的文章,其中描述了预定义的

我对的回答描述了用于获取桌面文件夹的命令行,在本例中,代码被修改为获取所有用户的公共桌面文件夹,而不是用户的桌面文件夹

如果要修改的值已存在于带有
的行下方的行中,则批处理文件不会修改配置文件

我建议再次为此任务编写PowerShell脚本,而不是使用批处理文件,因为这样会更安全、更快。对于在更改的XML文件内容上使用此批处理脚本造成的XML文件损坏,我不承担任何责任

要了解所使用的命令及其工作方式,请打开w
@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