# copying the compiled pdf in a folder from within tex file

In order to keep clean the folder where my .tex file is, I have successfully adopted the strategy suggested in other posts (see e.g. here or here), setting `-output-directory=build` and `-aux-directory=build` and adding `./build` in the additional search paths for the log file and the pdf file in the build advanced options.

This works, though all the files (including the pdf) are now in the `build` subfolder, whereas I would like to have the pdf in the same folder where the .tex file is.

To this aim, I created a simple batch file called `copy.bat` and put it in the same folder where the .tex file is. Its content is:

``````for /R "%cd%\build" %%f in (*.pdf) do copy "%%~f" ".\"
``````

What it does is to copy all the pdf files from the build subfolder to the current folder. If run from terminal it works as expected.

Now, I would like to run the .bat file from within the tex file I am working on. I have changed the PdfLatex command option so that now it is:

``````pdflatex.exe -synctex=1 -interaction=nonstopmode --shell-escape -output-directory=build -aux-directory=build %.tex
``````

I have then included `\immediate\write18{"./copy.bat"}` in my tex file, but I get an error when I try to compile (the pdf is corrupt). I really can't understand why. As a side question, would you use a different strategy to copy the pdf in the tex directory?

• The pdf is not finished when you call your script.Why do you use `-output-directory` when you don't want to redirect the pdf? – Ulrike Fischer Oct 17 '15 at 23:19
• Conversely, make a batch file that run pdflatex with options you need, and copy your pdf. – touhami Oct 18 '15 at 6:12
• @UlrikeFischer because without it I still have the synctex.gz file in the main folder. But I agree it's mostly because of my stubborness. Just out of curiosity, is there a way to wait until th pdf is finished? – Stefano Lombardi Oct 18 '15 at 7:50
• How should this be possible? Use some tool like arara or latexmk or build tools of your editor if you want to do some clean up after the job. – Ulrike Fischer Oct 18 '15 at 10:08
• do you really need synchronization? if yes don't `-output-directory=build` you'll lose it otherwise. if no you can try `pdflatex.exe -interaction=nonstopmode --shell-escape -aux-directory=build %.tex` and every thing is fine. – touhami Oct 18 '15 at 11:22