Page 1 of 1

How to delete lines of file without changind date?

Posted: 19 Mar 2024 12:31
by rodrigo.brasil
No, this is not possible. But there is a workaroud:
  • You edit the file
  • Then you change the date
Now I have a problem:
  1. Can I edit the file inplace?
  2. If YES, I need to store the date in memory an then change it
  3. If NO, I need create a new file, toch it, delete the old and rename the new one
Until now, I don't see the solution to edit it inplace.

The other option, I see the answer from this question. This is the command:

Code: Select all

type input.txt | findstr /v ERROR | findstr /v REFERENCE > output.txt
Now, how can I change the last modified date of the output.txt file to be equal to input.txt?

After this, just delete and rename the new file.

So, how can I edit a file inplace or how can I copy the last mofified date of one file to another only using cmd.exe? Is this possible?

Re: How to delete lines of file without changind date?

Posted: 21 Mar 2024 19:17
by penpen
You might use powershell within cmd.exe to change the last modified date of a file, so this might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

set "sourceFile=%~f0"
set "targetFile=stest.txt"

powershell.exe -Command "$(Get-Item '%targetFile%').LastWriteTime=$(Get-Item '%sourceFile%').LastWriteTime"

goto :eof
penpen