Link to home
Start Free TrialLog in
Avatar of webdott
webdottFlag for United States of America

asked on

vb script creates new txt file, but not overwrite existing one

i have a vbs script that will create a new .txt file with today's date as the name

but if i run it on the same day, it overwrites the existing file.txt.

is there a way to have it create the name as today, if exists add +1

example:
2011-09-17.txt  exists reate 2011-09-17-2.txt or 2011-09-17-v2.txt

thanks
 
Dim TheDate, InputNote, fileOutput

REM TheDate is in YYYY-MM-DD format
TheDate = year(date) & "-" & right(100+month(date),2) & "-" & right(100+day(date),2)

InputNote = inputbox ("enter your note", "Note entry for " & date)
If InputNote="" then
  Msgbox "Cancelled. No notes created."
  wscript.quit
end if

Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set fileOutput = objFSO.CreateTextFile("C:\mydocs\notes\" & TheDate & ".txt")
    fileOutput.WriteLine InputNote
  fileOutput.Close 
Set objFSO = Nothing

Open in new window

Avatar of expert20
expert20

Hi

We can create a text file by timestamp - it will be uniq always

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
timestamp = replace(replace(now,"/","_"),":","_")
set fso = createobject("scripting.filesystemobject")
fso.createtextfile "D:\textfile_"&timestamp&".txt"
set fso = nothing

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Avatar of webdott

ASKER

forgive me - i am new to vbs scripts.

how would i put that into my vbs script?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry meant to paste this in for alternative above:

Dim TheDate, InputNote, fileOutput  
 
REM TheDate is in YYYY-MM-DD format  
TheDate = year(date) & "-" & right(100+month(date),2) & "-" & right(100+day(date),2)  
TheTime = right(100+hour(now),2) & ":" & right(100+minute(now),2) & ":" & right(100+second(now),2)
 
InputNote = inputbox ("enter your note", "Note entry for " & date & " at " & TheTime)  
If InputNote="" then  
  Msgbox "Cancelled. No notes created."  
  wscript.quit  
end if  
 
Set objFSO = CreateObject("Scripting.FileSystemObject")  
  Set fileOutput = objFSO.OpenTextFile("C:\mydocs\notes\" & TheDate & ".txt",8,true)  
    fileOutput.WriteLine TheTime & chr(9) & InputNote  
  fileOutput.Close  
Set objFSO = Nothing

As suggested in previous question for amending date formats etc. have a look at the options here:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
Hi

Just added my code to existing one...

Dim TheDate, InputNote, fileOutput

TheDate = replace(replace(now,"/","_"),":","_")
set fso = createobject("scripting.filesystemobject")
fso.createtextfile "D:\textfile_"&timestamp&".txt"
set fso = nothing

InputNote = inputbox ("enter your note", "Note entry for " & date)
If InputNote="" then
  Msgbox "Cancelled. No notes created."
  wscript.quit
end if

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fileOutput = objFSO.CreateTextFile("C:\" & TheDate & ".txt")
fileOutput.WriteLine InputNote
fileOutput.Close
Set objFSO = Nothing
Avatar of webdott

ASKER

that's what i need - thanks