4

I need to update the "Date Modified" property of files and folders as they are copied from one location to the other so that "Date Modified" = Current System Time. I have a PC with Windows 7, and I do NOT have administrative rights on it, so I can't install any custom utilities. My current bat file uses XCOPY:

xcopy "\\sharepoint\dept\gis\Abandoned_Wire\*.*" "\\corp.dom\fs4\g1\OUTPUT\GRIDPROD\PDF\Maps\Abandon Wire Maps" /c /s /r /y /i

On my Windows XP box I use the "touch" command from UnxUtils, but I can't find an equivalent that's native to Windows 7. Thank you!

dbenham
  • 127,446
  • 28
  • 251
  • 390
Graeca
  • 41
  • 1
  • 1
  • 2

4 Answers4

8

There is a very simple (though arcane) syntax to "touch" a file on Windows. (update the last modified timestamp)

If the file is in the current directory, all you need is:

copy /b fileName+

If the file is in some other path, then this works:

copy /b somePath\fileName+,, somePath\

However, it seems like you still would have a lot of coding to do, since I believe you only want to touch files that are copied.

The following is untested, though I believe it will work. I can't vouch for the performance. This solution requires 2 unused drive letters. I'm assuming K: and L: are available.

@echo off

:: map unused drive letters to your source and target paths
subst K: "\\sharepoint\dept\gis\Abandoned_Wire"
subst L: "\\corp.dom\fs4\g1\OUTPUT\GRIDPROD\PDF\Maps\Abandon Wire Maps"

:: replicate the folder hierarchy
xcopy K: L: /t

:: recursively copy and touch all files
for /r K: %%F in (*) do (
  xcopy "%%F" "L:%%~pnxF" /r /y
  copy /b "L:%%~pnxF"+,, "L:%%~pF"
)

:: release the temporary drive mappings
subst /d K:
subst /d L:
dbenham
  • 127,446
  • 28
  • 251
  • 390
6

You could use powershell, which I believe is already installed on Windows 7 by default. Add this line to your batch file to run a powershell command that updates the timestamp on all files named Abandoned_Wire*.*:

powershell.exe -command "ls 'folder\Abandoned_Wire\*.*' | foreach-object { $_.LastWriteTime = Get-Date }"

What that line is doing is simply:

-command: tells powershell to run the following command and return immediately

ls: list all matching files at the path specified

foreach-object: run the following block on each file that ls found

$_.LastWriteTime = Get-Date: for each file, set the LastWriteTime to the value returned by Get-Date (today's date and time)

Nate Hekman
  • 6,507
  • 27
  • 30
  • Nice to see someone explain their answer. Vote++. – Simon Catlin Mar 04 '13 at 22:36
  • 1
    I'll assume your ps code functions as advertised, I know next to nothing about ps. But it will "touch" *all* files, not just the files that were just copied. That might be a problem. – dbenham Mar 04 '13 at 23:03
  • @dbenham: No, it will touch all files named "Abandoned_Wire*.*", which is what the OP had originally specified. I see now that the question has been edited by you to add a \, but I'm not convinced that's what the OP meant. I leave it to him/her to decide. – Nate Hekman Mar 05 '13 at 00:06
  • I see the source of the confusion. The original question had the line formatted as a quote instead of code. This resulted in some `"\\"` displaying as `"\"`, and `"\"` displaying as nothing. I reformatted the quote as code, and I'm pretty sure it represents the OP's intent. The paths and the question make more sense now. Your answer made more sense with the originally formatted question, but the edits do invalidate your answer. Even with the original question format, your answer is still susceptible to touching pre-existing files that did not come from the source. – dbenham Mar 05 '13 at 00:31
  • Makes sense. I've added the \ to my answer. You're right, pre-existing files in the target folder will get touched as well. That could be fixed by using powershell to perform the copying in the first place. – Nate Hekman Mar 05 '13 at 05:28
  • I know this is an old answer, but this worked beautifully for me. All files in the folder have been updated to the current date/time. Thanks! – Andrew Mar 29 '22 at 01:41
0

Robocopy should be able to do this for you. robocopy is a native tool included in Windows since Vista.

robocopy "\sharepoint\dept\gis\Abandoned_Wire" "\corp.dom\fs4\g1\OUTPUT\GRIDPROD\PDF\Maps\Abandon Wire Maps" /COPY:DA /S /IS

By default robocopy will copy DAT: Data, Attributes, and the Time stamps, but this can be controlled with the /COPY flag.

See robocopy /? for all the options.

David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • That seems like it should work, but it is copying the source's timestamp for me. – dbenham Mar 04 '13 at 22:56
  • @dbenham Well that would be rather disappointing if it does not work as it says, but not all that surprising. **`:(`** I just tried it myself, and it just ignored the `/copy` flag and performed a `DAT` copy. Time to pull out the debugging! – David Ruhmann Mar 04 '13 at 22:58
  • If anyone has any insight into `robocopy`'s behavior go here http://stackoverflow.com/q/15212914/891976 – David Ruhmann Mar 04 '13 at 23:12
0
for /R %i in (\*.\*) do copy %i /B+ ,,/Y
BDL
  • 21,052
  • 22
  • 49
  • 55
butfly
  • 105
  • 1
  • 6
  • 4
    Although this might answer the question, it is always good to add an explanation to a code sample. – BDL Jun 22 '15 at 10:21