Deleting files without touching a directories date

Off-topic posts of interest to the "Everything" community.
Post Reply
therube
Posts: 4972
Joined: Thu Sep 03, 2009 6:48 pm

Deleting files without touching a directories date

Post by therube »

Deleting files without touching a directories date?
Conceptually, how do you go about this?


I've got some files that I want to delete from various top-level directories - without changing the directory time stamp.

So:

X/file1
X/file2
X/file3
Y/file1
Y/file2
Y/file3
Z/file1
Z/file2
Z/file3

& I want to delete all the 'file1' from there - retaining the (existing) directory times.
Now I could use something like, FolderTimeUpdate, but depending on the directory layout, it can be quirky in actual operation.

So then I thought, if there were a utility that would delete without touching, but then thought, except for malware writers, probably not.

Something like: rm -r -no-touch file1

I know copy programs can copy with a directory structure, retaining dates.
And I could exclude from the copy unwanted files - like 'file1' - while also retaining copied file AND directory dates.
(FastCopy can do that. Robocopy too, I'm guessing, but didn't look?)
But that the actual copying of files, is not efficient.

Then I thought if I de-dup'd the file1's, by sym-linking them, maybe ? that would not touch the directory dates?
And then if I deleted all those links, except for 1, that maybe ? that too would not touch the directory dates?
(I did not test & still awkward.)

So now, I'm thinking about copying the directory structure (no files) - which will be fast.
Deleting said files ('file1') from the source directories.
Then using the the copied directory date/time (which are* maintained with the copy) as basis to then touch (re-date) the original source directory dates.

So something like this to copy the source directory structure:

robocopy <source> <dest> /e /xf *
robocopy C:\TMP\BRU\BRC .\out2 /e /xf *

And then something like this to re-date the source directory dates:

set source=C:\TMP\BRU\BRC
set copy=C:\out\222\out2

for /D %%i in (*) do touch.exe "%source%\%%i" -r %%i

The -r uses %%i (the copied directory date) as its' time to re-date the source directories.

?


which are* maintained with the copy
Are*, may depend on the particular copy utility & "quirks" it may have.
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: Deleting files without touching a directories date

Post by NotNull »

Date changes of folders are on a filesystem level (IIRC), so not much you can do about that, other than "backup" the current state, delete your files and "restore" afterwards.

If I had to do this (I won't as folder dates are too volatile):
- In Everything, select the foldernames
- Menu => Edit => Advanced => Set Time
- Set Date Created to Date Modified
- Delete your files
- Menu => Edit => Advanced => Set Time
- Set Date Modified to Date Created

(you will lose the original Date Created though)
therube
Posts: 4972
Joined: Thu Sep 03, 2009 6:48 pm

Re: Deleting files without touching a directories date

Post by therube »

Not particularly concerned about Created.
Though appears robocopy does retain that too.
void
Developer
Posts: 16734
Joined: Fri Oct 16, 2009 11:31 pm

Re: Deleting files without touching a directories date

Post by void »

"backup" the current state, delete your files and "restore" afterwards.
This is what voidhash does.

It makes a backup of your timestamps and restores them after making any changes.
therube
Posts: 4972
Joined: Thu Sep 03, 2009 6:48 pm

Re: Deleting files without touching a directories date

Post by therube »

RE-DATE.bat, copies, waits for you to make changes, then re-dates in one "go".

Code: Select all

:: RE-DATE.bat - copies a directory tree /structure/ (no files)
::               allowing one to make changes to the source directories
::               then "re-dates" the source directory date/time back to what they were before they were changed, i.e., directory date/time is retained - even after changes
:: SjB 04-06-2024

:: NOTE: this only RE-DATE's the TOPMOST directories in a directory tree - i.e., no subdirectories of a particular directory in the tree
::       so if SOURCE tree is C:/DEV/LOCATE/, C:/DEV/LOCATE/15.1371/, but NOT, C:/DEV/LOCATE/15.1371/subdirectory/

:: as written, it is expected that RE-DATE.bat is run from within the COPY directory
:: but if i use %copy%\%%i ...

:: touch re-touches all directories, whether they've been touched or not
:: would be more efficient if it only touch'd touch'd directories

:: i accept no liability for quotes " "  "_"  ;-)



