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

zoomzoomvince

我有一个批处理文件,可以使用gsutil工具将一些本地文件复制到google存储区。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,则将源文件移动到另一个文件夹(这样就不会再次上传它)。

问题是源文件名后面附加了“ file://”,我似乎无法删除它,例如

file://C:\TEMP\file_1.xlsx

需要更改为此

C:\TEMP\file_1.xlsx

我正在使用for / f循环,并且不确定在/ / 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%

非常感谢任何指针或其他方式。

用户名

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

还使用for /f "skip=1"逃避命令,需要更多外部命令+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!
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Windows批处理文件删除重复的文件

批处理文件删除字符

Windows批处理文件循环中的字符串

从Windows批处理文件返回字符串

在Windows批处理文件中解析并传递特殊字符

如何使用Windows批处理文件删除不包含某些字符的文件?

使用 Windows 批处理文件删除/替换文本文件字符串

如何使批处理文件自行删除?

批处理文件中“£”的转义字符

Windows批处理文件删除X天之前的文件,但保留至少X个文件

cmd批处理文件删除字符失败

从cmd批处理文件中的“类型”中删除字符

Windows批处理文件中文件名中包含非ASCII字符的文件

从Windows的批处理文件中删除特定文件夹(如果为空)?

Windows批处理文件以删除音频文件(具有长度要求)

如何使用批处理文件在 Windows 10 中删除具有特定名称的文件?

如何使用批处理文件替换Windows文件名中的单个字符?

将字符串回显到多行.txt文件-Windows批处理文件

Windows批处理文件可在html文件中查找变量字符串

仅保留 txt 文件中包含某个单词/字符的行(Powershell)(Windows 批处理文件)

在Windows批处理文件的文件名中交换字符

Bash脚本:重命名批处理文件以删除/更改Windows文件系统下所有非法字符

批处理文件文件输出,字符串处理

如何在Windows 10中的批处理文件中从%〜dp0末尾删除空间

如何在Windows 7上通过批处理文件删除目录树?

Windows批处理文件插入空白行/删除单行-nmap输出

如何从Windows终端(批处理文件)中删除USB设备?

Windows批处理文件:将结构转换为单行字符串

Windows批处理文件-拆分字符串以设置变量