Is there a way to sync a txt file in batch mode?

I need to create a batch file that traces using the tracert some ip command and writes the trace to txt files. I want this to be fast, so I want to start a new command for each trace to immediately start a trace request.

there is my ping.bat:

@echo off set saveUnrechableLocation=..\pingUnreachableInfo\pingUnrechableInfoDB.txt set IpListLocation=..\ipInfo\all_DB_ip.txt set reachableLocation=..\pingRechableInfo\RechableIp\pingRechableInfoDB.txt set trace=..\pingRechableInfo\tracert\tracertDB.txt set numberOfPings=1 @echo pinging DB > %saveUnrechableLocation% copy /y NUL %reachableLocation% > NUL copy /y NUL %trace% > NUL for /F "tokens=*" %%A in (%IpListLocation%) do ( ping -n %numberOfPings% %%A | find "TTL=" >nul if errorlevel %numberOfPings% ( @echo %%A not rechable >> %saveUnrechableLocation% ) if not errorlevel %numberOfPings% ( @echo %%A >> %reachableLocation% start trace.bat %trace% %%A ) ) 

and trace.bat look like this:

 @echo off set saveLocation=%~1 set ip=%~2 tracert %ip% >> %saveLocation% exit 

The problem is that when I try to use this, I get this problem:

the process cannot access the file because it is being used by another process

What can I do to solve this problem? thanks!

+5
source share
2 answers

Windows redirection prevents multiple processes from simultaneously opening the same file for write access. Write operations must be serialized. This can be done with batch processing, as shown at fooobar.com/questions/402777 / .... However, I do not think this solution will help in your case.

Each trace process takes considerable time, and the output must be redirected all the time. But you want several processes to be executed at the same time, all weekends should be redirected to the same file. Even if you have to make it work, the output will alternate, and you will not be able to understand what all this means.

I recommend redirecting each tracert output to a unique file. You can include the ip address in the output file name, you can use the technique that I showed to merge the files after each process is complete.

Note that there is no need to pass the output location. Each child process has access to a trace variable, so it can easily redirect to the correct location.

contour of changes ping.bat

 ... set trace=..\pingRechableInfo\tracert\tracertDB ... start trace.bat %%A ... 

modified trace.bat

 @echo off tracert %1 >%trace%_%1.txt %= Redirect TRACERT to unique temp file =% :merge 2>nul ( %= Hide error messages inside the outer parentheses =% >>%trace%.txt ( %= Attempt stdout redirection - Fails if already locked =% type %trace%_%1.txt %= Write the temp file to the merge file =% (call ) %= Clear any error that TYPE may have generated =% ) ) || goto :merge %= Loop back and try again if stdout redirection failed =% del %trace%_%1.txt %= Delete the temporary file =% exit 

A short form without comments may look like this:

 @echo off tracert %1 >%trace%_%1.txt :merge 2>nul (>>%trace%.txt (type %trace%_%1.txt&(call )))||goto :merge del %trace%_%1.txt exit 
+5
source

this is fixed code based on dbenham's answer:

 @echo off tracert %1 >%trace%_%1.txt :merge 2>nul ( >>%trace%.txt ( type %trace%_%1.txt (call ) ) ) ||goto :merge del %trace%_%1.txt exit 
+1
source

Source: https://habr.com/ru/post/1246269/


All Articles