Jump to content

Need .bat file help


Woogi

Recommended Posts

Well I am working on a project and I am creating a batch file that will export some "Auto complete" data to a text file silently. (Using some of the Nirsoft tools) but I was wondering is there a way to time/date stamp the txt files.

I know 'time' and %date% will show the date but I cant get the created txt file's name to be either the time or date. Below is the syntex from nirsoft's web site, but i was wondering if any one knew what I could use to created:

\stext %date%.txt

If you look at the syntext below you will see that in therory that will create a text file with the actual date as the file name.. it doesn't any ideas?

/stext <Filename> Save all email accounts into a regular text file.

/stab <Filename> Save all email accounts into a tab-delimited text file.

/scomma <Filename> Save all email accounts into a comma-delimited text file.

/stabular <Filename> Save all email accounts into a tabular text file.

/shtml <Filename> Save all email accounts into HTML file.

/sverhtml <Filename> Save all email accounts into HTML file. (vertical)

/sxml <Filename> Save all email accounts into XML file.

Link to comment
Share on other sites

What format does the date / time get output as if you just push it out as the contents of a text file?

If you've got periods, colons, slashes, whatever, in there as a delimiter then the process will fail because they're not valid in file names.

Better to parse the date / time as part of your .bat script, strip out the numbers and create the file name from them in the format yyyymmddhhmm.

So, a date / time of:

04/12/08 19:05 (that's December 4th by the way, I'm in the UK ;) )

Would become: 200812041905

Link to comment
Share on other sites

Well I am working on a project and I am creating a batch file that will export some "Auto complete" data to a text file silently. (Using some of the Nirsoft tools) but I was wondering is there a way to time/date stamp the txt files.

I know 'time' and %date% will show the date but I cant get the created txt file's name to be either the time or date. Below is the syntex from nirsoft's web site, but i was wondering if any one knew what I could use to created:

\stext %date%.txt

If you look at the syntext below you will see that in therory that will create a text file with the actual date as the file name.. it doesn't any ideas?

/stext <Filename> Save all email accounts into a regular text file.

/stab <Filename> Save all email accounts into a tab-delimited text file.

/scomma <Filename> Save all email accounts into a comma-delimited text file.

/stabular <Filename> Save all email accounts into a tabular text file.

/shtml <Filename> Save all email accounts into HTML file.

/sverhtml <Filename> Save all email accounts into HTML file. (vertical)

/sxml <Filename> Save all email accounts into XML file.

You can format the date by using a simple vbScript and passing variables to the batch.

Something like this will give you the date-time in a good aspect:

