views:

265

answers:

5

I just wanna know:

How can i delete 'log.txt' if the last modification was not today?

With a batch file.

I'm just talking here about 1 FILE!

+2  A: 

PowerShell must replace batch.

if ((dir log.txt).LastWriteTime -lt [datetime]::today) { del log.txt }

Jay Bazuzi
After seeing your example, I agree :)
RedFilter
...although I`m a little saddened - I just realized batch was my first language about 25 years ago. GOTOs and everything!
RedFilter
is it really not possible without powershell?Not all computers have Powershell. It's going to be something with more users and i don't want to make it too diffecult for them.
YourComputerHelpZ
After a career of extreme CMD scripting (self-modifying, multi-threaded, auto-updating, deeply recursive = extreme), I am now convinced that even if it's *possible* in CMD, I want to do it in PowerShell anyway.Yes, getting PowerShell deployed to pre-Win7 computers is work, and that can suck. But it's so worth it.
Jay Bazuzi
A: 

Batch makes something that simple very complicated. If you can use Cygwin, or install Unix Tools, consider doing something like this:

find log.txt -mtime +1 -exec rm {} \;

UPDATE
Found something that might be useful to you using pure batch. You should be able to modify that and adapt it to suit your needs.

nmuntz
"Found something that might be useful to you using pure batch" - I wouldn't use it as it relies on assumptions about the regional settings (US date format).
Joe
+1  A: 

Unfortunately you can't do this in a robust and general way (*).

Powershell, Cygwin, Unix Tools are perfectly good solutions if you can be sure they'll be installed on the target machine.

I wrote a little utility program that takes a path with wildcards and number of days and deletes all files matching the path that are older than the specified number of days. In my environment this was more convenient than installing a 3rd party package.

(*) The following will work for your specific case (modification date not today) as long as the short date format in your regional settings includes the century (i.e. is 10 characters long). But it doesn't generalize for N days, and I don't like relying on the computer's regional settings for this kind of thing:

for %%i in (log.txt) do SET FILETIME=%%~ti
IF NOT "%FILETIME:~0,10%" == "%DATE%" DEL /f log.txt
Joe
+1  A: 

You can use forfiles:

forfiles /D -1 /M log.txt /C "cmd /c del @file"
Joey
If I'm going to go through the trouble of installing a new program, I'll install PowerShell instead.(Also, you should link to forfiles)
Jay Bazuzi
Huh? Forfiles is contained in recent versions of Windows. No need to install anything.
Joey
forfiles is fine, because if you use PowerShell, you really have to install a new program. with forfiles, you can just put forfiles.exe in the directory of the batch.
YourComputerHelpZ
A: 

For what its worth, you could use either jscript or vbscript. Both of these scripting languages have been installed on MS platforms since the mid 90's

Peter M