Solved

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

Posted on 2011-09-18
6
502 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…

830 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