# Weekly log for batch



## Lord Impaler (Oct 26, 2002)

Now that I have my batch setup I've been looking at creating logs for the batch. A simple way is to use


```
>> c:\log.txt
```
My problem is I want a weekly log or even a daily log, then have a new log created for the following day or week. I've read the following thread http://forums.techguy.org/dos-other/685747-write-log-output-batch-file.html but it creates a blank log when I use the code.

This batch will be run every hour and this is what i have


```
title WOS
setlocal
set LogPath=C:\Documents and Settings\toddz\Desktop\test\Log
set LogFileExt=.txt
set LogFileName=Daily Work Order%LogFileExt%
set MyLogFile=%date%
set MyLogFile=%MyLogFile:/=-%
set MyLogFile=%LogPath%%MyLogFile%_%LogFileName%
If NOT Exist "%MyLogFile%" goto:noseparator
Echo.>>"%MyLogFile%"
Echo.===================>>"%MyLogFile%"
:noseparator
echo.%Date% >>"%MyLogFile%"
echo.%Time% >>"%MyLogFile%"
:startbatch
pushd "C:\Documents and Settings\toddz\Desktop"
for %%j in (*) do (
	for /f "tokens=* delims=" %%G in ('dir /ad /b /s "C:\Documents and Settings\toddz\Desktop\*%%~nj"') do move "%%~j" "%%~G"
)

pause
popd
```


----------



## Squashman (Apr 4, 2003)

Show me the output of *echo %date%* from the cmd prompt.


----------



## Lord Impaler (Oct 26, 2002)

Mon 02/06/2012

All the log has is:

Mon 02/06/2012 
14:13:40.47


----------



## Squashman (Apr 4, 2003)

Because that is all you are putting into it. 
None of your other code is using the >> symbols to redirect any of the output to the log file. Not that there is any to redirect. You will need to additional ECHO statements to redirect the information you want to the log file.
What do you want in it and how do you want it too look?

So is your log file file name:*Mon 02062012.txt*?
I personally would use a YYYYMMDD format for the file name.


----------



## Lord Impaler (Oct 26, 2002)

It is obnoxiously long, "LogMon 02-06-2012_Daily Work Order". So would I do



> for %%j in (*) do (
> for /f "tokens=* delims=" %%G in ('dir /ad /b /s "C:\Documents and Settings\toddz\Desktop\*%%~nj"') do move "%%~j" "%%~G"
> ) >> c:\Documents and Settings\toddz\Desktop\test\Log


----------



## Squashman (Apr 4, 2003)

Squashman said:


> You will need additional ECHO statements to redirect the information you want to the log file.


You might want to try and give my advice a try. At least make an attempt.


----------



## Lord Impaler (Oct 26, 2002)