@echo OFF
SET SOURCE=C:\DEV\Locate
SET   COPY=C:\OUT\LOCATE


:: robocopy  <source>  <dest> /e /xf *

:: copy source to destination, including subdirectories, even empty ones, & exclude EVERYTHING (all files) [so only directory /structure/ is copied - fast]
:: this also retains the <source> directories date/time

:: echo   %SOURCE%
:: echo   %COPY%


ROBOCOPY  %SOURCE%  %COPY%  /E /XF *

ECHO.
ECHO Directory date/time have been recorded.
ECHO Now, go ahead and make any changes to the source directories that you want :-).
pause


ECHO.
ECHO About to RE-DATE.
ECHO IF you press any key, RE-DATE will proceed - else press Ctrl+C to break out.
pause



:RE-DATE-ONLY

:: RE-DATE
:: for /d %%i in (*)       do touch  "%source%\%%i"  -r "%%i"
:: for /d %%i in (15.1371) do touch  "%source%\%%i"  -r "%copy%\%%i"  --no-create
:: --no-create will NOT re-create a directory deleted on the source end


for /d %%i in (*)  do  touch "%source%\%%i"  -r "%copy%\%%i"  --no-create



ECHO.
ECHO RE-DATE'd and Done.
PAUSE
RE-DATE - BU-DIRS.bat, backs up the directory dates, only:

Code: Select all

:: RE-DATE.bat - copies a directory tree /structure/ (no files)
::               allowing one to make changes to the source directories
::               then "re-dates" the source directory date/time back to what they were before they were changed, i.e., directory date/time is retained - even after changes
:: SjB 04-06-2024

:: NOTE: this only RE-DATE the TOPMOST directories in a directory tree - i.e., no subdirectories of a particular directory in the tree
::       so, C:/DEV/LOCATE/15.1371/, but NOT, C:/DEV/LOCATE/15.1371/subdirectory/

:: as written, it is expected that RE-DATE.bat is run from within the COPY directory
:: but if i use %copy%\%%i ...

:: i accept no liability for quotes " "  "_"  ;-)



@echo OFF
SET SOURCE=C:\DEV\Locate
SET   COPY=C:\OUT\RE-DATE-DIRS\LOCATE


:: robocopy  <source>  <dest> /e /xf *

:: copy source to destination, including subdirectories, even empty ones, & exclude EVERYTHING (all files) [so only directory /structure/ is copied - fast]
:: this also retains the <source> directories date/time

:: echo   %SOURCE%
:: echo   %COPY%


ROBOCOPY  %SOURCE%  %COPY%  /E /XF *

ECHO.
ECHO Directory date/time have been recorded.
ECHO Done.
pause
exit



ECHO Now, go ahead and make any changes to the source directories that you want :-).
pause


ECHO.
ECHO About to RE-DATE.
ECHO IF you press any key, RE-DATE will proceed - else press Ctrl+C to break out.
pause



:RE-DATE-ONLY

:: RE-DATE
:: for /d %%i in (*)       do touch  "%source%\%%i"  -r "%%i"
:: for /d %%i in (15.1371) do touch  "%source%\%%i"  -r "%copy%\%%i"  --no-create
:: --no-create will NOT re-create a directory deleted on the source end


for /d %%i in (*)  do  touch "%source%\%%i"  -r "%copy%\%%i"  --no-create



ECHO.
ECHO RE-DATE'd and Done.
PAUSE
RE-DATE - ONLY.bat, re-date's, only (after presumably first having backed up the directory dates), allowing you to make any number of changes to the source, over time, then resetting the directory dates to some previous date.

Code: Select all

:: RE-DATE.bat - copies a directory tree /structure/ (no files)
::               allowing one to make changes to the source directories
::               then "re-dates" the source directory date/time back to what they were before they were changed, i.e., directory date/time is retained - even after changes
:: SjB 04-06-2024

:: NOTE: this only RE-DATE the TOPMOST directories in a directory tree - i.e., no subdirectories of a particular directory in the tree
::       so, C:/DEV/LOCATE/15.1371/, but NOT, C:/DEV/LOCATE/15.1371/subdirectory/

