xcopy & ping help

Discussion in 'Software' started by almulder, Sep 19, 2011.

  1. almulder

    almulder Private E-2

    Ok hope you guys can help me out.

    Below is my code

    Everything work. (Yay)

    But i wanted to see if i could output error code results to an "errorlog.txt" file

    First if ping fails add "Date, Time, Connection Failed to Albert's PC" to errorlog.txt

    next if any file fails to copy add "Date, Time, File Name here, Reason Here" to errorlog.txt

    I would need this to be a continues error log file. Never overwriting anything.

    lastly I would need some code to echo back onto the screen if any error was found. "Errors found see errorlog.txt for more info."

    I have about 10 computers i have in the list. below is only one of the computers

    Any thoughts?

    Code:
    echo. Connecting to Albert's PC...
    PING 192.168.1.99 -n 1| FIND "bytes=" > NUL
    IF ERRORLEVEL 1 ECHO.   Connection failed... && ECHO.     Skipping Albert's PC... && ECHO. && GOTO :EOF
    echo.   Connection established...
    echo.
    echo.     Copying started...
    echo.
    XCOPY  *.* "\\192.168.1.99\C$\Users\Albert\Desktop\Reports\" /EXCLUDE:copyfiles.cfg /e/y/z/v/k
    echo.
    echo. Copying report files to Albert's PC Complete...
    
     
  2. Mada_Milty

    Mada_Milty MajorGeek

    You're so close!

    You're already using the output redirection operator (>) to output to NUL

    If we tweak that slightly, and use the append redirection operator (>>) and point it to a file, that should keep a continuous log for us.

    For example, you have:

    Code:
    IF ERRORLEVEL 1 ECHO.   Connection failed... && ECHO.     Skipping Albert's PC... && ECHO. && GOTO :EOF
    If we changed that to:

    Code:
    IF ERRORLEVEL 1 ECHO.   Connection failed... Skipping Albert's PC... [B]>> C:\LOG.TXT[/B] && GOTO :EOF
    That would append your error message to the file C:\LOG.TXT. Of course, you can modify the path to store the file where you like, but hopefully, that's a step in the right direction?
     
  3. almulder

    almulder Private E-2

    Ok that will work for half of it, but now how do i do the same thing but for the files I'm copying?

    Code:
    XCOPY  *.* "\\192.168.1.99\C$\Users\Albert\Desktop\Reports\" /EXCLUDE:copyfiles.cfg /e/y/z/v/k
    
    PS. just tested the code. Works great. Is there a way to add date and time to the same line?
     
    Last edited: Sep 19, 2011
  4. Mada_Milty

    Mada_Milty MajorGeek

    The documentation on xcopy may help.

    Looks like xcopy has a few error levels:

    Exit code Description
    0 Files were copied without error.
    1 No files were found to copy.
    2 The user pressed CTRL+C to terminate xcopy.
    4 Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
    5 Disk write error occurred.

    So you could write out error messages like:

    Code:
    if errorlevel 4 echo "Insufficient Memory" >> C:\LOG.TXT
    if errorlevel 5 echo "Disc Error" >> C:\LOG.TXT
    
     
  5. Mada_Milty

    Mada_Milty MajorGeek

    Oh for sure:

    Code:
    echo %DATE% %TIME% "ERROR MESSAGE HERE" >> C:\LOG.TXT
     
  6. almulder

    almulder Private E-2


    Is there anyway to output the specific file that errored out? I could be copying up to 100 files at a time. and it would be a pain to locate what failed. (Sometimes they are being accessed at the time.)
     
  7. Mada_Milty

    Mada_Milty MajorGeek

    Hmmm.... Not sure on that one. I may need to defer to another geek for that answer.

    A possible solution would be to use the /f switch on the xcopy command, and append it's output to file, so you would have a list of file names UP TO the file that errored out. (I do believe it copies in alphabetical order)

    ex:

    Code:
    xcopy "c:\sourcedir" "c:\destinationdir" /f >> c:\LOG.TXT
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds