Windows批处理:如何从日志中获取作业ID并将其设置为变量?

时间:2021-11-24 02:07:09

I have a log file (job.log) that contains a jobID that is always changing. How do I open this file and parse the job ID and store 761 to a variable so I can use it for other command references? Again, this log will always have a different jobID so I'm looking for a dynamic way to do this.

我有一个日志文件(job.log),其中包含一直在更改的jobID。如何打开此文件并解析作业ID并将761存储到变量中,以便将其用于其他命令引用?同样,此日志将始终具有不同的jobID,因此我正在寻找一种动态方式来执行此操作。

"RUNNING","jobId":761,"logFileName":"outbox/logs/AQCONSOL-Finance_761.log

FYI, within my bat file, I will use the following command to download this log file...

仅供参考,在我的bat文件中,我将使用以下命令下载此日志文件...

call automate downloadfile "outbox/logs/AQCONSOL-Finance_"%jobid%".log"

1 个解决方案

#1


1  

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\Q43128657.txt"
FOR /f "usebackq tokens=3 delims=:," %%a IN ("%filename1%") DO SET job=%%a
ECHO job is %job%

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

您需要更改sourcedir的设置以适合您的具体情况。

I used a file named Q43128657.txt containing your data for my testing.

我使用了一个名为Q43128657.txt的文件,其中包含我的测试数据。

Simply parse the line using : and , as delimiters and select the third token.

只需使用以下方法解析该行:和,作为分隔符并选择第三个标记。


Revision to select file "rundatarule*.log"

修改选择文件“rundatarule * .log”

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN ('dir /b /a-d /od "%sourcedir%\rundatarule*.log" ') DO SET "filename1=%sourcedir%\%%a"
FOR /f "usebackqtokens=3delims=:," %%a IN ("%filename1%") DO SET job=%%a
ECHO job is %job%

GOTO :EOF

The dir command lists matching files in /b basic form /a-d with no directorynames /od in order of date (just in case there are multiple files, select the very last chronologically) and processes that list, applying the entire name (in case there are separators) to %%a and thus assigning the reconstructed name (with the directory) to filename1.

dir命令列出/ b基本格式/广告中的匹配文件,没有目录名/ od按日期顺序排列(以防有多个文件,按时间顺序选择最后一个文件)并处理该列表,应用整个名称(如果有的话)是分隔符)到%% a,从而将重建的名称(与目录)分配给filename1。

It is considered good practice to format date-timestamps as ymdhms, not dmyhms as this provides an easily-sortable sequence.

将日期时间戳格式化为ymdhms而不是dmyhms被认为是一种很好的做法,因为这提供了一个易于排序的序列。

#1


1  

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\Q43128657.txt"
FOR /f "usebackq tokens=3 delims=:," %%a IN ("%filename1%") DO SET job=%%a
ECHO job is %job%

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

您需要更改sourcedir的设置以适合您的具体情况。

I used a file named Q43128657.txt containing your data for my testing.

我使用了一个名为Q43128657.txt的文件,其中包含我的测试数据。

Simply parse the line using : and , as delimiters and select the third token.

只需使用以下方法解析该行:和,作为分隔符并选择第三个标记。


Revision to select file "rundatarule*.log"

修改选择文件“rundatarule * .log”

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN ('dir /b /a-d /od "%sourcedir%\rundatarule*.log" ') DO SET "filename1=%sourcedir%\%%a"
FOR /f "usebackqtokens=3delims=:," %%a IN ("%filename1%") DO SET job=%%a
ECHO job is %job%

GOTO :EOF

The dir command lists matching files in /b basic form /a-d with no directorynames /od in order of date (just in case there are multiple files, select the very last chronologically) and processes that list, applying the entire name (in case there are separators) to %%a and thus assigning the reconstructed name (with the directory) to filename1.

dir命令列出/ b基本格式/广告中的匹配文件,没有目录名/ od按日期顺序排列(以防有多个文件,按时间顺序选择最后一个文件)并处理该列表,应用整个名称(如果有的话)是分隔符)到%% a,从而将重建的名称(与目录)分配给filename1。

It is considered good practice to format date-timestamps as ymdhms, not dmyhms as this provides an easily-sortable sequence.

将日期时间戳格式化为ymdhms而不是dmyhms被认为是一种很好的做法,因为这提供了一个易于排序的序列。