Link to home
Start Free TrialLog in
Avatar of Milewskp
MilewskpFlag for Canada

asked on

DOS commands to change the File Modification Date and File Creation Date.

Are there DOS commands to change the File Modification Date and File Creation Date of files?
Avatar of Joseph Daly
Joseph Daly
Flag of United States of America image

There are no native windows applications that can do this but there is a third party program that was created that can do this.

http://www.anti-forensics.com/tag/timestompexe
What are you trying to do -- does it need to be from command promt, or is this to amend the date.time across a bunch of files?  There is a nice util. I have at home which pulls date/time from JPG files for instance to update the modified time with the Photo taken time.

You can do it in Powershell but not in VBScript afaik:

If powershell is an option is along these lines... learning my way through PS myself so might need tweaks.  This will set all the files in c:\somedir to 11:11 tomorrow.

$d = Get-Date "11/11/2011 11:11 AM"

$x = Get-ChildItem "C:\somedir"

foreach ($f in $x)
{
  $f.LastWriteTime = $d
}

Steve
Avatar of Milewskp

ASKER

Hi dragon,
I don't know Powershell. I'm trying to set something up in DOS batch file. Will the code you provided work in a batch file?
do you have powershell available if needed?  not sure what that link given above as cant see from here... Is that a commandline util?

Meanwhile can you explain what you are wanting to do a bit more please ?

steve
We don't have powershell at my company, and good luck trying to get it. Besides, I'd rather not deal with a new application or addin for this minor issue (Not worth it.). If it can't be done in DOS then I'll probably skip it.

Here's why I asked the question:
The File Creation Date generated by Windows is not reliable
(see: https://www.experts-exchange.com/questions/27439930/Is-File-Modification-Date-reliable.html?cid=1575&anchorAnswerId=37111022#a37111022 ),
which makes me suspicious of File Modification Date. If I can't trust the values that Windows generates, then one option is to change then myself after I create a file.
ok. I dont know a way in batch then, and have never seen the properties exposed under vbscript filesystemobject etc (though could have missed them I suppose)


Your options I imagine are third party tool, command line or otherwise or powershell then.  with the latter if it was installed you can call it from batch etc. Easily enough -basically run powershell.exe pointed at the ps* file.

Anyway sorry afaik answer is can't without a pc at hand to delve deeper.

I do have this gui util you can drag files onto to reset dtaes which i use to mod jpg files from their camera time picture was taken before renaming them by date in batch... But cant rememer off hand whether it only works for jpgs.  will check in couple of hours when at home.

Steve
btw never knowingly had a problem with modification dates..... If you want to amend the date time of a specific file to current time there ARE touch commands around which will do that and stamp it with the time now.  on mobile so difficult to look but a google will show you one... :-)

Steve
Steve, could this be done from vbscript, created with a series of echos, then cscripted from a batch file?
not afaik.  i dont know anywhere accessible to vbscript where you can set those fields... You can read them but dont think you can set.  will check if anytjing ive not used before later on at home.

Steve
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Steve:
How can nircmd be used to change the date?
Avatar of Bill Prew
Bill Prew

If you can use a small executable to get the job done, you could grab the "touch" utility from this package:

http://unxutils.sourceforge.net/

~bp
Right, can confirm that this works from VBScript... I was always under the impression it couldn't be done!

Sub Touch(FolderPath, FileName, NewDate)
         
        Set app = CreateObject("Shell.Application")
        Set folder = app.NameSpace(FolderPath)
        Set file = folder.ParseName(FileName)
         
        file.ModifyDate = NewDate
 
        set file = nothing
        set folder = nothing
        set app = nothing
End Sub
 
Call Touch("C:\4. WIP\EE", "copyit.cmd", "2012-01-01")

So... is VBScript an option.  Are you happy for it to be supplied to you as a VBScript you can call with

cscript //nologo "dir" "filename.txt" "date"

or do you want it to do it for all files in a dir etc.

Steve
If a utility is acceptable, a couple of addition free tools to try:

http://www.softtreetech.com/24x7/archive/47.htm

http://www.stevemiller.net/apps/

~bp
OK here you go with a VBScript.  Save this as touch.vbs (or whatever you feel like).

You can then do:

This to set mod. time to dd/mm/yyyy 0:00:
touch.vbs "C:\somedir" "somefilename.txt" "dd/mm/yyyy"

or this to set mod. time to dd/mm/yyyy hh:mm:ss
touch.vbs "C:\somedir" "somefilename.txt" "dd/mm/yyyy hh:mm:ss"

or this to set it to the time and date now.
touch.vbs "C:\somedir" "somefilename.txt"

We could mod. this to do multiple files if needed, or you can call it from batch file, e.g.

@echo off
for /f "delims=" %%A in ('dir /b /a-d "c:\somedir\*.pdf"') do cscript //nologo "C:\somedir" "%%~A" "1/1/2011"

to find all the files call *.pdf in c:\somedir and run "touch" over them with the date of 1/1/2011

Steve
REM Command in VBScript to touch files with current date, or supplied date:

Dim Dirname,Filename,TheDate

If Wscript.Arguments.Count <2 Then
  wscript.echo "Usage: touch.vbs ""c:\dirname"" ""filename.txt"" [""dd/mm/yyyy hh:mm:ss""]"
  wscript.quit
Else
  If Wscript.Arguments.count=2 then
    TheDate=Date & " " & time
  Else
    TheDate=wscript.Arguments(2)
  End If