:: as written, it is expected that RE-DATE.bat is run from within the COPY directory
:: but if i use %copy%\%%i ...


@echo OFF
:: SET SOURCE=C:\DEV\Locate
SET SOURCE=T:\NEW\EPORNER.COM\LOCATE-robo

SET   COPY=C:\OUT\LOCATE


goto RE-DATE-ONLY:



:: robocopy  <source>  <dest> /e /xf *

:: copy source to destination, including subdirectories, even empty ones, & exclude, EVERYTHING (all files) [so only directory /structure/ is copied - fast]
:: this also retains the <source> directories date/time

:: echo   %SOURCE%
:: echo   %COPY%


ROBOCOPY  %SOURCE%  %COPY%  /E /XF *

ECHO.
ECHO Directory date/time have been recorded.
ECHO Now, go ahead and make any changes to the source directories that you want :-).
pause



:RE-DATE-ONLY


ECHO.
ECHO About to RE-DATE.
ECHO About to RE-DATE the directories within:  %SOURCE%\
ECHO.
ECHO IF you press any key, RE-DATE will proceed - else, press Ctrl+C to break out.
pause

:: RE-DATE
:: for /d %%i in (*) do touch "%source%\%%i" -r "%%i"


:: --no-create will NOT re-create a directory deleted on the source end
:: for /d %%i in (15.1371) do touch "%source%\%%i" -r "%copy%\%%i"  --no-create
for /d %%i in (*) do touch "%source%\%%i" -r "%copy%\%%i"  --no-create



ECHO.
ECHO RE-DATE'd and Done.
PAUSE
1. after modifying files within various directories, any 4-6-2024 date was modified, today
2. my original backup up directory tree structure
3. after RE-DATING
.
Everything - RE-DATE'd directories.png
Everything - RE-DATE'd directories.png (334.3 KiB) Viewed 3485 times
.
FolderTimeUpdate intended actions. Note numerous millisecond* related changes that will happen.
(millisecond* related changes: Can't say I know if that is significant in any way or not, or significant to other utilities?)
(I'll get around to asking for some sort of tolerance factor.)
.
FolderTimeUpdate intended changes on SOURCE directories.png
FolderTimeUpdate intended changes on SOURCE directories.png (298.44 KiB) Viewed 3485 times
therube
Posts: 4972
Joined: Thu Sep 03, 2009 6:48 pm

Re: Deleting files without touching a directories date

Post by therube »

(backup copies i made before doing any of the above)


note that robocopy retains both DM & DC (directory) dates - across volumes (might be different, elsewhere)
(fcp did the same)



ROBOCOPY . T:/NEW/LOCATE-robo /e (& / *are* allowed ;-))

Code: Select all

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       176       175         1         0         0         0
   Files :      4259      4259         0         0         0         0
   Bytes :  31.752 g  31.752 g         0         0         0         0
   Times :   0:06:46   0:06:30                       0:00:00   0:00:15


   Speed :            87323988 Bytes/sec.
   Speed :            4996.718 MegaBytes/min.
fcp . /TO=T:/NEW/LOCATE-fcp/ (& / *are* also allowed ;-))

Code: Select all

C:\DEV\Locate>fcp . /TO=T:/NEW/LOCATE-fcp/
=================================================
fcp(ver5.5.0) start at 2024/04/06 09:34:30
<Source>  C:\DEV\Locate
<DestDir> T:\NEW\LOCATE-fcp\
<Command> Diff (Size/Date)
<FileLog> C:\BIN\Log\20240406-093430-0.log
-------------------------------------------------

TotalRead  = 32,514 MiB
TotalWrite = 32,514 MiB
TotalFiles = 4,259 (177)
TotalTime  = 04:27
TransRate  = 127.3 MB/s
FileRate   = 15.9 files/s
note that ROBOCOPY copies ADS, by default
FCP does not do that (by default, & unsure offhand if it does it at all ?)

on this 33 GB of data fcp was 2.min (52%) faster - in this situation
(robocopy did copy the ADS which were 18 MB x 3, but still)
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: Deleting files without touching a directories date

Post by NotNull »

