【发布时间】:2019-05-06 06:49:53
【问题描述】:
我有一个批处理文件,它使用 gsutil 工具将一些本地文件复制到谷歌存储区域。 gsutil 工具会生成一个不错的日志文件,其中显示了已上传文件的详细信息以及是否正常。
Source,Destination,Start,End,Md5,UploadId,Source Size,Bytes Transferred,Result,Description
file://C:\TEMP\file_1.xlsx,gs://app1/backups/file_1.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.804000Z,CPHHZfdlt6AePAPz6JO2KQ==,,18753,18753,OK,
file://C:\TEMP\file_2.xlsx,gs://app1/backups/file_2.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.813000Z,aTKCOQSPVwDycM9+NGO28Q==,,18753,18753,OK,
我想做的是
- 检查第 8 列中的状态结果(OK 或 FAIL)
- 如果状态正常,则将源文件移动到另一个文件夹(以免再次上传)。
问题是源文件名附加了“file://”,我似乎无法删除,例如
file://C:\TEMP\file_1.xlsx
需要改成这个
C:\TEMP\file_1.xlsx
我正在使用 for /f 循环,我不确定在 for /f 循环中对变量 %%A 的操作是否不同。
@echo off
rem copy the gsutil log file into a temp file and remove the header row using the 'more' command.
more +1 raw_results.log > .\upload_results.log
rem get the source file name (column 1) and the upload result (OK) from column 8
for /f "tokens=1,8 delims=," %%A in (.\upload_results.log) do (
echo The source file is %%A , the upload status was %%B
set line=%%A
set line=!line:file://:=! >> output2.txt echo !line!
echo !line!
)
输出是这样的。
The source file is file://C:\TEMP\file_1.xlsx , the upload status was OK
The source file is file://C:\TEMP\file_2.xlsx , the upload status was OK
我希望它将更改的值转储到一个新文件中,但目前它没有产生任何东西。 通常我会用这样的东西从一个特定的字符提取到字符串的末尾,但它不适用于我的 For/f 循环。
%var:~7%
非常感谢任何指针或不同的方式。
【问题讨论】:
-
您正在对变量使用延迟扩展,但您没有使用
SETLOCAL命令启用它。 -
这要么是您的语法错误,要么是我的错误。
file://:字符串不存在!你的意思是file://?那么,你的方法就好了! -
另外,
*text text text text* >> output2.txt echo !line!是什么意思。你的意思是*text text text text* >> output2.txt && echo !line!没有echo !line!在下一行?
标签: windows loops batch-file substring