-1

I'm having a strange problem when running a bat file from c# code(Also from cmd.exe and Run window). First of all, my bat file looks like this

file_copy.bat

NET USE %1 /USER:%2 %3 COPY /Y %4 %5 NET USE %1 /DELETE PAUSE

I've included this bat file in visual studio 2013, and set Copy To Output Directory to Always Copy from properties. My code to run this bat file looks like this

            string args = "\"\\\\192.168.54.196\\c$\" \"user\" \"123456\" \"\\\\192.168.54.196\\c$\\paykiosk\\logs\\pgw_150113.log\" \"C:\\Users\\IlhamIs\\AppData\\Local\\Temp\\pgw_150113.log\""
            var processInfo = new ProcessStartInfo
            {
                UseShellExecute = false,
                FileName = "file_copy.bat",
                Arguments = args
            };

            var process = new Process { StartInfo = processInfo };
            process.Start();

I build my project and obviously file_copy.bat is copied to bin\debug directory(in my case). Then I run application, bat file executes, but first command is somehow modified at runtume, therefore an error occurs saying 'я╗┐NET' is not recognized as an internal or external command, operable program or batch file.. This is wierd, я╗┐ is written to the beginning of first command. Here is complete output that I got:

C:\Users\IlhamIs\Desktop\test\bin\debug>я╗┐NET USE "\\192.168.54.196\c$" /USER:"user" "123456"
'я╗┐NET' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\IlhamIs\Desktop\test\bin\debug>COPY /Y "\\192.168.54.196\c$\paykiosk\logs\pgw_150113.l
og" "C:\Users\IlhamIs\AppData\Local\Temp\pgw_150113.log"
Logon failure: unknown user name or bad password.

C:\Users\IlhamIs\Desktop\test\bin\debug>NET USE "\\192.168.54.196\c$" /DELETE
The network connection could not be found.

More help is available by typing NET HELPMSG 2250.


C:\Users\IlhamIs\Desktop\test\bin\debug>PAUSE
Press any key to continue . . .

Then create a bat file in bin\debug directory with the same script (file_copy_2.bat), note that this is not created by visual studio. This time I change FileName = "file_copy.bat" to FileName = "file_copy_2.bat" to make my application to run the file I created and then I run my application. It looks ridiculous, but it worked, all 3 commands executed successfully.

This is really strange. What am I missing? Does visual studio do something special with bat file during compilation and copying file to output directory? Both file_copy.bat and file_copy_2.bat are the same (I've checked attributes just in case, also the same). The file visual studio created fails to execute first command, BUT the one I created myself succeeds. Why?

FYI, Code Page code returned from command chcp is 866. I've tried 65001 which is utf-8, but I've got similar result, different characters written to the beginning of command instead of я╗┐

Please comment below if question details aren't clear to you, as I could explain wrong

Thanks in advance

  • 1
    look at [this](http://superuser.com/questions/113696/strange-symbols-while-executing-win-command-file) question and answer on SuperUser, he has the same problem with the same strange symbols – grabthefish Jan 13 '15 at 14:27
  • 1
    That looks like a byte order mark. Use a text editor that can save without a BOM. Like notepad++. Or save as ASCII/ANSI. – spender Jan 13 '15 at 14:28
  • I saved the file without BOM and it worked! Thank you for your assistance, I googled a lot, but didn't find that question posted on SuperUser, spent all day solving this – Ilham Israfilov Jan 13 '15 at 14:41
  • See the second paragraph of http://stackoverflow.com/questions/24735579/codedui-test-will-not-find-csv-input/24768318#24768318 where I wrote about how Visual Studio creates files with unexpected BOMs. The rest of that Q&A is about BOMs being misinterpreted by another part of Visual Studio. – AdrianHHH Jan 13 '15 at 15:38

1 Answers1

0

The problem is clearly in your file_copy.bat.
You can proove it, by run the file_copy.bat from command line (with appropriate parameters).

Do you creating the file_copy, or copy paste from some location in project?

Update: From comments i understand that. The problem is the encoding of the file_copy.bat.
When converting bat file to the UTF-8 without BOM, the problem is solved.

Avram
  • 4,267
  • 33
  • 40
  • 2
    as I mentioned in my question, I also tried running this bat file from command line and Run window. And, yes, I'm creating new bat file from scratch. Same results with copy/paste. The problem was in the encoding that I provided, by default it's saved with BOM, I tried saving the file with UTF-8 encoding without BOM and problem solved. Thank you for the answer – Ilham Israfilov Jan 13 '15 at 15:10