批量编程帮助 - 无法按预期工作

时间:2013-07-05 14:03:45

标签: csv batch-file

我在目录“C:\ Paypal_EMT \ data”中有许多CSV文件,我需要根据第一列处理和提取某些数据列。

但是我收到以下错误“系统找不到文件C:\ Paypal_EMT \ data \ STL-20130614.01.005.CSV”

C:\Paypal_EMT\data\STL-20130614.01.005.CSV
C:\Paypal_EMT\data\STL-20130615.01.005.CSV
C:\Paypal_EMT\data\STL-20130616.01.005.CSV
....

And data file looks as follows:
"RH",2013/06/15 02:14:58 -0400,"X","LQ3SUEEWPWKL6",005,
"FH",01
"SH",2013/06/14 00:00:00 -0400,2013/06/14 23:59:59 -0400,"LQ3SUEEWPWKL6",""
"CH","TransactionID","InvoiceID","PayPalReferenceID","PayPalReferenceIDType","TransactionEventCode","TransactionInitiationDate","TransactionCompletionDate","TransactionDebitOrCredit","GrossTransactionAmount","GrossTransactionCurrency","FeeDebitOrCredit","FeeAmount","FeeCurrency","CustomField","ConsumerID","PaymentTrackingID"
....

Here's my code so far:

@echo off
setlocal EnableDelayedExpansion

:: Set path directory where csv files reside in variable
Set _InputPath=C:\Paypal_EMT\data

FOR %%I IN (!_InputPath!\*.CSV) DO ( 

   Set "_OutFile=%%I.sum.rpt"

   FOR /F "tokens=1-18* delims=," %%A IN (!I!) DO (
     if "%%~A"=="RH" echo %%~B
     if "%%~A"=="FH" echo %%~B
     if "%%~A"=="SH" echo %%~B
     if "%%~A"=="CH" echo %%~B,%%~Q,%%~R
     if "%%~A"=="RF" echo %%~B,%%~Q,%%~R
    )

) > %_OutFile%

1 个答案:

答案 0 :(得分:2)

试试这个:

@echo off
setlocal EnableDelayedExpansion

:: Set path directory where csv files reside in variable
Set "_InputPath=C:\Paypal_EMT\data"

FOR %%x IN ("%_InputPath%\*.CSV") DO ( 

   Set "_OutFile=%%~x.sum.rpt"

   (FOR /F "usebackq tokens=1-18* delims=," %%A IN ("%%~x") DO (
     if "%%~A"=="RH" echo %%~B
     if "%%~A"=="FH" echo %%~B
     if "%%~A"=="SH" echo %%~B
     if "%%~A"=="CH" echo %%~B,%%~Q,%%~R
     if "%%~A"=="RF" echo %%~B,%%~Q,%%~R
    ))>"!_OutFile!"
)