Solved

batch file to create a .txt file

Posted on 2011-09-17
6
478 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

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

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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