【问题标题】:Windows version of Unix touch command to modify a file's modification date from filename partWindows 版本的 Unix touch 命令从文件名部分修改文件的修改日期
【发布时间】:2014-06-05 14:22:03
【问题描述】:

我有这个 bash 脚本,它遍历当前目录中的文件并从 文件名 中提取 日期部分,然后使用 ( Unix)touch 命令将该文件的 modification-date (mtime) 修改(或更新)到该日期。

文件名示例

Electric company name - bill - 2014-03-22.pdf

Bash 脚本:

我将这个 bash 脚本保存为 _save_file_mtime_from_filename.sh(将 chmod +x 添加到其中)并放入我想要更改文件修改时间的目录中。然后从命令行运行它。

#!/bin/bash
CURRENT_DIR=$(dirname $_)
cd $CURRENT_DIR

for f in *
do
    # Strip the file extension
    d=${f%.*}

    # Strip the last 10 characters
    d=${d:${#d}-10}

    # Check the format / mask
    if [[ $d = ????-??-?? ]] ; then
        # Strip the dash
        d=${d//-}

        # Run `touch` on the file with the extracted date format
        # and add `0000` to the file date
        touch -t ${d}0000 "$f"
    fi
done

现在我正在寻找 此脚本的 Windows 版本

我已经搜索了网络(和 Stackoverflow)。发现了一些相关的问题,例如https://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-commandhttps://superuser.com/questions/251470/windows-recursive-touch-command/251507#251507

有没有人知道Windows 版本 使用_save_file_mtime_from_filename.bat 可执行版本基本上做同样的事情?

在 (Mac) Automator 操作的帮助下,保存为“应用程序”,您甚至可以通过鼠标右键 (https://discussions.apple.com/thread/5287944?start=15&tstart=0) 在 Mac Finder 中触发此脚本。甜蜜!

【问题讨论】:

  • 为什么不直接安装 cygwin?

标签: windows bash shell batch-file timestamp


【解决方案1】:

安装Cygwin,或从GnuWin32UnixUtils 安装二进制文件

【讨论】:

    【解决方案2】:

    我同意@konsolebox。只需安装Cygwin,您就可以运行您的脚本而无需更改。

    但是,如果您不想走这条路,您应该可以从GnuWin32 安装“coreutils”。 Coreutils 包含“触摸”等内容。不过,编写一个 DOS 批处理文件来模拟您在上面编写的内容可能有点痛苦。对我来说看起来很棘手的部分是[[ $d = ????-??-?? ]]。你必须做一些有创意的事情来匹配它,也许使用 grep,你可以从同一页面获得。不过,现在您正在安装一些东西,并且做了很多工作。安装 Cygwin 会更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多