I have a shared path (like //servername/c$/batches/) where all my batch files are located now I am writing an web application in C# to run .bat files from that application. I know way to do it when I have a physical path.
But here I dont have a physical path. Is it possible to do it.
EDIT# 1
I execute my bat files just by double clicking on them or open the cmd progam on the physical server and then navigate to the drive and execute the bat file.
EDIT #2
when I put UNC path the get the following error
I getting an error myprogram.exe is not recognized as an internal or external command operable program or batch file. 9009
Batch files don't support UNC paths as their "current directory". There's a hackish work around of doing:
pushd "%~dp0"
your batch stuff
popd
%~dp0 expands to the current (d)rive/(p)ath/(0)batchfilename
example:
ok. a Simple batch file:
pushd %~dp0
echo "Hello from batch land"
echo %~dp0
popd
put that on a server somewhere, and try to run it via a unc path:
C:\> \\server\share\test.bat
You'll get as output:
C:\>pushd \\server\share\
Z:\>echo Hello from batch land
Hello from batch land
Z:\>echo \\server\share\
\\server\share\
Z:\>popd
C:\>
Weird, but it works.
That's called a UNC path.
You can use it just like any other path.
However, the user that your ASP.Net code is running as must have read access to the network share.
Apparently, you do have a current-directory issue.
The .bat file is trying to run myprogram.exe from the current directory.
You can make a wrapper batch file on your local machine that maps the network share:
pushd \\server\c$\dir
call filename.bat
popd
You can put this wrapper file anywhere, then call it from your code.
Related
I have massive 500GB index files on a Windows Server 2012R2 which are being constantly updated by an application.
I have to zip the file using PowerShell, but when I try to zip it using the following snippet of code I get this exception:
Exception calling "CreateFromDirectory" with "4" argument(s): "The process cannot access the file 'E:\Program Files (x86)\Application Folder\File\Status.FCS' because it is being used
by another process."
$zip = "E:\Folder\File.zip"
$can = "E:\Program Files (x86)\Application Folder\File
Import-Module AWSPowerShell
# functions
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}
ZipFiles $zip $can
I don't have any issues if I am compressing the files using the Windows GUI, but problem seems to happen only when I am using a PowerShell script. Also if I stop application services, the PowerShell script works fine (which I can't do in prod environment).
One of the solution is to copy folder with 500Gb of index and compress it (which should work) but I don't have enough disk space on my Windows server to do so.
So is there any solution to compress the file while it is write locked using PowerShell script?
you need to set a different directory for the zip file : [System.IO.Directory]::SetCurrentDirectory($inPath)
so
Function ZipFiles( $zipFileName, $pathTarget )
{
Add-Type -Assembly System.IO.Compression.FileSystem
# Emplacement de sortie
[System.IO.Directory]::SetCurrentDirectory($inPath)
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($pathTarget, $zipFileName, $compressionLevel, $false)
}
I have create a program that delete a folder.
Like this :
if (Directory.Exists((System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "My Dir Name"))))
{
Directory.Delete(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "My Dir Name"), true);
}
this is alright . But a file that name is "Delete.exe" is in directory of "My Dir Name" [Like my code ] ! It gave an error, because "Delete.exe" is running and can't delete .
What can I do to delete, "Delete.exe"?
You're trying to delete the folder in which your running application is in and therefore delete the program itself? Well, you have a few options. It is interesting to note as well that batch files can delete themselves. So, that also leaves you a few more options:
have your C# program copy itself to a temp directory before deleting the folder, and run from there.
use a batch file entirely instead of C# - having it do whatever and then delete itself afterwards.
have your C# program generate and execute a batch file and then quit - leaving the batch file to clean up after the C# program exits.
run a CLI command to delete your program after a specified time:
Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + Application.ExecutablePath);
you can use MovFileEx to specify to have your program deleted on next start up.
Most of these answers imply that the running EXE is your own appliation. if it is not, though - #5 will still work. you can schedule it to be deleted the next time the computer starts.
I'm trying to edit the hosts file while my program is started as "NT AUTHORITY\SYSTEM" with psexec, and I still get UnauthorizedAccessException. I thought that SYSTEM should be able to edit hosts file that's why I'm started my program with psexec -i -d -s myapp.exe.
So what should I do to be able to edit hosts file?
Make sure that your hosts file is not marked as read-only. If it is, you will get this exception regardless of current user permissions.
I am trying to run xxx.exe file using command prompt with silent mode. i saw this link in Google: http://www.powerware.com/Software/lansafe_help/LSHelp424.htm.
when i run this command : C:>"D:\xxx.exe" -r -f1"D:\Test.iss"
am getting error : "xxx.exe" is not recognized as an internal or external command operable program or batch file.
Can any body give the idea where i am doing mistake.
As others said, make sure your path to your exe file is correct. You can change directory where exe is before execution or write out the full path.
By silent mode if you mean to run exe without any output on screen, then simply redirect the output to a file.
E.g. if your exe is in D:\myprog\myprog.exe, then following command will make your program run in "silent" mode:
c:>"D:\myprog\myprog.exe" > "D:\myprog\output.txt"
Above example will dump output into output.txt file.
you have to run your command in which location EXE file is located. can you check whether its residing in d:?
You may switch over to the D:\ drive before running your command, but it shouldn't matter. Double check it actually exists in that location
First I am running the below Command "mysqldump.exe --user=root --password=root accounts>accounts.sql" in the path C:>, to take backup, but it show error message "mysqldump is not a recognized command".
Then i changed the path in command prompt using the cd command to the location
"C:\Program Files\MySQL\MySQL Server 5.0\bin>"
Now i am running the below command.
C:\Program Files\MySQL\MySQL Server 5.0\bin> mysqldump.exe --user=root --password=root accounts>accounts.sql
It is successfully working, but how can i achieve this work from VB.NET or C#.NET.
Not quite sure what you are asking, but the following, runs the mysqldumb.exe with the parameters you specified. (C#)
Process.Start(#"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe", "--user=root --password=root accounts>accounts.sql")
You mean changing the current path?
Here's the C# code to do it ...
Directory.SetCurrentDirectory(#"C:\Program Files\MySQL\MySQL Server 5.0\bin");