End If

Dirname=wscript.arguments(0)
Filename=wscript.arguments(1)

call touch(dirname,filename,thedate)

Sub Touch(FolderPath, FileName, NewDate) 
         
        Set app = CreateObject("Shell.Application") 
        Set folder = app.NameSpace(FolderPath) 
        Set file = folder.ParseName(FileName) 
         
        file.ModifyDate = NewDate 
 
        set file = nothing 
        set folder = nothing 
        set app = nothing 
End Sub

Open in new window

Hi Bill,
Where is the touch.exe utility?
Hi dragon,
Thanks, but I need a DOS solution. How do I plug this code into a .bat file?
==> Where is the touch.exe utility?

On the following page:

http://unxutils.sourceforge.net/

is a download link to:

http://unxutils.sourceforge.net/UnxUtils.zip

which contains the touch.exe utility.

In addition I posted a couple of additional location for similar tools above.

~bp
See http:#37117552

You can use it from a batch file with

cscript //nologo touch.vbs "c:\dirname" "filename.txt" "dd/mm/yyyy"

If needed we can make it make the VBS on the fly in a temp. drive self contained in the batch file.

Steve
i.e. you create touch.vbs, and save it somewhere to run, or in the same dir as the batch for instance, or:

cscript //nologo c:\myscriptsdir\touch.vbs "c:\dirname" "filename.txt" "dd/mm/yyyy"

Steve
Did you try the exe's to download, or the vbscript / batch file given above.  If you did want to have it just in batch you could have it create the VBS for you, a bit trimmed down of some of it's baggage of error checking... and added some more of it in the batch file itself:

You just need the two subroutines under the REM at the bottom and then the line:

call :touch "c:\4. WIP\EE" "log.txt" "1/11/2011 12:43"

The rest is just showing it working.

Steve
@echo off
REM Command in VBScript to touch files with current date, or supplied date:
REM Stephen Knight.  Dragon-IT.  http://www.dragon-it.co.uk/
REM EE question 27441352
CLS

call :CreateTouchVBS

echo Before:
dir "c:\4. WIP\EE\log.txt" | find ":"
echo.

call :touch "c:\4. WIP\EE" "log.txt" "1/11/2011 12:43"

echo.
echo After: 
dir "c:\4. WIP\EE\log.txt" | find ":"

exit /b

REM These two subroutines just need pasting at the bottom of your current script
REM Make sure there is an EXIT /B or goto:eof above it so that your script stops before them
REM
REM Touch SUB Saves having to type all this on each TOUCH Line
:Touch
cscript //nologo "%temp%\touch.vbs" "%~1" "%~2" "%~3"
echo File "%~1\%~2" changed to %~3:

exit /b

REM This SUB creates Touch.vbs in the Temp drive if not already there
:CreateTouchVBS
if exist "%temp%\touch.vbs" exit /b
echo Creating "%temp%\touch.vbs"
(echo Dim Dirname,Filename,TheDate
echo If Wscript.Arguments.count=2 then TheDate=Date ^& " " ^& Time Else TheDate=wscript.Arguments^(2^)
echo Set app = CreateObject^("Shell.Application"^) 
echo Set folder = app.NameSpace^(wscript.arguments^(0^)^) 
echo Set file = folder.ParseName^(wscript.arguments^(1^)^) 
echo   file.ModifyDate = TheDate 
echo set file = nothing 
echo set folder = nothing 
echo set app = nothing 
) > "%temp%\touch.vbs"
exit /b

Open in new window

Folks, I decides on Steve's solution from
http://classicasp.aspfaq.com/files/directories-fso/how-do-i-change-the-modified-time-of-a-file.html

(my code attached)
It seems the simplest.
Public Sub Touch(FolderPath As String, FileName As String, NewDate As String)
   Const ThisProcName As String = "Touch"
   Call Stacker(conModuleNum, ThisProcName)
   If Not (DefErrHandling Or SSM) Then On Error GoTo UnexpectedError
   'This proc changes the File Modification Date of Filename in folder Folder to NewDate.
   'It is similar to the Touch command in Unix.
   'USEAGE: Call Touch("C:\", "somefile.txt", "2012-01-01")
   'NOTES: Shell does not have a file.CreateDate property.
   'SOURCE: http://classicasp.aspfaq.com/files/directories-fso/how-do-i-change-the-modified-time-of-a-file.html

   Dim app As Object, folder As Object, file As Object
         
   'Initialize
   Set app = CreateObject("Shell.Application")
   Set folder = app.Namespace(FolderPath)
   Set file = folder.ParseName(FileName)
    
   'Change File Modification Date
   file.ModifyDate = NewDate

   GoTo QuitSub
'_________________________________________________________________________________
UnexpectedError:
   Call UnexpectedError(conModuleNum, ThisProcName, Err.Number, Err.Description, Erl)
   Resume
'_________________________________________________________________________________
QuitSub: Call QS(ThisProcName)
   If Not (DefErrHandling Or SSM) Then On Error Resume Next
   Set file = Nothing
   Set folder = Nothing
   Set app = Nothing
'_________________________________________________________________________________
'NOTES:
'  1.

End Sub

Open in new window

Thanks everyone.
Fair enough.  All my batch scripts etc are effectively just using those VBScript methods for you as you asked for batch.... but if you can use VBScript natively so much the better.

Steve