dir command is running on path given in command + current path - c#

I am running a test to check if there are files of type *.exe in a certain folder.
I am sending the commands from one computer to another (if that's matter..)
my condition in the code is:
if (string.Contains("File Not Found"))
test PASS
else
test FAIL
(don't mind the syntax PASS\FAIL it's just the idea)
(when files not found with DIR command then this string will appear at the end of search)
the command sent is:
"dir " + PATH + " *.exe /s"
the problem is, in results I get all the subfiles in the wanted directory, but also the command runs on the current folder (which can be anything, I can't change that and it can change from time to time and obviously I don't want this folder to effect on the result of the PATH I searched in).
the code written in C#, the commands themselves run on cmd.
what can be the problem?
the results look like this:
Directory of C:\Windows\Temp //PATH = C:\Windows\Temp
12/26/2019 04:26 PM <DIR> .
12/26/2019 04:26 PM <DIR> ..
0 File(s) 0 bytes
Directory of C:\Program Files (x86)\.............
09/03/2019 08:11 PM 8,704 NAME.exe
.
.
.
18 File(s) 2,141,696 bytes
so my test fails when it shouldn't.
how can I resolve that?

"dir " + PATH + " *.exe /s"
^--- this space is your problem.
You execute dir with two parameters, so it lists all files matching the first string (PATH), then all files matching the second string (*.exe from the current working folder). You can verify in the command prompt with dir c:\windows *.* vs. dir c:\windows\*.* So remove the space.
Note: it's safer to also put quotes around the string (in case there are intended spaces (like in Program Files): dir "c:\program files\*". (I don't know how to do that in C#, but if you know about C#, it should be easy)

You can use multiple commands on the same line
"cd " + PATH + " && dir *.exe /s /b"

Related

Command Line with C#

I'm trying to run a software's batch utility tool via C# and the command line but am not able to get it to work. I am able to build a string which is correct and works when I just copy and paste it into the command prompt, but when my code tries to do that step automatically nothing happens; the script just runs to the end and nothing happens.
For reference, this is what I'm trying to do: https://knowledge.autodesk.com/support/navisworks-products/learn-explore/caas/CloudHelp/cloudhelp/2020/ENU/Navisworks/files/GUID-14B017E2-B4A2-432F-8C2D-1F1856EECB8E-htm.html
I've tried a number of times changing the different properties of StartInfo, but nothing has worked so far. Could there be an issue with the different directories that are at play?
"filesTextFile" is a .txt file on the C: drive which lists paths for files located on a shared network drive (\\{network}\)
cmd.exe is in the C: drive
"NWBatchUtilityLoc" variable is the file path for an .exe on the C: drive
.cs script is running from the D: drive.
This is an excerpt of my code that shows how the string is being built and how I am using System.Diagnostics.Process.
var utilityCmd = $#"""{NWBatchUtilityLoc}"" /i ""{filesTextFile}"" /osd /log ""{logDir}"" /lang en-US";
var p = new Process
{
StartInfo =
{
FileName = #"C:\Windows\System32\cmd.exe",
Arguments = "/C " + utilityCmd,
}
};
p.Start();

I want to copy filenames in dynamic folder on right-klick to clipboard but zip

I want to copy filenames on right-clicking the containing folder to clipboard, but if there are zip, iso, rar, or 7z files I need a list of the content from them too.
I tried in batch/cmd but it won't work and now I am testing with C#. But I don't know how to simulate an explorer contextmenue-event to send the parameters to C#-App.
On both ways I start with a regedit entry in:
HKEY_CLASSES_ROOT\Directory\shell\Liste\command\` REG_SZ with attribute C:\liste.bat "%1%"`
liste.bat:
set var=%1\
set listtmp
del c:\ESC\* /q /f
dir c:\ESC\
copy %var% c:\ESC\
cd c:\ESC\
dir /s /b >%listtmp%
if exist c:\ESC\*.zip
(7z l *.zip >>%listtmp%)
if exist c:\ESC\*.7z
(7z l *.7z >>%listtmp%)
echo listtmp|clip
C# offers the benefit of list handling in variables and enums for checking archives, but I don't understand how to use the parameter from a drop-menu to start my C#-app, so the batch is more handy, with C#-exes more people have doubt because adware... paranoid colleague ;)
I think there is a way, without copping the files and without that amount of variables. Oh and sorry when I not mentioned a vary necessary thing, this is my first post.
Next C:\bat\dirToClp.bat file could help with your I want to copy filenames on right-clicking the containing folder to clipboard:
#ECHO OFF >NUL
#SETLOCAL enableextensions
set "listtmp=%tmp%\list.tmp"
pushd "%~1"
dir /S /B /A-D>"%listtmp%"
if exist *.zip 7z l *.zip>>"%listtmp%"
if exist *.7z 7z l *.7z>>"%listtmp%"
type "%listtmp%"|clip
del "%listtmp%"
popd
echo Your clipboard now holds result of:
echo dir %CD%
pause
#ENDLOCAL
#goto :eof
Here echo and pause for when using clip in a batch script you should warn the user that their clipboard is about to be overwritten. Eventually /A-D switch could be removed from dir /S /B /A-D if desired (causes folder names not implicated in list).
Please pay your attention to 7z calls since I can't verify and confirm rightness of.
The value c:\windows\system32\cmd.exe /D /C c:\bat\dirToClp "%1" exported from registry as follows (but could be under HKEY_CLASSES_ROOT\Folder registry key instead of HKEY_CLASSES_ROOT\Directory):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\DirToClipboard]
[HKEY_CLASSES_ROOT\Directory\shell\DirToClipboard\command]
#="c:\\windows\\system32\\cmd.exe /D /C c:\\bat\\dirToClp \"%1\""
;
Here is not pure C:\bat\dirToClp.bat call but via cmd.exe /D /C because, for workaday and security reasons, I use "Open with PSPad" default action for .bat, .cmd, .vbs etc.
Also the batch saved in that C:\bat folder for I avoid placing files to disk root (and to %userprofile% root and to desktop as well) as a matter of general principle.

