1

I am trying to write a simple batch script that renames, moves and stores crash log files of a program. The log files are named using date stamp and time stamp so the end result is a folder with many log files with different date/time stamps. I want then to list the content of this folder in a list. I am able to do the following:

dir /b >> list.txt

The problem is with this method it lists alla the files in the folder every time a new log file is created. For example, I have the following log files in a folder:

log1
log2
log3

and after the program crashes a new log file is created log4, when the script is run the list shows the following:

log1
log2
log3
log1
log2
log3
log4

Is there any way to append only the new log files to my list?

GIS_DBA
  • 221
  • 2
  • 11
  • 2
    Possible duplicate of [Write to file, but overwrite it if it exists](http://stackoverflow.com/questions/4676459/write-to-file-but-overwrite-it-if-it-exists) – Daniel Bank Dec 08 '15 at 18:37

2 Answers2

4

You are appending by using the double >> but you could simply use one > to overwrite the file. This might be the easiest way if you are not trying to actually exclude any files in the resulting list.

Eric Wilk
  • 59
  • 2
2

I agree with @Daniel and @Eric, off the top of my head I can't think of a scenario where you couldn't just overwrite list.txt with all existing logs using > redirect...

If, for some reason you need to, you could do something like this:

@echo off
if not exist list.txt type nul>list.txt

for /f "tokens=*" %%a in ('dir /b') do call :findDuplicate "%%a"
goto:eof

:findDuplicate
type list.txt | find %1>nul
if %ERRORLEVEL% EQU 0 (
   :: file already in list.txt, so don't append
   goto:eof
   ) else (
   :: file wasn't found, append to list.txt
   echo %~1 >> list.txt
   )
goto:eof
Tommy Harris
  • 109
  • 6