Saving A File With the Date attached

  • Thread starter Thread starter Travis
  • Start date Start date
T

Travis

I was wondering if there is a way to automate date
insertion on filenames for programs that run scheduled.

For example what if I have audit software running and
everynight it saves a text file to the hard drive. Since
it has the same name in the save as area then it
continuously overwrites the old one. What if I want to
keep the old ones though by date?

Something that would give me the below.

c:\audit\21Sep03

Thank You in advance.
 
-----Original Message-----


The easiest way would be to check that your audit software doesn't have
some setting in it itself to control the name of the file so that it
doesn't automatically over-write itself. Seems like that would be
reasonable to expect such a thing in audit software.

The next easiest way would be to write a small program that would be
scheduled to run after the audit program to copy the created file into a
new file which, say, incorporates the date in it's file name, e.g.
21SEP03.TEXT or to enable correct file sorting in Explorer, make it
030921.TXT. This program could be written as BAT file using normal
commands, or you could use a progamming langauge like Visual Basic,
Perl, or Python. While this is very simple to do, it does require a bit
of knowledge and experience in programming.

.

Precisely this is not pertaining to auditing software,
but used as an example. I had a feeling I might have to
write something to accomplish what I needed. I was
really hoping that Microsoft had provided variable
support when using Save as: to add information such as
the date to the end of the filename. Thanks for your
help.
 
cmd /c echo. |time>myfile.txt
for /f "tokens=5" %A In (myfile.txt) Do Copy %1 %A.txt

Note. If you use : as a hour seperator it won't work. So yopu'll need to go through it again with for /f to extract the time with out a colon.



See for /?

akso note in a batch file %A is %%A, %B is %%B, ,,, . %n is %%n.


EG this batch file copies any file dropped on it with the time as the name (to the hundreds of a second), substituting dashes for colons

echo.|time>c:\myfile.txt
for /f "tokens=5,6,7,8 delims=: " %%A In (c:\myfile.txt) Do copy %1 %%A-%%B-%%C-%%D.txt

If you want to get fancy with paths the For command can also pull out parts of file names. See For /?. But I'd just hardcode the paths.
 
Travis said:
software doesn't have


file so that it


would be


that would be


created file into a


name, e.g.


Explorer, make it


using normal


Visual Basic,


does require a bit



Precisely this is not pertaining to auditing software,
but used as an example. I had a feeling I might have to
write something to accomplish what I needed. I was
really hoping that Microsoft had provided variable
support when using Save as: to add information such as
the date to the end of the filename. Thanks for your
help.

See David Candy's reply where he provides you some code.

Microsoft *does* provide the capability to create a filename by date (or
anything else) and then save the file. This is why what David suggests
works. And even better, if you don't like David's way, there there are
a lot of other ways to accomplish this, all enabled by what Microsoft
provides in XP.
 
XP can do it at the command prompt but not in a windows programs.

There is also %time% that is in NT based systems (not 9x but then neither is those for /f commands). But File - Save As only returns what you type or select to the program, and most programs don't expect environtmental variables, so unless the program expands then it will be treated as text. Note this is the program not windows that decides what to do.
 
Expansion happens automatically with file associations. If explorer see a reg_expand registry data type it calls ExpandEnvironmentStrings. But windows is not in control in a program, the program is, So entering %time% in a File Save As dialog depend entirely on the program what happens. Save As just tells the programs the user typed %time% (which is a legal file name) and if the program doesn't ca;ll an expansoion functions then it will save with that name.

<quote>
I was
really hoping that Microsoft had provided variable
support when using Save as: to add information such as
the date to the end of the filename.
</quote>
 
David said:
Expansion happens automatically with file associations. If explorer see a reg_expand registry data type it calls ExpandEnvironmentStrings. But windows is not in control in a program, the program is, So entering %time% in a File Save As dialog depend entirely on the program what happens. Save As just tells the programs the user typed %time% (which is a legal file name) and if the program doesn't ca;ll an expansoion functions then it will save with that name.

<quote>
I was
really hoping that Microsoft had provided variable
support when using Save as: to add information such as
the date to the end of the filename.
</quote>

Sorry. I read and re-read what you wrote and still didn't realize you
were referring to use of environment variables like %time%. You were
being specific and I was being generic in saying that "a program could
be written". Peace.
 
Back
Top