0

I am trying to execute a batch file which directs 7zip to compress a directory. The batch file is working fine when I run it by 'double-clicking' the file or if I try to run it in the command Prompt. But I am having problem while I am try to execute the file through a C# application. Below is my code in C#.

   string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
           path = path.Substring(0, path.IndexOf("Debug") + 6) + "Scripts";

            String EnvironmentPath = System.Environment
            .GetEnvironmentVariable("path", 
            EnvironmentVariableTarget.Machine);

            string[] varList = EnvironmentPath.Split(';');
            string enviVar=  varList.First(x=>x.Contains("7-Zip"));

            Process proc = new Process();

            proc = new Process();
            proc.StartInfo.WorkingDirectory = path;
            proc.StartInfo.Arguments = enviVar; 
            proc.StartInfo.FileName = "Script_To_BackUp_DB.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.UseShellExecute = false;
            proc.Start();

            proc.WaitForExit();

The value in path variable is : D:\Projects\Common\common\Common\Utilities\Utilities\bin\Debug\Scripts. If I remove the "proc.StartInfo.UseShellExecute = false" line, then batch will execute and close with an exception stating 7z is not recognized as Internal or external command. And I have already set the path in Environment Variable.

The batch file code is:

 set backup_dir=C:\Users\FU386DKH\Desktop\Card logs\
 set db_dir=D:\Projects\Projects\db\

 :: set dt string in dd_mm_yy_HH_MM_SS format 
 set dt=%Date:~0,2%_%Date:~3,2%_%Date:~6,4%_%Date:~0,2%_%Date:~3,2%_%Date:~6,2%

 :: compress folder 
 7z a -tzip "%backup_dir%_%dt%.zip" "%db_dir%" -ssw
Dannyboi
  • 3
  • 8
  • 1
    Show us also code in `Script_To_BackUp_DB.bat` as it looks like the error occurs in this batch file. Please take a look on [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) It could be reason 4 why `7z.exe` is not found on execution of the batch file. – Mofi Jan 18 '18 at 07:10
  • @Mofi I have added the batch file code as well. If there was a problem in the batch file, how could it run when I double-clicked it or when I ran it in cmd.exe ? – Dannyboi Jan 18 '18 at 07:22
  • When/where did you add the 7zip directory to the path? Add a diagnostic to your script to echo the path variable and then pause. All processes inherit their environment from their parent process. Read the answer Mofi linked in his comments. – jwdonahue Jan 18 '18 at 08:16
  • `set` and `dt=%Date:~0,2%_%Date:~3,2%_%Date:~6,4%_%Date:~0,2%_%Date:~3,2%_%Date:~6,2%` must be on one line. And with using `"%ProgramFiles%\7-Zip\7.exe"` or wherever you have installed 7-Zip instead of just `7z` in the batch file the Windows command interpreter has not to search around for `7z.*` with a file extension specified in `PATHEXT` in current folder and all folders in `PATH`. And is better to specify also `-ssw` left to ZIP file name and not at end of the command line. `-ssw` at end works also, but it is better to specify switches before archive file name. – Mofi Jan 18 '18 at 08:32
  • Add to your batch file above the 7-Zip command line following command line `set PATH>"%USERPROFILE%\Desktop\EnvPathAndPathExt.txt" & start "" %SystemRoot%\notepad.exe "%USERPROFILE%\Desktop\EnvPathAndPathExt.txt"` which outputs the __local__ environment variables `PATH` and `PATHEXT` to a file created on your desktop and open that file in Notepad for viewing it by you. – Mofi Jan 18 '18 at 08:38
  • @mofi Hi, I tried the solutions that you suggested but unfortunately they did not work. I However came across another solution which suggested to set the path with the location of the 7z .exe :- set PATH=%PATH%;C:\Program Files\7-Zip\ . Thanks for your help. – Dannyboi Jan 18 '18 at 13:43
  • I recommend reading MSDN articles [WOW64 Implementation Details](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384274.aspx), [File System Redirector](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187.aspx) and [Registry Keys Affected by WOW64](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384253.aspx). – Mofi Jan 18 '18 at 15:56
  • I suppose `"%ProgramFiles%\7-Zip\7.exe"` did not work because your C# app is compiled as 32-bit app resulting in running 32-bit `cmd.exe` for the batch file resulting in `%ProgramFiles%` expanding to `C:\Program Files (x86)` on your 64-bit Windows PC. I recommend also to skip entire batch file calling and do ZIP file creation directly in your C# app, see [Create normal zip file programmatically](https://stackoverflow.com/questions/2454956/). There is of course also a C# function to get current local date/time in a region independent format to build the date/time string for the ZIP file name. – Mofi Jan 18 '18 at 15:57

1 Answers1

0
set backup_dir=C:\Users\FU386DKH\Desktop\ConsoleApplication2\
set db_dir=D:\Projects\NPCI\db\

:: set dt string in dd_mm_yy_HH_MM_SS format 
set dt=%Date:~0,2%_%Date:~3,2%_%Date:~6,4%_%Date:~0,2%_%Date:~3,2%_%Date:~6,2%

:: compress folder 
::Setting the path with the location of 7zip exe file. 
set PATH=%PATH%;C:\Program Files\7-Zip\
7z a -tzip "%backup_dir%_%dt%.zip" "%db_dir%" -ssw
Dannyboi
  • 3
  • 8