Solved

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

Posted on 2011-09-18
6
499 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:webdott
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Expert Comment

by:expert20
ID: 36556589
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

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
0
 

Author Comment

by:webdott
ID: 36556643
forgive me - i am new to vbs scripts.

how would i put that into my vbs script?

thanks
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 36556677
This will save your file as yyyy-mm-dd hh.mm.ss if that will work for you?

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)  
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 & " " & TheTime & ".txt")  
    fileOutput.WriteLine InputNote  
  fileOutput.Close  
Set objFSO = Nothing

Another alternative would be to have a note for the same day appended to the same file, if you wish:

e.g.

2011-09-18.txt

11:00  First entry
12:00 Second entry
etc.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36556689
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:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
0
 
LVL 1

Expert Comment

by:expert20
ID: 36556839
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
0
 

Author Closing Comment

by:webdott
ID: 36557208
that's what i need - thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now