jbyrd1981
asked on
Simple batch file... I think
I am using forfiles in a batch file to move a few files to a network share. Then it will delete the original directory on the local machine. I works great but I need it to skip the RD command if there is an error copying the files, like the server is unavaliable. Can't figure out how to work this in. Thanks in advance!
Are you moving files that have the same name as existing files on the destination? If not then you can do a simple test to see if the files are there after the move.
move c:\test.txt h:\test.txt
IF NOT EXIST H:\test.txt GOTO :failure
IF EXIST H:\test.txt GOTO : Continue
:Continue
REM The file has been successfully copied. You may continue your following commands here.
exit
:failure
REM The file has failed to be copied. The batch file will now quit
exit
ASKER
I have to use forfiles because it will scan down into the directory pulling only the file I tell it without copying the directory structure. If that is successful I need it to delete the directory containing that file on the local source. If it is not successful I need it to leave the source directory.
ASKER
forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs"
@echo
rd "C:\Results\" /S /Q
@jbyrd1981: Can you show the forfile command you are using?
ASKER
Its is above your post.
So you are coping all .xml files? Not moving them as your original post said?
It is working as typed (UNC path included) but you want to introduce error checking?
It is working as typed (UNC path included) but you want to introduce error checking?
Should the following work?
Note the double && after the first command, and the single & further to separate commands.
command1 && command2 means command2 is executed only if command1 gives no error
forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs && set d=%curdir% & cd .. & rd %d%"
Note the double && after the first command, and the single & further to separate commands.
command1 && command2 means command2 is executed only if command1 gives no error
ASKER
Yes. Pony10us. pfrancois It does not seem to remove the directory and I get this input in the cmd window.
1 file(s) copied.
The system cannot find the file specified.
The system cannot find the path specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the path specified.
1 file(s) copied.
The system cannot find the file specified.
The system cannot find the path specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the path specified.
Try this:
I have not tested this code however I did REM out the line to remove the folder.
@echo off
forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs"
IF NOT EXIST \\server\logs\@file GOTO :failure
@echo This is where the folder would be removed if the last file ("@file") is found in the new location
REM rd "C:\Results\" /S /Q
pause
:failure
@echo The last file ("@file") has failed to copy. The batch file will terminate without removing the folder.
pause
exit
I have not tested this code however I did REM out the line to remove the folder.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Would this work?
forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs && rd "C:\Results\" /S /Q"
No, that will remove the whole directory after only the first file is copied.
~bp
~bp
Nice Bill, I had a chance to try my attempt and it didn't work. For some reason I couldn't get it to carry the variable @file through the process. Even if it had, it would only have checked that the last file copied correctly. That wouldn't have accounted for possible errors on copying other files.
ASKER
Thanks Bill! I think this is exactly what I was looking for. :)
Welcome, glad to help, thanks for the feedback.
~bp
~bp
ASKER
Bill... Hate to come back to this. The script is working great for the most part. I have some machines, more are working than not, which are not copying. I am able to take the copy /y command and run it manually in the cmd window. It would appear something is happening on these machines causeing the copy /y to not run in the script. Below is the output from the script run on the failed machine, thoughts?:
Good=0
Bad=1
C:\>setlocal EnableDelayedExpansion
C:\>REM Define files to copy and locations
C:\>set "FromDir=C:\Results"
C:\>set "ToDir=\\srv001\results\Logs"
C:\>set "Filter=*xccdf*.xml"
C:\>REM Reset counters
C:\>set CountGood=0
C:\>set CountBad=0
C:\>REM Process all matching files in the from location, including subfolders
C:\>for /R "C:\\Results" %A in ("*xccdf*.xml") do (
REM For each file, copy to destination, and count any errors
copy /y "%~A" "\\svr001\results\Logs" 1>NUL 2>NUL && (set /a CountGood+=1 ) || (set /a CountBad+=1 )
)
C:\>(
REM For each file, copy to destination, and count any errors
copy /y "C:\\Results\v1.2.0.0\2014-02-21_080005\XML\756103_SCC-3.1_2014-02-21_080005_XCCDF-Windows-7.xml" "\\srv001\results\Logs" 1>NUL 2>NUL && (set /a CountGood+=1 ) || (set /a CountBad+=1 )
)
C:\>REM Echo counts for testing
C:\>echo Good=0
Good=0
C:\>echo Bad=1
Bad=1
C:\>REM Delete the source folder, but only if no errros during the copy
C:\>if 1 EQU 0 (rd /s /q "C:\Results" )
For a temporary test remove this from the COPY line:
>NUL 2>NUL
Then rerun. It should show any error that the copy is generating.
~bp
>NUL 2>NUL
Then rerun. It should show any error that the copy is generating.
~bp
ASKER
Without that entry it copies fine... Would removing that make it remove the results directory even if the file copy was not successful? Thanks!
No, I wouldn't think so. My guess would be whatever error it had last time is no longer present, maybe a file open or in use by some other program? All that chunk does is suppress the output from the COPY command, including any errors. Shouldn't change the execution at all.
~bp
~bp
Open in new window
If you work with directories, use XCOPY instead of COPY and RMDIR (or RD) instead of DEL and adapt the flags accordingly.