我在批处理文件中写下了将文件夹中的csv文件复制到txt文件并移入我们的导入文件夹
copy "\\jarvisfs1\groups\InforIFC\Payroll\NEW\*.csv" "\\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
------------------------------------------------------------------------
在尝试导入sql或bi Tool时,生成的文本文件似乎不再分隔列。
但是当我使用下面的命令使用vba脚本时,列在Sql或BI Tool
中就可以了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自动运行脚本,我可以自动运行批处理文件
答案 0 :(得分:1)
在批处理中,只需删除引号和逗号,并用空格替换逗号
因此,我们可以将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"
最后注意替换功能的工作原理。要替换的字符位于=
之前,而您要替换的字符位于=
之后,因此如果您要将+
替换为-
然后,如果你没有!var:+=-!
来了解delayedexpansion的工作原理,那么你将%var:+=-%
或setlocal enabledelayedexpansion
,你可以从 cmd.exe运行setlocal /?
答案 1 :(得分:0)
我不认为该命令会在引号和逗号中添加。他们在那里开始。在记事本中打开你的csv,亲自看看。
所以你只需要一些方法来删除它们。虽然这在cmd中技术上是可行的,但我不想尝试它。所以这是powershell的解决方案:
(Get-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW*.csv") | ForEach-Object {
$_.Replace(",", "").Replace("`"", "")
} | Set-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"