Solved

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

Posted on 2011-09-18
6
503 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 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