1.

Solve : Close a file from within a called batch file??

Answer»

Is there a way to explicitly close a file from within a called BATCH file?

I have two batch batch files, AAA and BBB (see below). Batch file AAA repeatedly calls batch file BBB, passing two params. Param1 varies (it is a SEARCH param) and param2 is constant (it is the name of a file to which batch file BBB appends the results of its search). Batch file BBB searches *.txt for %1 and writes the result to temp.txt. Them it appends temp.txt to %2. Finally, it deletes temp.txt

My problem is that if batch file AAA has dozens of invocations, then once its gets cranking, I occasionally receive the error message ""The process cannot ACCESS the file because it is being used by another process.". I presume this is because the %2 file is being written to by a subsequent invocation of batch file BBB, but it is still danglng open from a PRIOR invocation of batch file BBB.

Is there a way to explicitly close a file from within a called batch file? In this case, from within BBB


batch file AAA.bat
=================
del file_name > NUL
call BBB A file_name
call BBB B file_name
call BBB C file_name
.
.
.
call BBB Z file_name


batch file BBB
===============
FINDSTR %1 *.txt > temp.txt
type temp.txt >> %2
del temp.txt > NUL
The files should be closed by the process as soon as it is finished being read from / written to. There are probably ways to force it to close, but (1) that would probably be a bad idea unless you want to chance corrupting your date, and (2) by the time we forced the file to close, it would probably be open by the next process anyway. I think it is more likely just a "random" timing issue.

Is there a reason you don't just combine the 2 batch files into a single batch file that does everything? That would probably serialize the processes better and may fix your problem.I don't know how close the example you posted is to your actual code, but this is a replacement for what you posted to combine the 2 files into a single batch file:

Code: [Select]if exist file_name del file_name >NUL
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do findstr %%a *.txt >>file_nameGuruGary

In reality, batch file BBB is large, so the "combine" suggestion would be problematic. However, the possible solution using "For" looks good - I'm playing around with it now

Thank-you

Bill CEven if BBB is big, what about adding AAA to it. You can call "functions" within a batch file, so you could try making AAA a function in BBB or make BBB a function in AAA.



Discussion

No Comment Found