We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Logging WIndows Batch Script Question

mmoya
mmoya asked
on
Medium Priority
550 Views
Last Modified: 2012-06-27
Hello, this is something simple. I need to move certain files and would like to create a logging to log successful and not-succesful move to a file with datetimestamp. Please help...

=-=-=-=-
@echo off
move /Y "C:\Users\MM_5a*.*" "M:\"
=-=-=-=-
Comment
Watch Question

Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
I'm assuming your existing code is otherwise correct.

Put everything into a single log file
@echo off
echo Move started at %date% %time%>>move.log
move /Y "C:\Users\MM_5a*.*" "M:\" >>move.log 2>>&1
echo Move completed at %date% %time%>>move.log

Open in new window


Put everything into two log files - errors in one, successes in another.
@echo off
move /Y "C:\Users\MM_5a*.*" "M:\" >move.success.log 2>move.failure.log

Open in new window



Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
Note:

> will delete an existing file and start clean
>> will APPEND to an existing file

So if you replaced the >> in the first example with a > then the existing log file "move.log" would be overwritten.  Likewise, if you replaced the > with >> in the second example, the existing log file would be appended to instead of overwritten.

Author

Commented:
I want to append because I need tor reference all the dates that it will capture. So, which code should I use the first one or the second one? Thanks!

Author

Commented:
I tested both and Ilike the first one better..



@echo off
echo Move started at %date% %time%>>move.log
move /Y "C:\Users\MM_5a*.*" "M:\" >>move.log 2>>&1
echo Move completed at %date% %time%>>move.log
Not sure if you are asking or telling ... Seems Leew is away

As mentioned by leew, this should append the log file as below....

move.log
move started at Fri 04/29/2011  2:19:52.13
C:\users\MM_5a**fliename
C:\users\MM_5a**fliename   (All the flie names will be listed) 
        2 file(s) moved.
Move completed at  Fri 04/29/2011  2:21:51:14

Open in new window


Commented:
What about this

 
@ECHO OFF

SETLOCAL enabledelayedexpansion

REM THE LOG FILE WILL HAVE THE SAME NAME AS THE BATCH FILE BUT WITH THE .LOG EXTENSION
SET LogFile=%~n0.log
IF EXIST %LogFile% DEL %LogFile%

SET SourceFolder=C:\Users\MM_5a*.*
SET DestFolder=M:\

IF NOT EXIST "%DestFolder%" MD "%DestFolder%" || (ECHO DESTINATION INVALID & PAUSE & EXIT)

FOR /F "delims=" %%A IN ('DIR /b /s "%SourceFolder%"') DO (
	ECHO MOVING: %%~fA
	MOVE /Y "%%~fA" "%DestFolder%"
	ECHO ErrorCode:[!errorlevel!] %%~tA "%%~fA">>%LogFile%
)
ECHO.
ECHO DONE
ECHO.
PAUSE
EXIT

Open in new window

Commented:
My only concern here is that if it finds a "MM_5a*.*" file within a different user profile and with the same name, it will erase the one previously moved.

Commented:
This will resolve my previous concern by adding the user profile name at the begining of the moved file.

Enjoy,
Rene

 
@ECHO OFF

SETLOCAL enabledelayedexpansion

REM THE LOG FILE WILL HAVE THE SAME NAME AS THE BATCH FILE BUT WITH THE .LOG EXTENSION
SET LogFile=%~n0.log
IF EXIST %LogFile% DEL %LogFile%

REM SET SourceFolder=C:\Users
REM SET FileSearch=MM_5a*.*
REM SET DestFolder=M:\

SET SourceFolder=C:\Temp
SET FileSearch=*.txt
SET DestFolder=C:\TEMP2

IF NOT EXIST "%DestFolder%" MD "%DestFolder%" || (ECHO DESTINATION INVALID & PAUSE & EXIT)

FOR /F "delims=" %%A IN ('DIR /B /ad "%SourceFolder%"') DO (
	ECHO USER PROFILE=%%A
	FOR /F "delims=" %%B IN ('DIR /b /s "%SourceFolder%\%FileSearch%"') DO (
		ECHO MOVING: %%~fB
		MOVE /Y "%%~fB" "%DestFolder%\%%A_%%~nxB"
		ECHO ErrorCode:[!errorlevel!] %%~tB "%%~fB">>%LogFile%
	)
)

ECHO.
ECHO DONE
ECHO.
PAUSE
EXIT

Open in new window

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
ReneGe: Just wanna say your script is always impressive and self explanatory .  :). Any tips for me ?

Commented:
Thanks subhashchy ;-)

I just wana help.

Cheers,
Rene
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.