How to delete lines of file without changind date?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rodrigo.brasil
Posts: 10
Joined: 20 Jun 2023 16:15

How to delete lines of file without changind date?

#1 Post by rodrigo.brasil » 19 Mar 2024 12:31

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?

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

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

#2 Post by penpen » 21 Mar 2024 19:17

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

Post Reply