therube wrote: Mon Apr 08, 2024 2:49 pm Note numerous millisecond related changes that will happen.
A theory:
I'm guessing your touch comes from gnuwin or cygwin?
That will likely use the POSIX subsystem for files and I suspect will that will use a different time resolutioncompared to the Windows FILETIME granularity.
(lots of guessing ..)

Untested, but this might work too, without using touch or a for-loop to parse all items:

- robocopy.exe "c:\source folder" "c:\save-the-date\" /E /XF *
- (execute your actions in "c:\source folder")
- robocopy.exe "c:\save-the-date" "c:\source folder" /E /XF *
therube
Posts: 4972
Joined: Thu Sep 03, 2009 6:48 pm

Re: Deleting files without touching a directories date

Post by therube »

with a quick look, that (robocopy alone) is looking good :-)
(come to think of it, FreeFileSync forums has probably mentioned that before. i'll have to look...)

& thinking more about it, odd that it even does as it does.
but then, i guess if you look at it that you're copying an "item" & retaining its' date & time.

only in this case, this "item", instead of being a file is a directory "item"
(& rather then a directory & its' contents), & that directory has a date & time,
just like a file, so in that respect it might not be that "odd".

robocopy also re-dates subdirectories to the parent source (heh, did that make sense ;-).)
(in my situation, i wasn't really concerned about those, so if they were touched, it didn't matter.)

fcp can do the same as robocopy
- fcp c:/source/ /to=c:/dest/
fcp is weird with trailing /, or not & i didn't explore that
(as in above might need to be, fcp c:/source /to=c:/dest/ or maybe, c:/source /to=c:/dest - be careful, heh)
also its' /include= /exclude= include/exclude both files *&* directories,
so how to tell it to exclude all files (if there happened to be files in the backed up dated directories)
i'm not sure offhand, but i suppose it could be done?
fcp can be awkward, at times. any of these copy programs can be awkward, at times.

(fcp.exe is the command-line version of FastCopy.)
anmac1789
Posts: 669
Joined: Mon Aug 24, 2020 1:16 pm

Re: Deleting files without touching a directories date

Post by anmac1789 »

therube wrote: Fri Apr 05, 2024 6:10 pm Deleting files without touching a directories date?
Conceptually, how do you go about this?


I've got some files that I want to delete from various top-level directories - without changing the directory time stamp.

So:

X/file1
X/file2
X/file3
Y/file1
Y/file2
Y/file3
Z/file1
Z/file2
Z/file3

& I want to delete all the 'file1' from there - retaining the (existing) directory times.
Now I could use something like, FolderTimeUpdate, but depending on the directory layout, it can be quirky in actual operation.

So then I thought, if there were a utility that would delete without touching, but then thought, except for malware writers, probably not.

Something like: rm -r -no-touch file1

I know copy programs can copy with a directory structure, retaining dates.
And I could exclude from the copy unwanted files - like 'file1' - while also retaining copied file AND directory dates.
(FastCopy can do that. Robocopy too, I'm guessing, but didn't look?)
But that the actual copying of files, is not efficient.

Then I thought if I de-dup'd the file1's, by sym-linking them, maybe ? that would not touch the directory dates?
And then if I deleted all those links, except for 1, that maybe ? that too would not touch the directory dates?
(I did not test & still awkward.)

So now, I'm thinking about copying the directory structure (no files) - which will be fast.
Deleting said files ('file1') from the source directories.
Then using the the copied directory date/time (which are* maintained with the copy) as basis to then touch (re-date) the original source directory dates.

So something like this to copy the source directory structure:

robocopy <source> <dest> /e /xf *
robocopy C:\TMP\BRU\BRC .\out2 /e /xf *

And then something like this to re-date the source directory dates:

set source=C:\TMP\BRU\BRC
set copy=C:\out\222\out2

for /D %%i in (*) do touch.exe "%source%\%%i" -r %%i

The -r uses %%i (the copied directory date) as its' time to re-date the source directories.

?


which are* maintained with the copy
Are*, may depend on the particular copy utility & "quirks" it may have.
What you can do is to make a zip using winrar of only folders using this switch
-x *.*
of the top level parent folder and then when you delete/add any files within those subfolders, you can just overwrite the parent folder with the folders you have within the zip file so that all the folders timestamps will remain intact
Post Reply