> title WOS
> 
> pushd "C:\Documents and Settings\toddz\Desktop"
> for %%j in (*) do (
> ...


I am at a lose on how to get the date in the file name and making the log daily or weekly.


----------



## Squashman (Apr 4, 2003)

Now you are confusing me.
I asked this.


Squashman said:


> So is your log file file name:*Mon 02062012.txt*?


You replied with this.


Lord Impaler said:


> It is obnoxiously long, "LogMon 02-06-2012_Daily Work Order". So would I do


And now you are contradicting yourself.


Lord Impaler said:


> I am at a lose on how to get the date in the file name and making the log daily or weekly.


----------



## Lord Impaler (Oct 26, 2002)

I have successfully confused you and myself. I win at the internet.....



> I've read the following thread http://forums.techguy.org/dos-other/...atch-file.html but it creates a blank log when I use the code.


The last string of code I posted works, and gives me a date in the log itself, but the code I originally used did not work, although it did change my file name. The thread I read has added to my confusion instead of helping. I've been reading through the commands and right around here


> (use set MyLogFile=%date:~4% instead to remove the day of the week)
> set MyLogFile=%date%
> set MyLogFile=%MyLogFile:/=-%


is where I get lost.


----------



## Squashman (Apr 4, 2003)

I don't think you have really understood any of the code in all the batch files we have helped you with so far. You are just kind of taking them and using them which really doesn't help you at all. I only go so far as giving someone a fish. Eventually they have to learn to fish!

You need to learn how to do string parsing and string substitution.
What you are attempting to do is String Substitution.

So this code and takes whatever the variable DATE is assigned and assigns it to the Variable MyLogFile.

```
set MyLogFile=%date%
```
It then uses string substitution to remove the forwarded slashes and makes them hyphens

```
set MyLogFile=%MyLogFile:/=-%
```
You don't need two SET statements. You can do that all in one SET statement.Whatever character is before the equals sign will get substituted with the character that is after the equals sign.

```
set MyLogFile=%date:/=-%
```
But as I said earlier, I wouldn't make my File Name in a *DAY MM-DD-YYYY* format. I personally would not put the day of week in the file name. It also makes it easier to look at the files in a sequential order if you make the File Names in a YYYYMMDD format. In order to do that you need to do string parsing.
If I wanted to just set the YEAR to a variable you need to tell it what the starting position is and the length of the string you want to parse. But you have to remember that batch like other programming languages uses ZERO as the starting position.
So if todays date is: Tue 02/07/2012
You need to get the 11 position but because batch starts at Zero you need the 10th position.

```
set Year=%date:~10,4%
```
You can play around with all these commands at the cmd prompt and echo the variable you just SET to see what it is doing.


----------



## Lord Impaler (Oct 26, 2002)

You are 100% correct. I've asked for a site or guide to batch files. DOS shell only helps so much. Examples help a lot. Do you have a reference guide, or even a book to refer to? The sites I have googled do not seem to help.


----------



## Squashman (Apr 4, 2003)

The sites I reference the most are:
http://www.robvanderwoude.com/
http://www.dostips.com/


----------



## Lord Impaler (Oct 26, 2002)

Thank you for the references. Once I have variables set let's use the beginning code as an example how do I redirect the commands to be logged? The set commands are creating a log already so I do not see how I can insert the move commands into the same log. I've been using ">>(everything under the sun)", but with no success.


----------



## Squashman (Apr 4, 2003)

Need to see the current code you are trying to use.


----------



## Lord Impaler (Oct 26, 2002)

```
title WOS

setlocal
set LogPath=C:\Documents and Settings\toddz\Desktop\test\
set LogFileExt=.txt
set LogFileName=Log%LogFileExt%
set MyLogFile=%date:/=-%
set MyLogFile=%LogPath%%MyLogFile%_%LogFileName%
If NOT Exist "%MyLogFile%" goto:noseparator
Echo.>>"%MyLogFile%"
Echo.===================>>"%MyLogFile%"
:noseparator
echo.%Date% >>"%MyLogFile%"
echo.%Time% >>"%MyLogFile%"
:startbatch

pushd "C:\Documents and Settings\toddz\Desktop"
for %%j in (*) do (
	for /f "tokens=* delims=" %%G in ('dir /ad /b /s "C:\Documents and Settings\toddz\Desktop\*%%~nj"') do move "%%~j" "%%~G"
) >>??????????

pause
popd
```
I haven't been able to successfully redirect the move commands to the log, because I am redirecting them to the wrong location.


----------



## Squashman (Apr 4, 2003)

Lord Impaler said:


> ```
> title WOS
> 
> setlocal
> ...


Let me ask you this. When you use the MOVE command to move a file from one directory to another does it output any verbose information to the screen? Open up a command prompt and manually move a single file to another directory and see what happens.

And this is the 3rd time I have said this.


Squashman said:


> You will need additional ECHO statements to redirect the information you want to the log file.


----------



## Lord Impaler (Oct 26, 2002)

I see what I did wrong. I was not putting in the quotations with %MyLogFile%, after the move command I added

```
>>"%MyLogFile%"
```
 and it works great.


----------



## Squashman (Apr 4, 2003)

I guess I would rather not see this get echoed to my log file.


> 1 file(s) moved


Nor does your script tell you if a file was not moved. If a file exists in your dropbox and the corresponding job folder does not exist in the destination it will not tell you if the file was not moved.


----------

