Solved

batch file to create a .txt file

Posted on 2011-09-17
6
477 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

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

Title # Comments Views Activity
Creating An Intelligent Dropdown 8 46
VMWare Calculate number of processors 10 92
Need help Creating PowerShell Script 5 64
Drive mapping problem 7 41
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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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