ni_hao
ni_hao

Reputation: 426

windows batchfile: change filestamp

I have several files where the filename consists of a date. I want to change the date & time of the files (filestamp) into that one which is in the filename by using a windows batchfile. Let assume the file is 2013-02-20.txt and I want that file having a datestamp correspondending to which is in the filename and thus set to 20130220, while the timestamp can be set to "00:00". I extract the year, month and date from the filename into variables but how to filestamp these files with that date & time?

for %%f in (*.txt) do (
  set FILENAME=%%~nf
  set YEAR=!FILENAME:~0,4!
  set MONTH=!FILENAME:~5,2!
  set DAY=!FILENAME:~8,2!
  set TIME=00:00
)

Question is how to change the filedate and filetime using the variables YEAR, MONTH, DAY and TIME (in Linux I do it with the 'touch' command)?

Upvotes: 0

Views: 258

Answers (3)

ElektroStudios
ElektroStudios

Reputation: 20464

I did my own CLI app written in .NET to get/set filestamps, it's so easy to use and has beneffits than filetouch for windows, maybe you will preffer to use mine app.

Download: http://elektrostudios.tk/FileDate.zip

enter image description here

enter image description here

Upvotes: 1

ni_hao
ni_hao

Reputation: 426

I fixed it with 'touch' which is in the coreutils package. I downloaded coreutils from here. Then I added the folder C:\Program Files (x86)\GnuWin32\bin to the windows PATH and used this batch file:

@echo off
set TIME=0000
for %%f in (*.txt) do (
  set FILENAME=%%~nf
  set YEAR=!FILENAME:~0,4!
  set MONTH=!FILENAME:~5,2!
  set DAY=!FILENAME:~8,2!
  set NEW_STAMP=!YEAR!!MONTH!!DAY!!TIME!
  touch -t !NEW_STAMP! %%f
)

goto:EOF
:EOF
pause

Upvotes: 0

Leptonator
Leptonator

Reputation: 3519

Does it have to be a Batch File?

You can do it in Batch, but not easily. Stay with me here and don't lose heart. :)

Starting in this post - http://www.dostips.com/forum/viewtopic.php?f=3&t=4846

And there is some bleed-over to this post - http://www.dostips.com/forum/viewtopic.php?p=27422#p27422 and I will be perfectly honest with you, I have not re-timestamped a file using direct batch.

Have done it with the next thought or idea: You can do this pretty easily in VBScript or in PowerShell..

VBS:

Set fso = CreateObject("Scripting.FileSystemObject")
' -- Re-date files
'    Call Touch(Server.MapPath("/"), "somefile.htm", "2005-09-01") 
'    Call Touch("C:\", "somefile.txt", "2012-01-01") 
Sub  Touch(strDir, strFileName, NewDate)
Dim objShell, objFolder, objFile
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.NameSpace(strDir) 
Set objFile = objFolder.ParseName(strFileName) 
If fso.FileExists(strDir & strFileName) Then
objFile.ModifyDate = NewDate 
End If
End Sub

PowerShell:

if ($DTNew) {
    (dir $aZip).lastwritetime = $DTNew
}

Upvotes: 0

Related Questions