Solved

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

Posted on 2011-09-18
6
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 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