需要从文本文件中删除引号

Need to remove quotes from text file

我在下面的批处理文件中写了将文件夹中的 csv 文件复制到 txt 文件并移动到我们的导入文件夹中

copy "\jarvisfs1\groups\InforIFC\Payroll\NEW\*.csv" "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
------------------------------------------------------------------------

尝试导入 sql 或 bi 工具时,生成的文本文件似乎不再具有分隔的列。

但是当我使用 vba 脚本使用下面的命令时,列在 Sql 或 BI 工具

中是正常的
Dim objFSO, objFile, objFileTSV
Dim strLine, strNewLine
Dim FileNameLength, LineLength, NewFileName, Linepos, Quote, QuoteCount, 
TotalFilesConverted
set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
strCurPath = objFSO.GetAbsolutePathName(".")
TotalFilesConverted = 0
For Each objFile In objFSO.getfolder(strCurPath).Files

            If UCase(Right(objFile.Name, 4)) = ".CSV" Then
                            Result = WshShell.Popup("Converting " & 
  objFile.Name & "...", 3,"" , 64)
                            FileNameLength = Len(objFile.Name)-4
                            NewFileName = "EPRO.txt"
                            Set objFile = objFSO.OpenTextFile(objFile, 1)
                            Set objFileTSV = 
  objFSO.CreateTextFile(NewFileName)

                            Do Until objFile.AtEndOfStream
                                            strLine = objFile.ReadLine
                                            LineLength = Len(strLine)
                                            Linepos =1
                                            strNewLine =""
                                            Quote = False
                                            QuoteCount = 0


   Do While Linepos <= LineLength
                                                            If Mid(strLine, 
   Linepos, 1) = "," And Not Quote Then

    strNewLine = strNewLine + vbTab

   Quote = False
                                                            ElseIf 
  Mid(strLine, Linepos, 1) = Chr(34) Then

 QuoteCount = QuoteCount + 1

 If QuoteCount = 2 And Linepos <> LineLength Then

 If Mid(strLine, Linepos, 2) = Chr(34) & Chr(34) Then

 strNewLine = strNewLine + Chr(34)

  Linepos = Linepos + 1

 Quote = True

  QuoteCount = 1

  Else

  Quote = False

  QuoteCount = 0

  End If

  Else

  Quote = True

  End If
                                                           ElseIf QuoteCount 
  = 0 And Mid(strLine, Linepos, 1) = "-" Then

  strNewLine = strNewLine + Mid(strLine, Linepos, 1) + "0"
                                                            Else

  strNewLine = strNewLine + Mid(strLine, Linepos, 1)
                                                            End If
                                                            Linepos = 
   Linepos + 1
                                            Loop


                                            objFileTSV.WriteLine strNewLine
                                            strNewLine = ""
                            Loop
                            objFile.Close
                            TotalFilesConverted = TotalFilesConverted +1
                            objFileTSV.Close

            End If
    Next

    MsgBox CStr(TotalFilesConverted) + " Files Converted from CSV to TXT."

但是使用上面的命令,文本文件中的列没问题。我对第二个脚本的问题是我无法使用 Task Schu 将脚本自动获取到 运行,而作为批处理文件,我可以自动获取到 运行

How it needs to appear

How it appears after the new script is ran

我不认为该命令添加了引号和逗号。他们一开始就在那里。在记事本中打开您的 csv 并亲自查看它们。

所以你只需要一些方法来删除它们。尽管这在 cmd 中在技术上是可行的,但我不想尝试。所以这是 powershell 中的解决方案:

(Get-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW*.csv") | ForEach-Object {
    $_.Replace(",", "").Replace("`"", "")
} | Set-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"

在 Batch 中,只需删除引号和逗号并将逗号替换为 whitespace

就足够了

因此,我们可以将 CSV 文件复制到临时文件,然后在替换所有引号和逗号后将内容重新写入您的实际文本文件。

@echo off
setlocal enabledelayedexpansion

if exist "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt" (del /Q "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt")
copy "\jarvisfs1\groups\InforIFC\Payroll\NEW*.csv" "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt"
for /f "delims=" %%f in(\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt) do (
set var=%%f
set var=!var:"=!
set var=!var:,= !
echo !var! >> "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
)
del /Q "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt"

终于注意到替换函数是如何工作的了。您要替换的字符出现在 = 之前,您要替换的字符出现在 = 之后 所以如果您想将 + 替换为 - string 那么你将做 !var:+=-!%var:+=-% 如果你不 setlocal enabledelayedexpansion 确切地理解 delayedexpansion 是如何工作的,你可以 运行 setlocal /? 来自 cmd.exe