?
Solved

How to add date and time as a file name?

Posted on 2009-07-06
5
Medium Priority
?
1,230 Views
Last Modified: 2013-12-28
I would like to send a command-line output to a file named with the current date and time.
I tried this but it did not worked:

net view >> C:\Temp\%date%%time.txt

How can I do it?
0
Comment
Question by:Carlos_Felipe
[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
5 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24787681
Replace the / and : with nothing.
net view >> "C:\Temp\%date:/=%%time::=%.txt"

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24787692
Basically removing those invalid filename characters. You could replace with _ or - if you wish.
net view >> "C:\Temp\%date:/=_%%time::=-%.txt"

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 24787880
If you want to keep the files, the format of %date% is no good. It MIGHT contain the weekday, and mm/dd/yyyy is something strange to look at and sort for. If this is for temporary issues, keep the simple approachs shown above.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 24792265
Carlos
The issue was certainly either of the explanations given above, ie. the / characters in the date that cannot be used in file names, or unwanted spaces and days of the week in the DATE output, but aside from that you need to be careful to check your code for things like missing % in your variables.  In your code you missed the closing % off the TIME variable:

C:\Temp\%date%%time.txt
                                     ^
should have been:

C:\Temp\%date%%time%.txt

0
 
LVL 14

Accepted Solution

by:
Robin CM earned 1000 total points
ID: 24824029
depending on how your date/time is displayed, here's a batch file that might be handy
my date shows as 10/07/2009
my time shows as 16:04

@echo off
 
for /f "tokens=1,2,3 delims=/" %%i in ('date /t') do (
set day=%%i
set month=%%j
set year=%%k
)
 
for /f "tokens=1 delims= " %%i in ("%year%") do (
set year=%%i
)
 
for /f "tokens=1,2 delims=:" %%i in ('time /t') do (
set hour=%%i
set minute=%%j
)
 
echo %year%-%month%-%day%-%hour%-%minute%

Open in new window

0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Suggested Courses

800 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