Delete a program that are running in console of C#

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.

Trouble running .cmd file with c#

For some reason, I can't seem to launch and run a .cmd file with c#. An example of a line of the cmd file is:
"C:\Windows\system32\ffmpeg64.exe" -v verbose -y -i "S:\TEMP\A.ts" -c:v copy -c:a copy -ss 00:00:00.000 -t 2 "S:\TEMP\A_SHORT.ts"
I've tried several different ways to launch this file from within C#, such as (where curDirectory is for example "S:\TEMP")
Process p = Process.Start(curDirectory + "\\ffmpeg.cmd");
I've also tried
string path = curDirectory + "\\ffmpeg.cmd";
Process p = Process.Start("cmd.exe", #"/c " + path); //I've also tried /k
But what happens is the cmd prompt will show up and say "C:\Windows\System32\ffmpeg64.exe" is not recognized ..." even though the file is there. What am I doing wrong?
If your system is running the Windows 6.1 kernel or later, System32 is actually comprised of other directories based on the application you're running (depending on whether it is a 32-bit or 64-bit application).
I assume that ffmpeg64.exe is a 64-bit application, and when you execute the .cmd file manually, it should default to a 64-bit command prompt - Also ensure that your application is targeting "x64" or "Any CPU". Alternatively, you could place a 32-bit version of ffmpeg in the WoW64 directory.
Also, I know you've stated in comments that you don't want to read the .cmd file and modify it, but you could compose your ProcessStartInfo with Environment.SystemDirectory instead of the hard-coded path.
As a last option, you could place the ffmpeg exe somewhere static (as you stated in the comments, in c:\ works), or just in your application's working directory.

Can we run a batch file (.bat) from the shared path

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.

Categories

Resources