Solved

batch file to create a .txt file

Posted on 2011-09-17
6
479 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
[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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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