我的文件是制表符分隔文件(TSV),有 5 列
我的第一列上有本地驱动器路径,但路径中的每个图像名称都包含双引号。
路径
C:\Users\magoo\“flower.jpg”
C:\Users\magoo\“flower2.jpg”
我试过代码但无法删除双引号。
@ECHO Off
SETLOCAL
(
FOR /f "usebackqtokens=1* delims=" %%a IN (input.csv) DO ECHO %%~a,%%b
)>output.csv
GOTO :EOF
答案1
如果您可以访问基于 Linux 的系统,我建议您使用 sed :
sed -i 's/"//g' filename
如果没有的话,这篇文章似乎指向了正确的方向,但我自己并不是批处理专家:https://www.robvanderwoude.com/bht.php#Strip
PowerShell 替代方案:
(Get-Content C:\Tess.txt) | ForEach-Object { $_ -replace '"' } > C:\Tess.txt
答案2
您可以尝试使用此代码进行测试:
@echo off
Title Replace Double Quotes into a Variable
Setlocal EnableDelayedExpansion
set Var1=C:\Users\magoo\"flower.jpg"
echo Variable Var1 with quotes !Var1!
pause
:::::::::::::::::::::::::::::::::::::::::
Call :Dequote !Var1! NewVar1
echo New Variable NewVar1 without quotes !NewVar1!
pause
:::::::::::::::::::::::::::::::::::::::::
set Var2=C:\Users\magoo\"flower2.jpg"
echo Variable Var2 with quotes !Var2!
pause
:::::::::::::::::::::::::::::::::::::::::
Call :Dequote !Var2! NewVar2
echo New Variable NewVar2 without quotes !NewVar2!
:::::::::::::::::::::::::::::::::::::::::
pause
Exit
::::::::::::Function DeQuote:::::::::::::
:Dequote <Var> <NewVar to Set>
set MyString=%1
set StrToFind="
set NewStr=
set %2=!MyString:%StrToFind%=%NewStr%!
Exit /b
:::::::::::::::::::::::::::::::::::::::::
编辑 : 对于你的情况,你可以尝试类似的方法:
@echo off
Title Replace Double Quotes into a Variable
Setlocal EnableDelayedExpansion
(
FOR /f "delims=, tokens=1-5" %%a IN ('Type input.csv') DO (
Set "MyVar1=%%a"
Rem We dequote the variable MyVar1 and we set it to a new on NewVar1
Call :Dequote !MyVar1! NewVar1
echo !NewVar1!,%%b,%%c,%%d,%%e
)
)>output.csv
Pause & Exit
::::::::::::Function DeQuote:::::::::::::
:Dequote <OldVar> <NewVar to Set>
set MyString=%1
set StrToFind="
set NewStr=
set %2=!MyString:%StrToFind%=%NewStr%!
Exit /b
:::::::::::::::::::::::::::::::::::::::::