Incrementing folder modification time based on an ordered folder list

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchrun
Posts: 2
Joined: 08 Aug 2017 09:01

Incrementing folder modification time based on an ordered folder list

#1 Post by batchrun » 08 Aug 2017 09:30

Hi all

I'm looking to read set and increment by one second the File Modification Time of a series of folders (and all their contents) inside a parent folder. The first four letters of each folder name in the subdirectory I want to work in are a numeric sequence, which should form the basis of the sequence and incrementing.

An example:

.\albums\0001 first album name
.\albums\0002 second album name
.\albums\0003 third album name
...

I'm looking to write a batch script that will do the following:
- obtain a list of parent directories within the folder it is run from
- obtain the first four characters and store these as a substring
- sort the substrings in ascending order
- modify the File Modification Time of each parent directory and all its contents using a single seed date and timestamp, incremented by one second for every change in parent folder

I'd be happy to hard code the seed date and timestamp in the file.
So in the example folder list above if the seed date and timestamp were today at 13:00
.\albums\0001 first album name date and timestamp would be today at 13:00:00
.\albums\0002 second album name date and timestamp would be today at 13:00:01
.\albums\0003 third album name date and timestamp would be today at 13:00:02

I've not used a Windows/DOS batch file in donkey's years. Can it be done?

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: Incrementing folder modification time based on an ordered folder list

#2 Post by barnabe0057 » 08 Aug 2017 15:09

Hi,

No problem to modify a file, but it's a bit more complicated regarding a folder.

I found a third-party tool, I don't know if it can do the trick :
http://www.nirsoft.net/utils/folder_time_update.html

Otherwise I didn't quite understand if you want to keep the first 4 digits of the original folder name.

batchrun
Posts: 2
Joined: 08 Aug 2017 09:01

Re: Incrementing folder modification time based on an ordered folder list

#3 Post by batchrun » 08 Aug 2017 16:20

barnabe0057 wrote:Hi,

No problem to modify a file, but it's a bit more complicated regarding a folder.

I found a third-party tool, I don't know if it can do the trick :
http://www.nirsoft.net/utils/folder_time_update.html

Otherwise I didn't quite understand if you want to keep the first 4 digits of the original folder name.


Thanks for responding. The folder names should be left unchanged, only their last modification date should change. I'm running Linux here so I can't show an actual Windows based directory listing. Best I can do is try to explain. Assume 3 directories 001, 002 & 003. For a number of reasons their last modified date and timestamps are not sequential.

001 last modified Aug 9 03:03:03
003 last modified Aug 1 12:09:00
002 last modified Aug 7 14:21:56

I want to change the last modified date and timestamp so they're sequential, much like 001, 002 & 003 are sequential. Assuming I use a seed time of August 1 13:00:00 the end result should be:

001 last modified Aug 1 13:00:00
002 last modified Aug 1 13:00:01
003 last modified Aug 1 13:00:02

Having done this, other programs that can sort content on last modified date can present the contents in the same sequence that was manually derived using the first 3 letters of each folder name.

So, in essence, sort folder names in ascending order, make a call to touch to change the last modified timestamp for each folder and its contents, starting with the seed of August 1 13:00:00. Each change in parent folder, ie. from 001 to 002 increments the timestamp by adding a second to the seed. In pseudo code terms:

Code: Select all

obtain list of parent folders in current directory tree
sort list ascending order
seed_time = August 1 13:00:00
for i = 1 to number of parent folders

     set last modified timestamp of folder[i] and all its contents = seed_time
     seed_time = seed_time + 1 second
     i = i +1


I had a look at the utility you suggested, but it cannot increment the timestamp.

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: Incrementing folder modification time based on an ordered folder list

#4 Post by mcnd » 09 Aug 2017 04:04

For the folder part, isn't it enough to iterate over the list of folders, in the required order, creating and deleting a temporary file inside each one? This "should" change the folder last modified time.
The files can be updated with a simple `copy` command

Code: Select all

@echo off
    setlocal enableextensions disabledelayedexpansion
   
    for %%a in ("%~f1.") do set "target=%%~fa"
    if not exist "%target%\" (
        echo Folder does not exist
        goto :eof
    )
   
    set "tempFile=~%random%%random%%random%%random%%random%%random%.tmp"
    for /d %%a in ("%target%\*") do pushd "%%~fa" && (
        echo Updating "%%~fa"
        >nul (
            for %%b in ("*") do copy /b "%%~nxb"+,,
            >"%tempFile%" type nul & del /q "%tempFile%"
            ping -n 2 ""
        )
        popd
    )

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: Incrementing folder modification time based on an ordered folder list

#5 Post by barnabe0057 » 09 Aug 2017 06:41

I had the same idea, but if you delete the temporary file, I think the folder timestamp will be re-modified.

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: Incrementing folder modification time based on an ordered folder list

#6 Post by mcnd » 10 Aug 2017 01:09

barnabe0057 wrote:I had the same idea, but if you delete the temporary file, I think the folder timestamp will be re-modified.


You are right, it is changed to current time when the file is created and changed again to current time when the file is deleted. While the proposed code does not follow the 1 second increment in the request, it could comply with

batchrun wrote:Having done this, other programs that can sort content on last modified date can present the contents in the same sequence that was manually derived using the first 3 letters of each folder name.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Incrementing folder modification time based on an ordered folder list

#7 Post by aGerman » 10 Aug 2017 10:03

Time stamps can be changed using PowerShell (where I'm not familiar with). Also there are 3rd party tools. Search for tools named "touch". If you don't find some I could provide my own utility.

Steffen

Post Reply