我们使用的人力资源软件允许导入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
请注意,我不能使用任何第三方工具。
答案 0 :(得分: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