Solved

batch file to create a .txt file

Posted on 2011-09-17
6
473 Views
Last Modified: 2012-05-12
i need a batch file that will create a .txt file when clicked in a folder called C:\DateModified

example:
click on create.bat
it creates a file in C:\DateModified\notes.txt

this is basically used to create a date stamp note.

it would be great if it would do an echo and allow me to add the note inside the cmd box that
shows up, but i do not know if that is possible?

also if it can auto name the file to today's date like,  2011-sept-17 would be awesome,
but do not think that it is fair to ask that with this question for only 500 points
if that can be done, let me know and i will post another question to provide more points for that
question to add file with today's date name.

thanks
0
Comment
Question by:webdott
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554560
something like:
set /p note=Just one line: 
set filename=note_%date%_%time%.txt
echo %date%/%time% > %filename%
echo %note% >> %filename%

rem if you want to note more, enable this
rem notepad %filename%

Open in new window

0
 

Author Comment

by:webdott
ID: 36554602
that is almost it.

1. it does not create it as a .txt file,  it does not actually add the .txt to the file name

2. it names it as note_Sat , i need it to be like:  note_2011/09/17.txt

3. finally it adds the note like below attached;
 
09/17/2011_13:39:40.47.txt
hi tiday  09/17/2011_13:39:40.47.txt

Open in new window


any way to actuall get rid of the time inside the file so it looks like this:
 
hi today

Open in new window


4. and finally, i need it to create it in a specific folder like: C:\DateModified\
  this one creates it in it's self folder wherever it is located

this is nice that it is close to what i need.
thanks for that - it helps me get started.
0
 
LVL 10

Expert Comment

by:acbxyz
ID: 36554718
Problem is, %date% on windows is regional different. Here, it is just dd.mm.yyyy, which is not good when sorting, but works. On the other hand, there are colons in time, which can't be in a filename.

This should work right:
set /p note=Just one line: 
set filename="note_%date%_%time%.txt"

set year=%date:~-4%
set month=%date:~-10,2%
set day=%date:~-7,2%
set second=%time:~-5,2%
set minute=%time:~-8,2%
set hour=%time:~-11,2%

set datetime=%year%%month%%day%_%hour%%minute%%second%
set filename=note_%datetime%.txt

echo %date% at %time% > %filename%
echo %note% >> %filename%

rem if you want to note more, enable this
rem notepad %filename%

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36554787
Here is an alternative approach.  It prompts for a string in a pop-up window using a VBScript dialog box, gets the date/time usign reliable methods within VBScript, and then outputs the file:

See article here of mine on why using %date% is dodgy for dates in batch files:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
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 ("Please enter your note:", "Note entry for " & date)

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

msgbox "Thankyou. I have created file C:\DateModified\" & TheDate & ".txt for you with contents of " & chr(10) & TheNote

Open in new window

WriteNote.vbs
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 36554795
BTW You can run that from a batch file if needed:

@Echo off
cscript //nologo writenote.vbs

Also in the last line, msgbox the word "TheNote" on the end should be "InputNote" to show what was added.  If you want to remove the confirmation just remove the whole msgbox line.

Can also write in just batch file but is much more prone to variations in date formats and the like.

The attached VBS has this corrected and also stops the process if you press Cancel or enter a blank string.
WriteNote.vbs
0
 

Author Closing Comment

by:webdott
ID: 36554960
thanks - that is what i was looking for!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

19 Experts available now in Live!

Get 1:1 Help Now