5

通过搜索此站点并学习 Windows 批处理脚本这一荒谬的语言,我已经取得了很大进展,但我现在处于困境中。我有一个包含可变行数的文本文件,每行看起来像:

AA8315,"United States",N777AN,"American Airlines",AAL98,B772,"Boeing 777-223",AAL,"2013-06-11 23:30:47.923","2013-06-12 00:01:14.459"

我的批处理文件:

set THEDATE=2013-06-12
set THEDATABASE=c:\Kinetic\BaseStation\Basestation.sqb
set THECSVFILE=c:\Flights.csv
set THEOUTPUTFILE=c:\FlightsNew.csv
set THISLINE=""

if exist %THECSVFILE% del %THECSVFILE%
if exist %THEOUTPUTFILE% del %THEOUTPUTFILE%

:: allow time for the csv file to be deleted
timeout /t 2 /nobreak

c:\sqlite3.exe -header -csv %THEDATABASE% "select Aircraft.ModeS, Aircraft.ModeSCountry as Country, Aircraft.Registration as Reg, Aircraft.RegisteredOwners as Owner, Flights.Callsign, Aircraft.ICAOTypeCode as Type, Aircraft.Type as Model, Aircraft.OperatorFlagCode as 'Op Flag', Flights.StartTime as 'First Seen', Flights.EndTime as 'Last Seen' from Aircraft INNER JOIN Flights ON (Aircraft.AircraftID=Flights.AircraftID) where Flights.EndTime like '%THEDATE% %%' order by Flights.EndTime DESC;" >> %THECSVFILE%

::allow time for the csv to be written to file
timeout /t 5 /nobreak

::read %THECSVFILE% and loop through each line
for /F "usebackq tokens=* delims=" %%A in (%THECSVFILE%) do (
    set the_line=%%A
    call :process_line
)

:process_line
for /F "usebackq tokens=1,2,3,4,5,6,7,8,9,10 delims=[,]" %%1 in (%the_line%) do (
    set hexcode=%%1
    set country=%%2
    set reg=%%3
    set owner=%%4
    set callsign=%%5
    set planetype=%%6
    set model=%%7
    set opflag=%%8
    set firstseen=%%9
    set lastseen=%%10
    set THISLINE=%hexcode%,%country%,%reg%,%owner%,%callsign%,%planetype%,%model%,%opflag%,%firstseen%,%lastseen%
    echo %THISLINE% > %THEOUTPUTFILE%
)

(我将标记分配给变量,因为稍后我将对它们进行额外的验证和格式化。我需要先让这部分工作!)

执行时,脚本确实会遍历文件的每一行,但它似乎没有分配%%1给变量hexcode.

执行命令的输出如下所示:

C:\>for /F "usebackq tokens=1,2,3,4,5,6,7,8,9,10 delims=[,]" %1 in (AA8315 "United States" N777AN "American Airlines" AAL98 B772 "Boeing 777-223" AAL "2013-06-11 23:30:47.923" "2013-06-12 00:01:14.459") do (
set hexcode=%1
 set country=%2
 set reg=%3
 set owner=%4
 set callsign=%5
 set planetype=%6
 set model=%7
 set opflag=%8
 set firstseen=%9
 set lastseen=%10
 set THISLINE=,"United States" ,N807FD ,"Fedex Express" ,FDX1378 ,,"Airbus A310-324" ,FDX ,"2013-06-12 22:56:54.639" ,"2013-06-12 23:05:31.822"
 echo ""  1>c:\FlightsNew.csv
)
The system cannot find the file AA8315.

任何帮助是极大的赞赏!

4

3 回答 3

4

这在这里有效:

    for /f "tokens=1-10delims=," %%a in ("AA8315,"United States",N777AN,"American Airlines",AAL98,B772,"Boeing 777-223",AAL,"2013-06- 11 23:30:47.923","2013-06-12 00:01:14.459"") 做 (
     设置十六进制代码=%%a
     设置国家=%%b
     设置 reg=%%c
     设置所有者=%%d
     设置 callegn=%%e
     设置平面类型=%%f
     设置模型=%%g
     设置 opflag=%%h
     设置第一次见=%%i
     设置 lastseen=%%j
     设置这条线=%%a,%%​​b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j
    )
    >"c:\FlightsNew.csv" 回显 %THISLINE%

我不确定,为什么你需要令牌。

于 2013-07-03T16:32:49.343 回答
3

我一直在for循环中遇到逗号分隔值的问题。这是我为使您的代码正常工作所做的工作。

测试.txt

AA8315,"United States",N777AN,"American Airlines",AAL98,B772,"Boeing 777-223",AAL,"2013-06-11 23:30:47.923","2013-06-12 00:01:14.459"

批处理文件.bat

set THECSVFILE=test.txt

::read %THECSVFILE% and loop through each line
for /F "usebackq tokens=* delims=" %%A in (%THECSVFILE%) do (
    set the_line=%%A
    call :process_line
)
goto TheEnd

:process_line
for /F "usebackq tokens=1,2,3,4,5,6,7,8,9,10 delims=~" %%1 in ('%the_line:,=~%') do (
    set hexcode=%%1
    set country=%%2
    set reg=%%3
    set owner=%%4
    set callsign=%%5
    set planetype=%%6
    set model=%%7
    set opflag=%%8
    set firstseen=%%9
    set lastseen=%%10
    set THISLINE=%hexcode%,%country%,%reg%,%owner%,%callsign%,%planetype%,%model%,%opflag%,%firstseen%,%lastseen%
    echo %THISLINE% > %THEOUTPUTFILE%
)

:TheEnd

注意:process_line for循环。我不得不在周围添加单引号,%the_line%因此它不会尝试将字符串解释为文件名。然后我用 ~ 字符替换了所有逗号,并使用 ~ 字符作为分隔符。它可能不适用于您的所有数据(如果它包含单引号或 ~ 字符),但它确实适用于这一记录并让您再次朝着正确的方向前进。

于 2013-07-03T16:46:54.387 回答
2

您只能使用元变量中的字母(%%1在您的代码中) - 但小写字母和大写字母是不同的。

是的,您可以使用其他一些字符,但可用的连续块"tokens=1-10"(​​这是 1、2、3 的更简单版本...)是 a..z 和 A..Z

%0..%9 保留给批处理或批处理过程的参数。

于 2013-07-03T16:36:19.783 回答