Windows version of Unix touch command to modify a file's modification date from filename part

匆匆过客 提交于 2019-12-23 05:18:10

问题


I have this bash script that loops through the files in the current directory and extracts the date part from the filename, then uses (Unix) touch command to modify (or update) that file's modification-date (mtime) to this date.

Filename example

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

Bash script:

I save this bash script as _save_file_mtime_from_filename.sh (add chmod +x to it) and put in the directory where I'd like to change the file's modification time. And then run it from the command-line.

#!/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

Now I'm searching for a Windows version of this script

I've search the net (and Stackoverflow). Found some related questions like https://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command and https://superuser.com/questions/251470/windows-recursive-touch-command/251507#251507

Does anyone have any idea for a Windows version using a _save_file_mtime_from_filename.bat executable version that does essentially the same thing?

With a little tweaking and help of a (Mac) Automator action, saved as an 'application', you can even trigger this script in the Mac Finder from the right-mouse button (https://discussions.apple.com/thread/5287944?start=15&tstart=0). Sweet!


回答1:


Install Cygwin, or install binaries from GnuWin32 or UnixUtils




回答2:


I agree with @konsolebox. Just install Cygwin, and you can run your script with no changes.

However, if you don't want to go that route, you should be able to install "coreutils" from GnuWin32. Coreutils contains "touch", among other things. Writing a DOS batch file to emulate what you've written above might be a little painful, though. The part that looks tricky to me is [[ $d = ????-??-?? ]]. You'll have to do something creative to match that, perhaps using grep, which you can get from the same page. Now you're installing a couple things, and doing a lot of work, though. Installing Cygwin would be easier.



来源:https://stackoverflow.com/questions/24062726/windows-version-of-unix-touch-command-to-modify-a-files-modification-date-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!