Set objShell = CreateObject("Wscript.Shell"
Set objEnv = objShell.Environment("PROCESS")
strDate = Year(now()) &amp; Right("0" &amp; Month(now()), 2) &amp; Right("0" &amp; Day(now()), 2)
strTime = Right("0" &amp; Hour(now()), 2) &amp; Right("0" &amp; Minute(now()), 2) &amp; Right("0" &amp; Second(now()), 2)
'Mes/Dia/Año
strFormDate = Right("0" &amp; Month(now()), 2) &amp; "." &amp; Right("0" &amp; Day(now()), 2) &amp; "." &amp; Year(now()) 
'HH:MM:SS
strFormTime = Right("0" &amp; Hour(now()), 2) &amp; "." &amp; Right("0" &amp; Minute(now()), 2) &amp; "." &amp; Right("0" &amp; Second(now()), 2)
objEnv("Date") = strFormDate
objEnv("Time") = strFormTime
strPath = "YOURBATCH.BAT"
objShell.Run strPath, 1, False

you can pass the Date and Time vbs variables to the YOURBATCH.BAT to format the time and date of the loggs:

\stext %computername%[%Date%-%Time%].txt
etc...

:EDIT

The first line must be:

Set objShell = CreateObject("Wscript.Shell")

Link to comment
Share on other sites

DMilton,

I can't get your vbs to work. Maybe it's just that I'm on Vista, but it gives me the regular date and time variables.

I have 'yourbatch.bat' set to 'vars.cmd' and then in there it makes a folder calling the variables '%date%-%time%'.

P.S. I also attempted to call 'vars.cmd' to see if that works too.

I want all the variables set in the 'vars.cmd' and then have all my batch files call 'vars.cmd' (looks neater and more effeicent)

Link to comment
Share on other sites

DMilton,

I can't get your vbs to work.

See for the code in line 1 of the vbs:

Set objShell = CreateObject("Wscript.Shell"

must be (also edited in previous post):

Set objShell = CreateObject("Wscript.Shell")

I have tested it and works ok (almost in WXP)

Maybe it's just that I'm on Vista, but it gives me the regular date and time variables.

I have 'yourbatch.bat' set to 'vars.cmd' and then in there it makes a folder calling the variables '%date%-%time%'.

If you get the variables, you can do whatever you want with them, calling them, batching them, etc.

A proof of concept on how it works (maybe for your mkdir it can be usefull, I have batched the next. Called yourbatch.bat (place it in the same folder as vbs), executethe vbs and it will call the yourbatch.bat file that will create a plain txt containing the results of vars given. Also it creates a new folder with the mentioned structure.

::yourbatch.bat
echo off
echo PROOF OF CONCEPT &gt;proofdatetime.txt
echo var Date: %Date% &gt;&gt;proofdatetime.txt
echo var Time: %Time% &gt;&gt;proofdatetime.txt
echo mkdirectory: %computername%[%Date%-%Time%].txt&gt;&gt;pruebadate.txt
mkdir %computername%[%Date%-%Time%]

P.S. I also attempted to call 'vars.cmd' to see if that works too.

I want all the variables set in the 'vars.cmd' and then have all my batch files call 'vars.cmd' (looks neater and more effeicent)

You can do what you want with the variables. But I think the problem was solved with the mentioned code change.

Link to comment
Share on other sites

I caught the missing parenthesis when I was looking at the code, which was cool because I don't know vb.

Yet another thing I learned...I need to run the vb script. Lol. I was just running the batch file; no wonder it didn't work!

Do you know of a way to get it so that the date and time variables are useable to all my batch files? Or even a way to do it from within a batch file?

Link to comment
Share on other sites

I caught the missing parenthesis when I was looking at the code, which was cool because I don't know vb.

Yet another thing I learned...I need to run the vb script. Lol. I was just running the batch file; no wonder it didn't work!

Do you know of a way to get it so that the date and time variables are useable to all my batch files? Or even a way to do it from within a batch file?

You can use the same vbs with each batch file you need to. There's two ways to do it, one is by executing one instance of the vbs per file... See

strPath = "YOURBATCH.BAT"
objShell.Run strPath, 1, False

The run command doesn't control the results of the running app, in this case, YOURBATCH.BAT.

But if you are attempting to run more batches in a time, you can add each batch from the same script and control their execution by managing the boolean value of the run method, with "True" the program will wait until the finishing of the batch, and with "False" it doesn't wait for finish of the batch, then the program will continue with next line of code.

Example:

Prog1 = "YOURBATCH.BAT" 'This is in the same folder as the vbs
Prog2 = "C:\Windows\hiddenbatch.bat" 'For a batch in other folder
Prog3 = "%SYSTEMROOT%\SYSTEM32\my_personal_trojan.exe 'Using environment variables
objShell.Run Prog1, 1, False ' The batch "yourbatch.bat" is executed in a minimized way and the program doesn't wait
objShell.Run Prog2, 1, True ' The batch "hiddenbatch.bat" is executed and the program wait for the finishing of it
objShell.Run Prog3, 1, False

In all the yourbatch.bat, hiddenbatch.bat or my_personal_trojan.exe, the Date and Time variables can be called from the batch.

If you want to know more on this stuff, see Microsoft Windows 2000 Scripting Guide

For the subject of this post, see This

<_< But if you want, you can format dates in a batch way... If you use in your batch some like this in an formated date as DD/MM/YYYY:

set DateToFile=%date:~6,4%-%date:~3,2%-%date:~0,2%
\stext %computername%[%DateToFile%].txt

As see, you can use %DateToFile% variable for using in your program, it will gives the Date formatted to YYY-MM-DD...

Now you can play with it to do the same with the %TIME% system enviroment variable or simply use SET /? from command line to see examples on how using variables and how deffining them. ;)

Link to comment
Share on other sites

I was really wondering if I could get the 'date' and 'time' variables to be able to be called from the 'yourbatch.bat' instead of adding to the vbs.

Haven't tried yet, but in 'yourbatch.bat' I'll put something like...

set "date=%date%"

set "time=%time%"

This would let me use 'anotherbatch.bat' to use the date and time variables...

call 'yourbatch.bat'

echo %date%

echo %time%

Get what I mean? Anyway, thanks for all the info! Very useful.

Link to comment
Share on other sites

Impressive voodoo.

:P

I was really wondering if I could get the 'date' and 'time' variables to be able to be called from the 'yourbatch.bat' instead of adding to the vbs.

Haven't tried yet, but in 'yourbatch.bat' I'll put something like...

set "date=%date%"

set "time=%time%"

This would let me use 'anotherbatch.bat' to use the date and time variables...

call 'yourbatch.bat'

echo %date%

echo %time%

Get what I mean? Anyway, thanks for all the info! Very useful.

You doesn't need to define the variables twice, if you CALL ANOTHERBATCH.BAT from yourbatch.bat, the variables given by the vbs will work perfectly. It's because when you're calling a batch program from other, all the defined variables exists.

But if you deffinitely want not to use a vbs launcher to give the variables to the batch, you can aply the batching way I show you to format the date and time.

Link to comment
Share on other sites

Here's what I use in some of my batch files when I need to organize by date.

%date:~4,2%%date:~7,2%%date:~10,4%

This will give you mmddyyyy format.

Use in your batch file where you want the date to be put.

Examples

echo %date:~4,2%%date:~7,2%%date:~10,4%

md %date:~4,2%%date:~7,2%%date:~10,4%

cd %date:~4,2%%date:~7,2%%date:~10,4%

copy file.txt %date:~4,2%%date:~7,2%%date:~10,4%.txt

------------------------------------------------

For time you can use either one of these two.

%time:~0,2%%time:~3,2%

This gives you hhmm format. Example 1531

%time:~0,5%

This gives you hh:mm format. Example 15:31

------------------------------------------------

If you haven't figured it out already by looking at the command above; I'll explain.

Lets look at dos command "echo %date%". The result in dos is "Sun 12/14/2008". We want to strip some of the data out to use in our batch. My example is "echo %date:~4,2%%date:~7,2%%date:~10,4%" and the result is "12142008".

Here's how it works. "%date" part calls the variable we want to use, ":~4" part tells it the character position to start reading after, ",2" tells it how many characters to read, and the final "%" closes the variable. So, you get the 12 part of the result.

We continue reading the example... now less broke down. "%date:~7,2%" reads character positions 8 and 9 and gives us the "14". "%date:~10,4%" reads character positions 11 thru 14 and gives us the "2008".

Have fun with this, it gives you a LOT of options and control as it will work with any variable to strip out just what you need. You can also rearrange the date. Example: %date:~7,2%%date:~4,2%%date:~10,4% would give you ddmmyyyy.

Have a Great Day,

Smoke_007

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...