2017-05-04 86 views
0

我們使用的HR軟件允許導入CSV文件,但是我們從第三方獲得的轉儲未格式化爲CSV。用一個逗號代替逗號的多次出現

我已經寫了一個批處理文件將其處理成一個CSV文件,但是有一部分轉儲文件可能有多個空格(每行可能不同),所以當我用逗號替換空格時,結束了多個逗號。

如何更新我的批處理文件以將任何出現的兩個或多個逗號更改爲一個逗號?

原始文件:

096 Parisella, Onorato - Perm   030417 05:53 
000          030417 06:44 
127 Thomas, Vincent - Perm    030417 06:44 
040 Ram, Gurdial - Perm     030417 07:09 
100 Smano, Petros - Perm     030417 07:12 
128 Machenbach, Werner - Perm   030417 07:13 
147 Samanovic, Milan      030417 07:14 
047 Hopkins, Hugo - Perm     030417 07:16 

我目前的批處理文件:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

set "search= - Perm" 
set "replace=" 

set "textFile=FINGERTEC RAW DATA*.txt" 
set "rootDir=." 

for %%j in ("%rootDir%\%textFile%") do (
    for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
     set "line=%%i" 
     setlocal EnableDelayedExpansion 
     set "line=!line:%search%=%replace%!" 
     >>"%%~j" echo(!line! 
     endlocal 
    ) 
) 

set "search= " 
set "replace=," 

set "textFile=FINGERTEC RAW DATA*.txt" 
set "rootDir=." 

for %%j in ("%rootDir%\%textFile%") do (
    for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
     set "line=%%i" 
     setlocal EnableDelayedExpansion 
     set "line=!line:%search%=%replace%!" 
     >>"%%~j" echo(!line! 
     endlocal 
    ) 
) 

endlocal 

電流輸出:

096,Parisella,,Onorato,,,,,,,,,,,,030417,05:53 
000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,030417,06:44 
127,Thomas,,Vincent,,,,,,,,,,,,,,,030417,06:44 
040,Ram,,Gurdial,,,,,,,,,,,,,,,,,,030417,07:09 
100,Smano,,Petros,,,,,,,,,,,,,,,,,030417,07:12 
128,Machenbach,,Werner,,,,,,,,,,,,030417,07:13 
147,Samanovic,,Milan,,,,,,,,,,,,,,,,,,,,,030417,07:14 
047,Hopkins,,Hugo,,,,,,,,,,,,,,,,,030417,07:16 

請注意,我不能使用任何第三方工具。

回答

3

您尚未指定所需的輸出。

這是我會做這樣的方式:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

set "search= - Perm" 
set "replace=" 

set "textFile=test.txt" 
set "rootDir=." 

for %%j in ("%rootDir%\%textFile%") do (
    for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
     set "line=%%i" 
     set "output=" 
     setlocal EnableDelayedExpansion 
     for %%a in (!line:%search%^=%replace%!) do set "output=!output!,%%a" 
     >>"%%~j" echo(!output:~1! 
     endlocal 
    ) 
) 

輸出:

096,Parisella,Onorato,030417,05:53 
000,030417,06:44 
127,Thomas,Vincent,030417,06:44 
040,Ram,Gurdial,030417,07:09 
100,Smano,Petros,030417,07:12 
128,Machenbach,Werner,030417,07:13 
147,Samanovic,Milan,030417,07:14 
047,Hopkins,Hugo,030417,07:16 
+0

這是我後。輸出是我想要的。 – James