• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

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!
0
jbyrd1981
Asked:
jbyrd1981
  • 7
  • 5
  • 4
  • +1
1 Solution
 
Pierre FrançoisSenior consultantCommented:
After copying a file to its destination, you can test that no error occured and then delete the source file.
COPY /Y source destination
IF %ERRORLEVEL% EQ 0 DEL source

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

Open in new window

0
 
jbyrd1981Author Commented:
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jbyrd1981Author Commented:
forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs" 

@echo

rd "C:\Results\" /S /Q

Open in new window

0
 
Pierre FrançoisSenior consultantCommented:
@jbyrd1981: Can you show the forfile command you are using?
0
 
jbyrd1981Author Commented:
Its is above your post.
0
 
pony10usCommented:
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?
0
 
Pierre FrançoisSenior consultantCommented:
Should the following work?

forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs && set d=%curdir% & cd .. & rd %d%" 

Open in new window


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
0
 
jbyrd1981Author Commented:
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.
0
 
pony10usCommented:
Try this:

@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 

Open in new window


I have not tested this code however I did REM out the line to remove the folder.
0
 
Bill PrewCommented:
Okay, given the need to trap for any errors during the copy, I don't think FORFILES will do the job. And you really don't need to use it in this case, plain DOS commands can get the job done, and trap errors.

Below is a proposal for this using the FOR /R command that finds all matching files in a directory structure including subfolders.  As we find each file, we COPY it to the destination folder, and we can trap success or failure of that command.  We keep a counter of the good copies, and the bad copies.  And then after all files are processed, we only remove the source folder if there were no errors.

@echo off
setlocal EnableDelayedExpansion

REM Define files to copy and locations
set "FromDir=C:\Results"
set "ToDir=\\server\Logs"
set "Filter=*.xml"

REM Reset counters
set CountGood=0
set CountBad=0

REM Process all matching files in the from location, including subfolders
for /r "%FromDir%" %%A in ("%Filter%") do (
  REM For each file, copy to destination, and count any errors
  copy /y "%%~A" "%ToDir%" >NUL 2>NUL && (set /a CountGood+=1) || (set /a CountBad+=1)
)

REM Echo counts for testing
echo Good=%CountGood%
echo Bad=%CountBad%

REM Delete the source folder, but only if no errros during the copy
if %CountBad% EQU 0 (
  rd /s /q "%FromDir%"
)

Open in new window

~bp
0
 
Pierre FrançoisSenior consultantCommented:
Would this work?

forfiles /p "C:\Results" /m *.xml /s /c "cmd /c copy @path \\server\Logs && rd "C:\Results\" /S /Q" 

Open in new window

0
 
Bill PrewCommented:
No, that will remove the whole directory after only the first file is copied.

~bp
0
 
pony10usCommented:
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.
0
 
jbyrd1981Author Commented:
Thanks Bill! I think this is exactly what I was looking for. :)
0
 
Bill PrewCommented:
Welcome, glad to help, thanks for the feedback.

~bp
0
 
jbyrd1981Author Commented:
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" ) 

Open in new window

0
 
Bill PrewCommented:
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
0
 
jbyrd1981Author Commented:
Without that entry it copies fine... Would removing that make it remove the results directory even if the file copy was not successful? Thanks!
0
 
Bill PrewCommented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now