1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Save and restore folder timestamps

Discussion in 'Support' started by Inds, Dec 29, 2013.

  1. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Rod and Charles in the last thread produced this script which worked flawlessly with saving and restoring timestamps of files but not folders.

    Code:
    @echo off
    
    for /r %%a in (*) do (
      echo %%a
      set _i=%@iniwrite[c:\fileinfo.ini,,%%a,%@filedate["%%a",c,s],%@filetime["%%a",c,s],%@filedate["%%a",w,s],%@filetime["%%a",w,s]]
    )
    Code:
    @echo off
    
    for /r %%a in (*) do (
      set _s=%@iniread[c:\fileinfo.ini,,%%a]
     
      iff "%_s" ne "" .and. exist "%%a" then
        echo %%a
        touch /q /dc%@word[",",0,%_s] /tc%@word[",",1,%_s] "%%a"
        touch /q /dw%@word[",",2,%_s] /tw%@word[",",3,%_s] "%%a"
      endiff
    )
    Is there something I can modify in the script to include folders too?

    Thanks.
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    In looking at HELP -> FOR, I find that the default attribute is /A:-D-H-S of items to be processed by FOR. Adding the attribute selection code /A: immediately after the FOR keyword should do the trick.

    You could simplify the data collection using Charles Dye's ISO8601.dll - just use
    PDIR /a: /s /(fpnq=%@filestamp[*,c,4,] %@filestamp[*,w]) * >! c:\fileinfo.ini
    Note: this overwrites any prior file info file. Cf. the one in the OP, which never deletes any files that no longer exist; while it does not harm, it can cause clutter, and in the unlikely case that a file which existed on a previous data collection, was deleted, but was created anew after the latest data collection, it will change the brand new files timstamps to those of one which had been deleted. Naturally, deleteing the info file between uses removes both of these issues. Regardless, a single redirected PDIR is likely to be much faster than a FOR loop which needs to perform insertion sort for each file.

    Another point. Unless you use @FILESTAMP, I would use UTC times rather than local times, because there will come a time when you collect the data during daylight saving time, and use it during standard time, or vice versa, and NTFS' screwy translation between the internal UTC and the external local time will get things mixed up. You don't need to work during the actual switchover, it is enough if you don't do both steps while UTC timeshift is the same.

    A minor point. In the restoration loop the file names are found by the FOR, so they must exist - there is no need to test for existence.
     
  3. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    Which is exactly what I had posted....
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    ... and the OP must have overlooked. Sorry, I did not go back to the original thread, I replied to the first post in this thread. For me, threads should be stand-alone. Switching threads should only be done if the thrust of the thread changed.
     
  5. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    I posted a new thread because I responded to Charles G's new script and the progress and nobody was answering.

    Code:
    @echo off
    
    for /r %%a in (*) do (
      set _s=%@iniread[c:\fileinfo.ini,,%%a,%%f_Attrs]
    
      iff "%_s" ne "" .and. exist "%%a" then
        echo %%a
        touch /q /dc%@word[",",0,%_s] /tc%@word[",",1,%_s] "%%a"
        touch /q /dw%@word[",",2,%_s] /tw%@word[",",3,%_s] "%%a"
      endiff
    )
    Charles, is this not the revised restore script? It fails to restore timestamps of the folders. The info in the INI file does indeed contain the mod and create timestamps as well as attributes (I don't need attributes preserved but no matter) so I presume that the save script works fine but the restore fails.
     
  6. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    Code:
    @echo off
    
    for /a: /r %%a in (*) do (
      set _s=%@iniread[c:\fileinfo.ini,,%%a,%%f_Attrs]
    
      IIF %@index[%f_attrs,D,0] gt 0 then
        rem update date/time stamps of directory
        iff "%_s" ne "" .and. isdir "%%a" then
          echo %%a
          touch /a:d /q /dc%@word[",",0,%_s] /tc%@word[",",1,%_s] "%%a"
          touch /a:d /q /dw%@word[",",2,%_s] /tw%@word[",",3,%_s] "%%a"
        endiff
      else
        iff "%_s" ne "" .and. exist "%%a" then
          echo %%a
          touch /a:-d /q /dc%@word[",",0,%_s] /tc%@word[",",1,%_s] "%%a"
          touch /a:-d /q /dw%@word[",",2,%_s] /tw%@word[",",3,%_s] "%%a"
        endiff
      endiff
    )
    
    should work - not tested though.
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    The above is the script posted by Charles in the original thread. It fails to restore folder timestamps for the same reason the /a: is needed to save folder time stamps - without /a: FOR will find only files, even if folder times are saved.

    A much faster method would use a plain text file to save the information, and read the file back for restoration, as shown below. It depends on Charles Dye's plug-in ISO8601.

    savetime.btm

    Code:
    *pdir /a: /s /t:wu /(@filestamp[*,cu,4] dy-m-d th:m:s fpnq) >! c:\fileinfo.lst
    resetime.btm

    Code:
    do line in @c:\fileinfo.lst
      set f=%@word[3,%line]
      if not exist %f iterate
      echo %f
      touch /q /dc%@instr[0,10,%line]u /tc%@instr[12,8,%line] %f
      touch /q /d%@word[1,%line]u /t%@word[2,%line]u %f
    enddo
    
    Notes:
    1/ CAVEAT!!! Not tested! In particular, the UTC option of the TOUCH command - suffixing the /T and /D fields with u instead of putting all the option characters in front of the value conforms to the literal description in HELP, but does not feel correct. Also, not verified that the date and time are retrieved from the correct positions of the report line, as created by @FILESTAMP.
    2/ to run either batch file, you must switch your current directory to be the top level of the tree to be saved
    3/ using UTC to ensure change of state of DST between saving and restoring has no effect
    4/ using @FILESTAMP is not necessary, one could use @filedate and @filetime instead, with appropriate options, and adjusting where the date and time information is to be retrieved from in the report line.
    5/ The fpnq field created by PDIR includes quotation marks if needed, so it can be used as is in resetime.
     
  8. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Charles,
    Code:
    TCC LE  13.06.77   Windows XP [Version 5.1.2600]
    Copyright 2013 JP Software Inc.  All Rights Reserved
    
    [C:\Program Files\JPSoft\TCCLE13]C:\test
    
    [C:\test]"C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "IIF"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "else"
    TCC: C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat [20]  Unknown command "endiff"
    [C:\test]

    That's what I got when trying your revised script.

    Steve, the INI file is a plain text file is it not?
    Your save script gave an incorrect function error
    Code:
    [C:\test]"C:\Documents and Settings\Admin\Desktop\timestamp save1.bat"
    pdir /a: /s /t:wu /(@filestamp[*,cu,4] dy-m-d th:m:s fpnq) >! c:\fileinfo.lst
    TCC: (Sys) C:\Documents and Settings\Admin\Desktop\timestamp save1.bat [1]  Incorrect function.
     "%@filestamp[C:\test\a,cu,4]"
    
    [C:\test]"C:\Documents and Settings\Admin\Desktop\timestamp restore1.bat"
    do line in @c:\fileinfo.lst
     
  9. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Charles' code had a mistype: IIF should be IFF. This cascaded into a lot of error messages.

    That's because - as I mentioned - @FILESTAMP is a plug-in function, located in Charles Dye's ISO8601.dll. You can download it from
    prospero.unm.edu/dll/iso8601.zip - includes full help.
     
  10. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Sorry, one l too many - prospero.unm.edu/dl/iso8601.zip
     
  11. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Steve, I don't see any /L command in your script to load the DLL though even after adding the load command it returned the same incorrect function error.
     
  12. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    I intentionally did not specify how you load the plugin. On my systems all plug-ins are automatically loaded when TCMD/TCC starts by being cataloged in the PLUGINS subdirectories of each TCMD installation directory, typically using junctions to share a single copy for multiple versions. This allows me to consider them as if they were part of TCMD, and not worry to make sure to load them only when I need them. I use @FILESTAMP in dozens of aliases which use the PDIR command; never had the "illegal function" problem. When the plugin is loaded, try the command iso8601help - if the load was successful, the command will work.

    BTW, it is not a good idea to use a batch file name with an embedded space - you must always quote it. I never do that, and put my batch files either in a directory on the path, or in the one where I work, so I don't need to enter a path. I type fast, but very inaccurate - short names save a lot of time!
     
  13. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Good thing you reminded me not to use spaces in the filename because this did seem to hinder things for the first script. The restore script still fails though. Strangely it says "plugin already loaded" when it's only in the script once.

    Code:
    TCC LE  13.06.77   Windows XP [Version 5.1.2600]
    Copyright 2013 JP Software Inc.  All Rights Reserved
    
    [C:\Program Files\JPSoft\TCCLE13]C:\test
    
    [C:\test]"C:\Documents and Settings\Admin\Desktop\savetimestamp1.bat"
    plugin /l "C:\Program Files\JPSoft\TCCLE13\iso8601.dll"
    ISO8601 plugin v1.3.10 loaded.
    pdir /a: /s /t:wu /(@filestamp[*,cu,4] dy-m-d th:m:s fpnq) >! c:\fileinfo.lst
    
    [C:\test]"C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat"
    plugin /l "C:\Program Files\JPSoft\TCCLE13\iso8601.dll"
    TCC: C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat [1]  Plugin already loaded "C:\Program Files\JPSoft\TCCLE13\iso8601.dll"
    do line in @c:\fileinfo.lst
    set f=C:\test\a
    if not exist %f iterate
    echo C:\test\a
    C:\test\a
    touch /q /dc2013-12-22u /tc9:22:22Z C:\test\a
    TCC: (Sys) C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat [7]  The system cannot find the file specified.
     "C:\test\a"
    touch /q /d2013-12-22u /t09:22:22u C:\test\a
    TCC: (Sys) C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat [8]  The system cannot find the file specified.
     "C:\test\a"
    set f=C:\test\a\b
    if not exist %f iterate
    echo C:\test\a\b
    C:\test\a\b
    touch /q /dc2013-12-22u /tc9:22:22Z C:\test\a\b
    TCC: (Sys) C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat [7]  The system cannot find the file specified.
     "C:\test\a\b"
    touch /q /d2013-12-22u /t09:22:22u C:\test\a\b
    TCC: (Sys) C:\Documents and Settings\Admin\Desktop\restoretimestamp1.bat [8]  The system cannot find the file specified.
     "C:\test\a\b"
    
    [C:\test]
     
  14. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    BUMP. What am I doing wrong?
     
  15. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    564
    Likes Received:
    8
    A plugin stays loaded unless you unload it. You are loading it twice; once in savetimestamp1.bat and again in restoretimestamp1.bat.
     
  16. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Thanks for that but it fails even if I only load the second script. Why can it not find the folders when they are there?
     
  17. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    You displayed the result, but not the code of the restoration program. Please show it!
     
  18. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Hmm, could've sworn I posted it. Here it is:
    Code:
    do line in @c:\fileinfo.lst
      set f=%@word[3,%line]
      if not exist %f iterate
      echo %f
      touch /q /dc%@instr[0,10,%line]u /tc%@instr[12,8,%line] %f
      touch /q /d%@word[1,%line]u /t%@word[2,%line]u %f
    enddo
    
     
  19. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    I think part of the problem is that the OP is not omitting the dot directories. They this to set FileInfo.ini

    @echo off
    rem Added
    rem /a: to inclde all non hidden and non-system file/folders
    rem /h to not include the . and .. folders
    rem added %@attrib to end of iniwrite
    for /a: /h /r %a in (*) do (
    echo %@attrib[%a] %a
    set _i=%@iniwrite[c:\fileinfo.ini,,%a,%@filedate["%a",c,s],%@filetime["%a",c,s],%@filedate["%a",w,s],%@filetime["%a",w,s],%@attrib[%a]]
    )
    rem end of batch to save
     
  20. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Is that script only for saving or restoring also? I can't tell.
     
  21. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    No, the PDIR command, by default, does not list the . and .. directories. Only the real ones.

    A minor error - unless its transcription - the first touch for the /t option does not include the trailing u.
    A more important error - all TOUCH commands should include the /A: option, else they will not TOUCH directories.

    Ah! My mistake! Even though folders are touched, when touching any file in a directory, the directory will be changed to the current date and time. This cannot be prevented, it is the way NTFS and FAT works (and also Unix, BSD and Linux file systems). You need an elaborate method to prevent it. After you handle files, you must touch the deepest directory first, and work your way up to the higher levels. First you must separate files from folders, which is easy (create two files, one for files and one for folders), and process all files. Now comes the tricky part. One method is to sort the folders in reverse order of depth - e.g., first all folders five level deep, next all folders four level deep, etc., and touch (restore) them in that order. I challenge the readers to create a simple method! It is too involved for my taste...
     
  22. Inds

    Joined:
    Dec 21, 2013
    Messages:
    17
    Likes Received:
    0
    Ah damn, way to kill my optimism. exFAT doesn't update a folder's timestamp but I don't use that FS for non-OS disks. WinRAR said a couple times they would fix this issue with their archiving program but they never did.
     
  23. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    post 19 is for saving
     

Share This Page