Logging WIndows Batch Script Question

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:\"
=-=-=-=-
mmoyaAsked:
Who is Participating?
 
ReneGeConnect With a Mentor Commented:
Oups

 
@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
SET FileSearch=MM_5a*.*
SET DestFolder=M:\

REM SET SourceFolder=C:\Temp
REM SET FileSearch=*.txt
REM 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

0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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



0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
mmoyaAuthor 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!
0
 
mmoyaAuthor 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
0
 
subhashchyCommented:
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


0
 
ReneGeCommented:
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

0
 
ReneGeCommented:
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.
0
 
ReneGeCommented:
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

0
 
subhashchyCommented:
ReneGe: Just wanna say your script is always impressive and self explanatory .  :). Any tips for me ?
0
 
ReneGeCommented:
Thanks subhashchy ;-)

I just wana help.

Cheers,
Rene
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.