?
Solved

Logging WIndows Batch Script Question

Posted on 2011-04-28
11
Medium Priority
?
538 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:\"
=-=-=-=-
0
Comment
Question by:mmoya
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 35484719
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
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 35484734
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
 

Author Comment

by:mmoya
ID: 35485211
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

Author Comment

by:mmoya
ID: 35485320
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
 
LVL 8

Expert Comment

by:subhashchy
ID: 35487193
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35500721
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35500731
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35500786
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
 
LVL 10

Accepted Solution

by:
ReneGe earned 2000 total points
ID: 35500789
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
 
LVL 8

Expert Comment

by:subhashchy
ID: 35501549
ReneGe: Just wanna say your script is always impressive and self explanatory .  :). Any tips for me ?
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35501785
Thanks subhashchy ;-)

I just wana help.

Cheers,
Rene
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question