简体   繁体   English

使用批处理文件从多个csv文件中删除特殊字符

[英]Delete special characters from multiple csv files using batch file

I want to delete all the special characters in my csv file using a batch file. 我想使用批处理文件删除我的csv文件中的所有特殊字符。 My csv file has one column of only keywords to be entered in google 我的csv文件只有一列只能在谷歌中输入关键字

For example 1.Ecommerce 2.dentist Melbourne cbd? 例如1.Ecommerce 2.dentist Melbourne cbd? 3.dentists Melbourne % 4.best dentist in Melbourne! 3.墨尔本墨尔本最好的牙医!

Sometimes I can have Aracbic/Chinese Characters as well and so on. 有时我也可以有Aracbic /中文字符等等。

Here When I add these files to GoogleAdwords-Keyword Planner, it shows me an error, on ignoring error i get wrong no. 这里当我将这些文件添加到GoogleAdwords-Keyword Planner时,它会向我显示错误,忽略错误我错了没有。 of hits for keyword and to avoid error i need to remove all the special characters from my csv file. 关键字的命中和避免错误我需要从我的csv文件中删除所有特殊字符。

I have Hundreds of csv files and want to save the updated(Without special characters) file to the existing file. 我有数百个csv文件,并希望将更新的(没有特殊字符)文件保存到现有文件。

I tried 我试过了

@echo off
set source_folder=C:\Users\Username\Documents\iMacros\Datasources\a
set target_folder=C:\Users\Username\Documents\iMacros\Datasources\keyfords-csv-file
if not exist %target_folder% mkdir %target_folder%

for /f %%A in ('dir /b %source_folder%\*.csv') do (
    for /f "skip=1 tokens=1,2* delims=," %%B in (%source_folder%\%%A) do (
    echo %%B>>%target_folder%\%%A
    )
)

timeout /t 20

But ended up Deleting all the records from csv file. 但最终删除了csv文件中的所有记录。

Is there anyway by which i can either 无论如何我也可以

1.Accept only Standard Characters which would be from AZ, az, and 0-9. 1.仅接受来自AZ,az和0-9的标准字符。

2.Or Delete all the string where I can put special characters in that string. 2.Or删除我可以在该字符串中放入特殊字符的所有字符串。 Like string1="?%!@#$^&*<>" 像string1 =“?%!@#$ ^&* <>”

3.Or is there anyway by which i can mention in csv file to accept only Standard English Characters Is there any way to achieve this using a batch file or any framework? 3.无论如何我可以在csv文件中提到只接受标准英文字符有没有办法用批处理文件或任何框架来实现这一点?

Thanks 谢谢

I think this is much cleaner in Powershell. 我认为这在Powershell中更加清晰。

$sourceFolder = "C:\Users\Username\Documents\iMacros\Datasources\a"
$targetFolder = "C:\Users\Username\Documents\iMacros\Datasources\keyfords-csv-file"
MkDir $targetFolder -ErrorAction Ignore

$fileList = Dir $sourceFolder -Filter *.csv 

ForEach($file in $fileList)
{
    $file | Get-Content | %{$_ -replace '[^\w\s,\"\.]',''} | Set-Content -Path "$targetFolder\$file"
}

I take every file from the source folder, get the contents, replace any character that is not wanted, and save it to another file. 我从源文件夹中获取每个文件,获取内容,替换任何不需要的字符,并将其保存到另一个文件中。 I use a little regex right in the middle '[^\\w\\s,\\"\\.]' with the replace command. The carrot ^ is a not match operator. So anything that does not match a word character \\w , space character \\s , a coma , , double quote \\" , or a period \\. 我在替换命令的中间'[^\\w\\s,\\"\\.]'使用了一个小正则表达式。胡萝卜^是一个不匹配的运算符。所以任何与单词字符\\w ,空格不匹配的东西字符\\s ,昏迷,双引号\\"或句号\\.

Someone may find a better regex for your needs, but I think you get the idea. 有人可能会根据您的需求找到更好的正则表达式,但我认为您可以理解。

Technically you could have a series of: 从技术上讲,你可以有一系列:

set variable=%variable:"=%
set variable=%variable:(=%
set variable=%variable:)=%
set variable=%variable:&=%
set variable=%variable:%=%

And so on. 等等。 I know this would be an annoyance to write all the special characters.. 我知道写下所有特殊字符会很烦人。

Seeing there would be less letters in the alphabet than "special characters" a findstr could be done on the file/folder name, if a letter from az is found true, write and move to the next character. 看到字母表中的字母数量少于“特殊字符”,可以对文件/文件夹名称进行查找,如果发现来自az的字母为真,则写入并移动到下一个字符。

_Arescet _Arescet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM