Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

Create log files inside a directory


Recommended Posts

Hi guys

 

it is me again, I bet you missed me :)
 

I would like to create a log file for the output of my commands inside a certain directory.

Here is my code:

mkdir "LogFiles\"

@echo\  PING www.google.com^^ > LogFiles\command.log

@echo\ IPCONFIG /ALL^^ >> LogFiles\command.log

 

but this doesn't work. Could you please help me on this?

Link to post
Share on other sites

I would like to create a log file for the output of my commands inside a certain directory.

Here is my code:

mkdir "LogFiles\"

@echo\  PING www.google.com^^ > LogFiles\command.log

@echo\ IPCONFIG /ALL^^ >> LogFiles\command.log

 

but this doesn't work. Could you please help me on this?

 

Why are you using ^^ before redirection symbols?

Wouldn't you want to make sure that you do not overwrite an existing "command.log"?

What is that you want to accomplish, i.e. what do you actually want to have written to the "command.log" file?

Are you sure you want to use "relative" paths such as the ones you used?

WHAT (EXACTLY) does not work?

 

 

Apart from the above, maybe what you really want/need is a "TEE" command. :unsure:

http://code.google.com/p/wintee/

http://code.google.com/p/wintee/wiki/FAQ

 

See if this:

http://www.robvanderwoude.com/battech_redirection.php

helps you.

 

The code you posted works fine, in the sense that it produces:

  1. a logfiles directory
  2. a command.log file in it with contents:
PING www.google.com^IPCONFIG /ALL^

Is this not what you expected?

 

jaclaz

Edited by jaclaz
Link to post
Share on other sites

Perhaps if your open to suggestion about the logging format, then perhaps the structure of a database might be better for saving multiple amounts of data. The example I show is using a 3rd party database tool known as SQLite and is used for local file based databases for applications and scripts. If you do not know SQL, then perhaps it may give you a reason to learn something new.

 

This script executes the commands, saves the data to a sql file and then it is read in by sqlite3.exe to be inserted into the database. It will then display you some results by accessing the database. 1 table named logs with 2 columns named name and data. Both columns are set as type text.

@echo offsetlocal enabledelayedexpansion@set prompt=::$G$S:: Just for clean, repetitive testing. Can be removed.if exist logs.db del logs.db:: Require::: sqlite-shell-win32-x86-3080701.zip (version number changes. File inside zip is sqlite3.exe):: download from http://www.sqlite.org/download.html:: Create the database if not exist and create the logs table.if not exist logs.db (	sqlite3 logs.db "create table logs (name TEXT, data TEXT);"):: Add begin transaction.echo begin;> tmp.sql:: Add insert values. Use 0 for 3rd parameter to not execute the 2nd parameter.call :addValues "TestName1"   "echo Hello World"call :addValues "TestName2"   "Hello Sun"           "0"call :addValues "DirOutput"   "dir /b"call :addValues "PingGoogle"  "ping www.google.com"call :addValues "IPConfigAll" "ipconfig /all":: Add commit transaction.echo commit;>> tmp.sql:: Read the sql transaction file into the database.if exist logs.db if exist tmp.sql (	sqlite3 -cmd ".read tmp.sql" logs.db ""):::: Test database access.if exist logs.db (	:: Display data value of TestName1 from logs.db and store in a variable.	echo 1. Select data from logs where name = 'TestName1';	for /f "usebackq delims=" %%A in (		`sqlite3 logs.db "select data from logs where name = 'TestName1';"`	) do set "data=%%A"	echo !data!	echo.		:: Display all values of TestName2 from logs.db.	echo 2. Select * from logs where name = 'TestName2';	sqlite3 -cmd ".mode line" logs.db "select * from logs where name = 'TestName2';"	pause		:: Display all values from logs.db.	echo 3. Select * from logs;	sqlite3 -cmd ".mode line" logs.db "select * from logs;"	pause)endlocalgoto :eof:addValues	setlocal enabledelayedexpansion	:: 1st value. Idea of echo without newline from http://stackoverflow.com/questions/7105433/windows-batch-echo-without-new-line	set "line=%~1"	set "reply="	@echo | set /p "reply=insert into logs values ^('!line:'=''!','">> tmp.sql	:: 2nd value. Executes the command and echos each line to a sql file.	set "line="	if /i "%~3" == "0" (		:: Just the text with no newline.		echo | set /p "reply=%~2">> tmp.sql	) else (		:: Execute and echo the output.		for /f "delims=" %%A in ('%~2') do @(			set "line=%%A"			set "line=!line:'=''!"			echo !line!		)	) >> tmp.sql	echo '^);>> tmp.sql	endlocalgoto :eof

I will show only the output of the 1st 2 display outputs as the 3rd which displays all values is long, due to IPConfig /all etc.

1. Select data from logs where name = 'TestName1';Hello World2. Select * from logs where name = 'TestName2'; name = TestName2 data = Hello Sun

Ensure sqlite3.exe in the current directory or %path% to test.

Link to post
Share on other sites

Thanks jaclaz.

 

It is probably a good torch. I probably need some good eyes rather then a good torch. The price tag is probably bumped up a bit high for my liking. Makes me think of a keyboard in comparison with complexity. Many factors determine what I may buy and what I will not.

 

As for my code, I am just getting started :P . Its 100% free so someone can take it or leave it. For an limited time, I will offer 50% off. Thats right, a huge 50% off. Get it while it lasts :sneaky: .

Link to post
Share on other sites

As for my code, I am just getting started  :P . Its 100% free so someone can take it or leave it. For an limited time, I will offer 50% off. Thats right, a huge 50% off. Get it while it lasts :sneaky: .

 

No special discount for resellers? :w00t:

 

;)

 

jaclaz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...