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

[文本处理] 紧急求助替换php、html等文件里的汉字会乱码

  1. rem 定义要替换的新旧字符串
  2. cd %cd%
  3. set strOld=www.baidu.com
  4. set strNew=www.qq.com
  5. for /f "tokens=*" %%i in ('dir *.txt,*.php,*.jsp,*.ini,*.html,*.mxl,*.css,*.js,*.tpl,*.json,*.config /S /B') do (
  6.     set "var=%%i"
  7.     setlocal enabledelayedexpansion
  8.     if not !var!.==. (
  9.     for /f "usebackq tokens=*" %%j in ("!var!") do (
  10.         set "tmp=%%j"
  11.         if not !tmp!.==. (
  12.           set "tmp=!tmp:%strOld%=%strNew%!"
  13.           echo !tmp!>>temp.txt
  14.         )
  15.     )
  16.     move temp.txt "!var!"
  17.     endlocal
  18.     )
  19. )
复制代码
经过测试 只有 txt文本文件里的 汉字不会乱码, php  html   .config  等这些文件 里的汉字 都会乱码,

经过测试 解决方法  加个 Chcp 65001  就解决了 哎哎

回复 2# red2020

用啊 不会不得问问吗

TOP

不只是编码错误, 还会把 <!--  --> 其中的 !  叹号删除, 包括 for 开头的 英文也会给删除

大神们 帮帮孩子吧

TOP

回复 5# flashercs

感谢老板 你这个方法我试过了,确实不会乱码了, 但是还是会把 <!--这个里的叹号给自动删除 -->

还有里面的  <!-- For more information on Entity Framework configuration, visit 这一段 For 开头的 会被都删掉

TOP

回复 6# zaqmlp

感谢帮忙 我电脑是 win7系统 使用你的这个发现是正常的 不会乱码 也不会乱删除 for 开头的英文  只是出个错误提示 不知道有啥用

还有就是 替换完  会把 UTF-8 无 BOM格式的 文件 都变成带 BOM 的编码。

原文件是 无 BOM格式 替换完的就变成 图片这种了

TOP

回复 9# zaqmlp

好的老板 , 我查看发现 原文件 web.config 文件默认是带 BOM ,PHP文件是不带 BOM

替换后 都变为 不带 BOM 或者都带 BOM 了, 这个能根据原文件编码 自动识别吗?

稍后我给支付宝绑个卡 ,小红包感谢!或者给我个微信码 微信里有钱

TOP

回复 12# flashercs

哥哥 你啥意思? 还好我用的备份文件测试 你这玩意直接把文件清空了 很是牛-逼 啊
  1. New-Object : 找不到“StreamReader”的重载,参数计数为:“5”。
  2. 所在位置 行:21 字符: 21
  3. +     $sr = New-Object <<<<  System.IO.StreamReader -ArgumentList @($filestream
  4. , $iso88591, $false, 64000, $true)
  5.     + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodExcept
  6.    ion
  7.     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
  8.    Shell.Commands.NewObjectCommand
  9. 不能对值为空的表达式调用方法。
  10. 所在位置 行:23 字符: 25
  11. +     $str = $sr.ReadToEnd <<<< ().Replace($oldstr, $newstr)
  12.     + CategoryInfo          : InvalidOperation: (ReadToEnd:String) [], Runtime
  13.    Exception
  14.     + FullyQualifiedErrorId : InvokeMethodOnNull
  15. 不能对值为空的表达式调用方法。
  16. 所在位置 行:27 字符: 16
  17. +     $sr.Dispose <<<< ()
  18.     + CategoryInfo          : InvalidOperation: (Dispose:String) [], RuntimeEx
  19.    ception
  20.     + FullyQualifiedErrorId : InvokeMethodOnNull
  21. New-Object : 找不到“StreamReader”的重载,参数计数为:“5”。
  22. 所在位置 行:21 字符: 21
  23. +     $sr = New-Object <<<<  System.IO.StreamReader -ArgumentList @($filestream
  24. , $iso88591, $false, 64000, $true)
  25.     + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodExcept
  26.    ion
  27.     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
  28.    Shell.Commands.NewObjectCommand
  29. 不能对值为空的表达式调用方法。
  30. 所在位置 行:23 字符: 25
  31. +     $str = $sr.ReadToEnd <<<< ().Replace($oldstr, $newstr)
  32.     + CategoryInfo          : InvalidOperation: (ReadToEnd:String) [], Runtime
  33.    Exception
  34.     + FullyQualifiedErrorId : InvokeMethodOnNull
  35. 不能对值为空的表达式调用方法。
  36. 所在位置 行:27 字符: 16
  37. +     $sr.Dispose <<<< ()
  38.     + CategoryInfo          : InvalidOperation: (Dispose:String) [], RuntimeEx
  39.    ception
  40.     + FullyQualifiedErrorId : InvokeMethodOnNull
  41. 请按任意键继续. . .
复制代码

TOP

返回列表