gpt4 book ai didi

Windows 批处理文件 - 去除前导字符

转载 作者:可可西里 更新时间:2023-11-01 11:15:30 24 4
gpt4 key购买 nike

我有一个批处理文件,它使用 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 循环,我不确定变量 %%A 的操作在 for/f 循环中是否不同。

@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%

非常感谢任何指示或不同的方法。

最佳答案

由于要删除的部分似乎是固定的,因此使用子字符串更容易。

还使用 for/f "skip=1" 避免了外部命令 more +1 和另一个中间文件的必要性。

@echo off & setlocal EnableDelayedExpansion
type NUL>output2.txt
for /f "skip=1 eol=| 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:~7!"
echo(!line!>>output2.txt
echo(!line!
)

关于Windows 批处理文件 - 去除前导字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com