Making log file in batch after a MOVE operation

半腔热情 提交于 2019-12-24 10:48:31

问题


I just need some help with creating a log file for a batch script I'm running at the moment. As I move files from 1 server to another it would be handy to know when things have been moved over.

At the moment I've tried a few things like:

if %errorlevel% GTR 0 (echo MOVE FAILURE>Logs\log.txt) else (echo MOVE SUCCESSFUL>Logs\log.txt)

But this just gets to be sloppy code, as I would have to put it after every line, and it doesn't tell me the name of the file I have just moved.

Here is my code at the moment:

@Echo OFF

cd C:\Test

MOVE "Home\*.*" "Work"

Echo COMPLETE
Pause

Of course there's more lines, about another 20, but for the sake of testing I just have the one folder, and the folder names are just placebos.

Now I can use the default return screen by the Move Command

C:\Test\Home\Test.txt
    1 file(s) moved.

But I don't know how to move that output into a simple .txt file that I can just keep a record off.

By the way if there a better way or better program to write it in let me know, and I'll investigate!


回答1:


try this:

@echo off&setlocal
set "fname=" 
for %%i in (*) do set "fname=%%~i"&call:process
goto:eof

:process
MOVE "%fname%" "test"
if %errorlevel% neq 0 (echo MOVE "%fname%" FAILURE) else echo MOVE "%fname%" SUCCESSFUL
goto:eof



回答2:


Courtesy of dbenham : You should investigate the ROBOCOPY command. It has options to move files, and it can give a complete log of what was done.

robocopy "Home" "Office" /MOV /NJH /NP /NS /NC /LOG+:"Desktop\Log-%date:/=%-%time:~0,2%;%time:~3,2%.txt"

is what I've used! Works fine, not perfect but give me oversight into what has been moved!



来源:https://stackoverflow.com/questions/17062367/making-log-file-in-batch-after-a-move-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!