如何使用批处理脚本在 txt 格式的报告中查找逗号的出现并将其替换为 space

How to find the occurrence of comma in a report of txt format and replace it with space using batch script

我在 D:\folder\data\data1 中有很多 txt 格式的报告。逗号的出现不是 predictable.Where 在我需要替换的 txt 文件中找到逗号a space.so 我必须对多个文本文件执行此操作

例如:报告中的数据会像

abacndl      123455      300,90.55     aldfankas    400,123,455.900
adsmnfnka    12345774    134,789.9     aeraermk     456,987,87.000

并且同样明智地继续

有人可以帮我为这个场景做一个批处理脚本吗

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR %%a IN (*.txt) DO (
 FOR /f "usebackq delims=" %%b IN ("%%a") DO (
  SET "line=%%b"
  >>"%%~na.XTX" echo(!line:,= !
 )
)
rem delete existing text files and renam new version to .txt
del *.txt
ren *.xtx *.txt
popd

GOTO :EOF

您需要更改 sourcedir 的设置以适合您的情况。

  • 切换到目标目录,
  • 对于每个.txt文件,读取每一行到%%b,将其保存在line中进行处理,并输出line处理以替换每个, Space

为新输出文件选择的名称是 %%~na.xtx - 即带有新扩展名的原始名称 xtx。这样,您可以在使用前轻松检查新格式。我假设在 运行.

之前不存在 .xtx 个文件

我不明白上面del/ren的问题;它非常适合我。

这是另一个版本 - 只有重命名代码不同。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR %%a IN (*.txt) DO (
 FOR /f "usebackq delims=" %%b IN ("%%a") DO (
  SET "line=%%b"
  >>"%%~na.XTX" echo(!line:,= !
 )
)
rem rename new version to .txt - alternative codeline
FOR %%a IN (*.xtx) DO (MOVE /y "%%~na.XTX" "%%~na.txt") >nul
popd

GOTO :EOF