Batch file which deletes a file which last modification was not today - batch-file

I just want to 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!

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

You can use forfiles:
forfiles /D -1 /M log.txt /C "cmd /c del #file"

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

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.

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

Related

ForFiles Newer Than Batch

I'm working with the forfiles command
I am attempting to get a list of files newer than X days ago.
When I use the /D +X command I get a not files found error:
forfiles /P %filename% /D +3
However if I use a date it works fine:
forfiles /P %filename% /D +06/29/2016
Am I missing something on the +X formatting? It seems like it is adding 3 days to the current date and then looking for files newer than that.
Or is there a simpler way to increment the date than I'm aware of. Only work with Batch once in a while
I don't want to use vbs, and I would rather not have a set of code longer than the rest of my script just to get the date, there should be a simpler way.

Using For /R to delete files from subdirectories

This is a really beginners question. I have never work with scripts before and today I decided to start learning because of some unknown reason upgrading to Windows 10 duplicated many of my files. There is a pattern for music files, duplicates end in -1,-2,-3... and so on. I created a script with the following and it did work to delete duplicated files.
del E:\folder_name\*-1.mp3
del E:\folder_name\*-2.mp3
del E:\folder_name\*-3.mp3
However, there are too many folders and specifying one by one will take me forever. I found this script to recursively loop through subdirectories.
For /R E:\music_sample\ %%G IN (*-1.mp3) do Echo del "%%G"
Which produces the following output
but it does not actually deletes the files. Can you help me understand what I am doing wrong? Thanks!
Remove the term "Echo" from your script:
For /R E:\music_sample\ %%G IN (*-1.mp3) do del "%%G"
Taking a moment to learn something new is always great!
Sometimes there is already a tool for the job.
Agent Ransack allows you to search by patterns/regular expressions for files. This or something similar might save you some time in the future.
del /s "%userprofile%\music\*-1.mp3" "%userprofile%\music\*-2.mp3 "%userprofile%\music\*-3.mp3"
type
set u
and
del /?
for help.
Command prompt has it's own regular expressions. It's different to dos even if the tokens are the same. EG in dos * is only valid at the end of a filename or extension but in command prompt it is valid anywhere.

Delete first two lines from txt files

I need to delete the first two lines from a very large amount of txt files (about 10.000). I'm looking for a way to do this from the command line or through another semi-automatic procedure. Every file is different from the other, but they all contain some information on the first two lines that I need to get rid of.
for %%a in (*.txt) do (
more +2 < "%%~fa" > 2linesskipped.txt
move /y 2linesskipped.txt "%%~fa"
)
without external tools.
I think aguslr's answer looks good on a UNIX-derivative system, but I'm guessing since the tags are "cmd" and "batch-file" that you are on Windows. You can get sed for Windows in several places, for example in the UnxUtils ports or more up-to-date ones via Cygwin or GnuWin32. But find is a built-in Windows program that is nothing like the find on UNIX-derivative systems. So you would probably want to replace the second half of the answer with a "for" command in Windows like:
for /r %a in (*.txt) do sed -i '1,2d' %a
Note that if you run this from a batch file you will want to use %%a instead of %a.

copy files by date to another folder

I want to be able to create a batch files that copies files by date only up to 3 or 4 days but use the %edate% %user%\ syntax instead of specifying a date. Here is what I have so far:
XCOPY %user%\folder_1 %edate% %user%\folder_2.
I am unable to download any 3 party software. I can only use the .bat format.
The comment suggestion to use forfiles is a good one, but I note the question has remained open. Perhaps more details are required.
If the questioner has a system more recent than XP (and I assume so, as XP and previous are now deprecated) the ROBOCOPY also has appropriate options. I'll give examples of both for files greater than 4 days old.
Using forfiles:
FORFILES -P %user%\folder_1 /D - 4 /C "CMD /C XCOPY #file %user\folder_2"
Using ROBOCOPY:
ROBOCOPY %user%\folder_1 %user%\folder_2 /MINAGE:4
I hope that is sufficient detail....

Batch - Remove backups a week old

So I've got a batch file that backs up a folder to my Google Drive directory, like so:
C:\Program Files\WinRAR\rar.exe a -r "D:\Google Drive\Saves Backup\%DATE%.rar" "D:\Documents\My Games\"
This makes a file called 30-Sep-12.rar (being run today) in the appropriate folder.
However, my question is this: Is there some way to go through said folder (D:\Google Drive\Saves Backup) and delete backups that are more than a week old, as determined by the filename?
Why must you use the date embedded within the file name? The last modified date should be the same as the date embedded in the file name as long as the backup has not been modified since it was created.
FORFILES is one of the few Windows utilities that conveniently works with date arithmetic. Type FORFILES /? from the command line to get help on its usage.
forfiles /p "D:\Google Drive\Saves Backup" /m "*.rar" /d -7 /c "cmd /c del #path"
If you have a risk that someone could modify a backup, thus changing the last modified date, then the above will not work. Parsing and comparing dates is a pain in batch. You would be better off using VBScript.

Resources