Solved

Simple batch file... I think

Posted on 2014-01-31
20
545 Views
Last Modified: 2014-02-21
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
Comment
Question by:jbyrd1981
  • 7
  • 5
  • 4
  • +1
20 Comments
 
LVL 10

Expert Comment

by:pfrancois
ID: 39825259
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
 
LVL 26

Expert Comment

by:pony10us
ID: 39825260
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
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39825286
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
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39825292
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
 
LVL 10

Expert Comment

by:pfrancois
ID: 39825352
@jbyrd1981: Can you show the forfile command you are using?
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39825376
Its is above your post.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39825394
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
 
LVL 10

Expert Comment

by:pfrancois
ID: 39825434
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
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39825502
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
 
LVL 26

Expert Comment

by:pony10us
ID: 39825581
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39825758
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
 
LVL 10

Expert Comment

by:pfrancois
ID: 39825996
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39826270
No, that will remove the whole directory after only the first file is copied.

~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39829693
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
 
LVL 1

Author Closing Comment

by:jbyrd1981
ID: 39837094
Thanks Bill! I think this is exactly what I was looking for. :)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39837478
Welcome, glad to help, thanks for the feedback.

~bp
0
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39877805
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39877868
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
 
LVL 1

Author Comment

by:jbyrd1981
ID: 39878370
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39878522
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now