Page 1 of 2

Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 11:53
by MikeP
In Command Shell

Code: Select all

copy  *.*  /B  +  ,,  /Y

will copy/replace all of the files in my directory, and give them todays date

I would like to create the same thing, but for my entire h: (including subfolders)

Initially I thought

Code: Select all

xcopy
or

Code: Select all

FOR

but I haven't had much luck,

I would greatly appreciate any help in the right direction

Mike Praeuner
Spring, TX

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 12:17
by Squashman
Copy seems like it would take a long time to do that on a lot of files. Seems like you would be better off with a 3rd party utility to update the time stamps.

Code: Select all

for /F "tokens=*" %%G in ('dir /b /s /ad H:') do copy  "%%G\*.*"  /B  +  ,,  /Y

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 12:24
by Squashman
I wonder if this might work quicker. I tested it on a few excel files just to make sure it wasn't corrupting a binary file.

Code: Select all

for /F "tokens=*" %%G in ('dir /b /s /a-d H:') do type NUL >>  "%%G"

This would update the modified date.
Were you looking to update the create date?

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 15:05
by MikeP
I think modified date, but how would the syntax change for created date?

Also to be clear can I use the above statement directly from cmd.exe or a batch file?

(Going to experiment myself when I am back at a computer. . .)

- Thanks for the suggestions,

Mike

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 15:09
by MikeP
.... also, I would love to use a 3rd party app, but I am working from a corporate machine that is severely limited.
So I am trying to accomplish this within the means at my disposal (at home I would have gone the 3rd party route before considering this)

-Thanks,

Mike

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 15:27
by dbenham
I just tested the COPY /B /Y *+,, method on a folder that included many huge files, including one file that was 1GByte. The copy is "instantaneous" - it does not actually read, let alone write, the entire file. It simply updates the last modified timestamp. So performance should not be a problem - no need for an external tool. 8) :D

In case it matters (hopefully it doesn't) I tested on Vista.


Dave Benham

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 15:34
by MikeP
The power of Command Shell (Dos / Batch scripting) always amazes me, a simple but effective tool that is often overlooked (and because of that most often not restricted by IT)
:lol:
My initial "simple" test in a sub directory was lightning fast (about 100 files)

-Mike

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 16:08
by Squashman
MikeP wrote:I think modified date, but how would the syntax change for created date?

Also to be clear can I use the above statement directly from cmd.exe or a batch file?

(Going to experiment myself when I am back at a computer. . .)

- Thanks for the suggestions,

Mike

Reading the first couple of lines from the help should give you that answer.
C:\Users\Squash>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.


I think changing the create date might require a 3rd party tool unless Dave has a trick up his sleeve.

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 16:19
by Fawers
Squashman wrote:

Code: Select all

for /F "tokens=*" %%G in ('dir /b /s /ad H:') do copy  "%%G\*.*"  /B  +  ,,  /Y


What do those 2 commas next to /Y do, exactly? They are confusing me.

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 16:43
by dbenham
Actually the commas are not needed, nor is the /Y option needed.

The following should work just as well:

Code: Select all

for /F "tokens=*" %%G in ('dir /b /s /ad H:\') do copy /b "%%G\*"+

As should this:

Code: Select all

for /r "h:\" %%D in (.) do copy /b "%%~fD\*"+

And as foxidrive alluded - make all double percents single percents to run from the command line.


Dave Benham

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 16:49
by foxidrive
MikeP wrote:.... also, I would love to use a 3rd party app, but I am working from a corporate machine that is severely limited.


I'm curious. Why do you need to do this on a corporate machine?

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 18:18
by MikeP
I am doing this because I have 4GB of files that are falling into our "new" file scanning software that identifies any file older than 12 months -then automatically schedules the files for deletion unless you open each individual file and save with 30 days.

This is stop gap solution to buy me some time to sort thru the files, without having to open every single one.

Along with this IT has diasbled all USB devices, so I can't just back them up to an external source (for security reasons)

I am playing with both solutions (over VPN) it looks like it might be dumping all of the files from my folders into the root?

will let it run for a bit and see what happens

I appreciate all of the input !
-Mike

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 18:27
by MikeP
This one is faster:

Code: Select all

for /r "h:\" %%D in (.) do copy /b "%%~fD\*"+

Code: Select all

for /F "tokens=*" %%G in ('dir /b /s /ad H:\') do copy /b "%%G\*"+
for /F "tokens=*" %%G in ('dir /b /s /ad H:') do copy  "%%G\*.*"  /B  +  ,,  /Y


I tried the three above and had similair results, started dumping all of the files from my subfolders into the root of H:

Running from a batch file with just the one line of code, batch file is placed on the root of H:

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 19:17
by MikeP
As a quick test I tried moving the batch file to my desktop, but it just started copying files to my desktop instead. . .

- Mike

Re: Using a batch to change the File Date (recursively?)

Posted: 16 Apr 2012 19:18
by Liviu
MikeP wrote:Along with this IT has diasbled all USB devices, so I can't just back them up to an external source (for security reasons)

Just wondering, but have you considered asking the ITs about _their_ reasons?

You sounded unsure whether you meant the last-modified vs. created timestamp, and unaware that there is a 3rd last-accessed one. Also that any "copy /b" approach requires full "write" rights to the file, as opposed to the lesser "metadata update" rights needed for just timestamp updates. Also that most production-level filesystems have "journaling" enabled, where ITs can lookup what's been done (or attempted) behind their backs. Also that blanket-changing all timestamps on the full drive could break unsuspecting apps, from version-control to auto-updaters. Don't know the context and don't mean to prejudge, but this certainly looks like the wrong solution.