[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
网盘下载的 xml ," 被转义,行字符数超过 cmd 的最大值

用 powershell 方便些
  1. $ht = @{
  2. "CPCIssueOrg" = "车      :"
  3. "EnTime"      = "时间    :"
  4. "CPUVlp"      = "IC卡车牌:"
  5. "EnVLP"       = "入口车牌:"
  6. "OBUVlp"      = " OBU车牌:"
  7. "VLP"         = "出口车牌:"
  8. }
  9. $filename = ".\20240526080607480603.xml"
  10. [xml]$xml = Get-Content -Path $filename -Encoding "UTF8"
  11. $xml.datas.data.attribute | ForEach-Object {
  12. if ( $_.name -eq "msg" ) { $string = $_.value }
  13. }
  14. $string = $string.Substring(1,$string.length-2)
  15. $string = $string -replace '(?<="),',"`n"
  16. $string = $string -replace '"(\w+)":"(.*)"',"`$1`t`$2"
  17. $as = $string -split "`n"
  18. $as | ForEach-Object {
  19. $a = $_ -split "`t"
  20. if ( $ht[$a[0]] ) {
  21. Write-Host ( "{0}   {1}" -f $ht[$a[0]] , $a[1] )
  22. }
  23. }
复制代码

TOP

回复 7# licunwei

从回报信息看,楼主修改了源文件d:\xml\test.xml的路径为d:\test.xml,但要确保其修改路径的真实性,以及输出结果文件的路径...
我用楼主一楼提供的源文件示例已经完全测试通过,只要按照源文件和脚本文件的编码一致性正确操作即可...

TOP

回复 6# aloha20200628

老师 帮忙再看看呢 匹配不上
C:\Users\Administrator\Desktop\新建文件夹>for %c in ("CPCIssueOrg:车站名称" "EnTime:时间" "CPUVlp:IC卡车牌" "EnVLP:入口车牌" "OBUVlp:OBU车牌" "VLP:出口车牌") do for /F "tokens=1-2 delims=:" %1 in ("%~c") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("CPCIssueOrg:车站名称") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "CPCIssueOrg=车站名称" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("EnTime:时间") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "EnTime=时间" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("CPUVlp:IC卡车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "CPUVlp=IC卡车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("EnVLP:入口车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "EnVLP=入口车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("OBUVlp:OBU车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "OBUVlp=OBU车牌" )

C:\Users\Administrator\Desktop\新建文件夹>for /F "tokens=1-2 delims=:" %1 in ("VLP:出口车牌") do (set "%~1=%~2" )

C:\Users\Administrator\Desktop\新建文件夹>(set "VLP=出口车牌" )

C:\Users\Administrator\Desktop\新建文件夹>(for /F "usebackq skip=9 delims=" %a in ("d:\test.xml") do (set "s=%a"  & for %k in ("CPCIssueOrg" "EnTime" "CPUVlp" "EnVLP" "OBUVlp" "VLP") do (set "v=!s:*%k=!"  & for /F tokens=1delims=" %b in ("!v:~2,25!") do echo,!%~k!:%b )
chcp 936 1>nul  & endlocal & exit/b) )

C:\Users\Administrator\Desktop\新建文件夹>pause
Press any key to continue . . .

TOP

本帖最后由 aloha20200628 于 2024-5-27 14:42 编辑

回复 1# licunwei

假设一楼测试文件或源文件是utf-8编码,存于 d:\xml\test.xml,则以下代码须用utf-8编码存为test.bat运行,其输出结果文件是 d:\xml\test.txt
  1. @echo off &setlocal enabledelayedexpansion &chcp 65001>nul
  2. for %%c in ("CPCIssueOrg:车站名称", "EnTime:时间", "CPUVlp:IC卡车牌", "EnVLP:入口车牌", "OBUVlp:OBU车牌", "VLP:出口车牌") do for /f "tokens=1-2 delims=:" %%1 in ("%%~c") do (set "%%~1=%%~2")
  3. (for /f "usebackq skip=9 delims=" %%a in ("d:\xml\test.xml") do (
  4. set "s=%%a"&for %%k in ("CPCIssueOrg", "EnTime", "CPUVlp", "EnVLP", "OBUVlp", "VLP") do (
  5. set "v=!s:*%%k=!"&for /f tokens^=^1^delims^=^" %%b in ("!v:~2,25!") do echo,!%%~k!:%%b
  6. )
  7. chcp 936>nul&endlocal&exit/b
  8. ))>"d:\xml\test.txt"
复制代码
假设一楼测试文件或源文件是ansi编码,存于 d:\xml\test.xml,则以下代码须用ansi编码存为test.bat运行,其输出结果文件是 d:\xml\test.txt
  1. @echo off &setlocal enabledelayedexpansion
  2. for %%c in ("CPCIssueOrg:车站名称", "EnTime:时间", "CPUVlp:IC卡车牌", "EnVLP:入口车牌", "OBUVlp:OBU车牌", "VLP:出口车牌") do for /f "tokens=1-2 delims=:" %%1 in ("%%~c") do (set "%%~1=%%~2")
  3. (for /f "usebackq skip=9 delims=" %%a in ("d:\xml\test.xml") do (
  4. set "s=%%a"&for %%k in ("CPCIssueOrg", "EnTime", "CPUVlp", "EnVLP", "OBUVlp", "VLP") do (
  5. set "v=!s:*%%k=!"&for /f tokens^=^1^delims^=^" %%b in ("!v:~2,25!") do echo,!%%~k!:%%b
  6. )
  7. endlocal&exit/b
  8. ))>"d:\xml\test.txt"
复制代码
1

评分人数

TOP

4楼代码 -replace '"' 改成 -replace '&去掉quot;'
去掉&后的“去掉”二字
bat小白,请多指教!谢谢!

TOP

借助ai写了一条powershell换行命令。
  1. @echo off
  2. rem "批处理保存为utf-8编码格式"
  3. chcp 65001 >nul
  4. cd /d "%~dp0"
  5. set "file=D:\20240526080607480603.xml"
  6. set "_"CPCIssueOrg"=    "
  7. set "_"EnTime"=时间:"
  8. set "_"CPUVlp"=IC卡车牌:"
  9. set "_"EnVLP"=入口车牌:"
  10. set "_"OBUVlp"=OBU车牌:"
  11. set "_"VLP"=出口车牌:"
  12. (for /f "delims=" %%a in ('powershell -Command "(Get-Content '%file%' -Encoding UTF8) -replace '"', [Environment]::NewLine" ^|findstr /rvb "; , :"') do (
  13. if defined _"%%a" (
  14. call set str=%%_"%%a"%%
  15. ) else if defined str (
  16. call echo=%%str%%%%a
  17. set str=
  18. )
  19. ))>"out.txt"
  20. pause
复制代码
bat小白,请多指教!谢谢!

TOP

回复 2# Batcher


以上传附件链接。

TOP

回复 1# licunwei


    请把xml文件上传到网盘,以便测试代码。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表