0

I'm trying to run a batch file in vb.net. This is the command I'm using:

    System.Diagnostics.Process.Start("C:\Folder\convertTIF2PNG.bat")

The batch file is supposed to crawl through the folder it is located in and convert all the .tif files to .png. When I call the file, the command window opens (so I know something is happening), however none of the .tif files are converted. When I simply double click on the batch file in the directory, it runs properly (so I know it is not a problem with the batch file). Why is my code not running the batch file correctly? Here is the code in the batch file:

    for /r %%a in (.) do (
pushd %%a   
  (
    "C:\Program Files (x86)\IrfanView\i_view32.exe" *.tif /convert=*.png /transpcolor=(255,255,255)
    erase /f/q *.tif
  )
  popd 

)

Aidan Kehoe
  • 104
  • 3
  • 14
  • How does the batch file refer to the directories that the TIF files are in? You'll need to consider the directory that you're running from as the default directory. You can add this as part of ProcessStartInfo. Or, simply make any folder references absolute in the batch file to avoid the issue. – ManoDestra Jun 01 '16 at 14:29
  • @ManoDestra Thanks for responding! I'm still a little confused. The batch file refers to the directories the TIF files are in simply by searching for the .tif extension in its parent directory (ie if it's in C:\folder it checks C:\folder). Here is the code from the batch file: `for /r %%a in (.) do ( pushd %%a ( "C:\Program Files (x86)\IrfanView\i_view32.exe" *.tif /convert=*.png /transpcolor=(255,255,255) erase /f/q *.tif ) popd )` How do I avoid the issue? – Aidan Kehoe Jun 01 '16 at 14:35
  • You can explicitly stated C:\Folder in your batch file, so that the path where it finds the files is absolute. Otherwise, you're going to have to amend the ProcessStartInfo so that it knows that the process is intended to be started in C:\Folder, so that your batch file then works. If you try running your batch file from a directory that isn't C:\Folder, then you'll see it fail in the same fashion as it does from VB.Net. – ManoDestra Jun 01 '16 at 14:51
  • 1
    Okay thanks for your help! – Aidan Kehoe Jun 01 '16 at 14:56

2 Answers2

0

The problem is, your batch file is running from whatever directory your vb.net app is running from. You would have to put the vb.net app in the same location (c:\folder) for this to work. Instead of doing it this way, you could convert your batch file into a vb.net command line app and pass in the directory you want converted as a command line argument.

Or, if you really want to keep your code in the batch file, pass in the path as a command line argument to the vb.net code, then pass it in to the batch file.

The important thing is to pass the path in as a command line argument, otherwise you will constantly be updating either your vb.net app or batch file whenever the location changes.

Brandon Griffin
  • 348
  • 1
  • 8
0

You can amend your batch file to refer to the absolute path where the files are located.

for /r %%a in (C:\Folder) do ( ... etc, etc.

Or, you can use a ProcessStartInfo object instead so that your VB.Net application knows which folder to start the processs in as its default folder. Your batch file would then be fine. However, I'd argue that it's better to amend the batch file, as it won't work unless it's run in that specific folder, which you should probably avoid. You could pass the directory to process as a parameter to the batch file.

See here for further info on the WorkingDirectory parameter of the ProcessStartInfo object.

And here for an example (C#, but easily amended to VB.Net): c# ProcessStartInfo

Community
  • 1
  • 1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50