需要替换“"带有“"使用批处理文件处理csv文件中的所有记录 [英] Need to replace """ with "" for all records in a csv file using a batch file

查看:88
本文介绍了需要替换“"带有“"使用批处理文件处理csv文件中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在csv文件中用""替换""",以替换文件中包含这些字符串的任何行.

I need to replace """ with "" in a csv file for any line in the file which contains these string of characters.

基本上,我需要这个:

"Pipe 60"""

看起来像这样:

"Pipe 60""

此字段是将数据导入回我正在使用的清单软件中所需的关键字段.当我在记事本中删除多余的报价时,它会毫无问题地导入.问题是,有几个像这样的关键字段,通过记事本编辑每个字段会很费时间.

This field is a key field that is needed to import data back in to the Inventory Software I'm using. When I remove the extra quote in notepad, it imports without issue. The problem is, there are several key fields like this, and it would be time consuming to edit each field through notepad.

我尝试了在以前的帖子中找到的以下代码,但输出文件未显示任何数据:

I tried the code below I found on a previous post but the output file displays no data:

@echo off
setlocal enableextensions enabledelayedexpansion

(for /f "tokens=*" %%f in (a.csv) do if not "%%f"=="" (
        set "line=%%f"
        set "line=!line"""=""!"
        echo(!line!
)) > b.csv

endlocal

这是我的csv文件的示例:

Here's an example of my csv file:

000,,PipeSm,,1
000,,"Pipe 60""",,1
000,,PipeMd,,1

我不经常使用批处理文件,因此任何帮助将不胜感激.

I don't use batch files that often so any help would be much appreciated.

推荐答案

按原样尝试:

@echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%f in (a.csv) do (
        set "line=%%f"
        echo(!line:"""=""!
 )
)> b.csv

编辑 根据您的评论,尝试一下..仍然假设文件名为a.csv,还要注意,这会将更改写回到原始文件,因此请首先在虚拟文件上进行测试.

EDIT As per your comment, give this a go.. still assuming the file is called a.csv also note that this will write the changes back to the original file, so please test it on dummy files first.

@echo off
set "_infile=C:\Users\jdoe\Desktop\Removing Quotes\ImportFolder\a.csv"
for /f "tokens=2* delims=]" %%f in ('type "%_infile%" ^| find /v /n "" ^& break ^> "%_infile%"') do (
       set "line=%%f"
       setlocal enabledelayedexpansion
       set "line=!line:"""=""!"
       echo(!line!
       endlocal
)>>"%_infile%"

这篇关于需要替换“"带有“"使用批处理文件处理csv文件中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