0

EDIT: I have proprietary application which supports only executing windows commands and executables (only from Windows folder) with paramaters and I need to create folder using that but I don't want to see cmd window blink. This app doesn't support any dll imports or code customization.

Is there a way how to create folder in windows without command prompt? When I run

cmd /C mkdir "C:\Path\To\Dir" 

it causes blink of quick showing and hiding cmd window (I want to avoid that). When I run

rundll.exe shell32.dll,SHCreateDirectory "C:\Path\To\Dir" 

or

rundll.exe kernel32.dll,CreateDirectoryA "C:\Path\To\Dir" 

Command passes just fine, but doestn't create my directory I wanted to create. Would be good to have such command also for creating direcotry with full non existing path.

EDIT2: I found a bit nasty, but working workaround for this problem: I found out that I can create text files using that proprietary app. So first I've created file: run.vbs Then I wrote few lines in it:

If WScript.Arguments.Count >= 1 Then
    ReDim arr(WScript.Arguments.Count-1)
    For i = 0 To WScript.Arguments.Count-1
        Arg = WScript.Arguments(i)
        If InStr(Arg, " ") > 0 Then Arg = """" & Arg & """"
      arr(i) = Arg
    Next
    RunCmd = Join(arr)
    CreateObject("Wscript.Shell").Run RunCmd, 0, True
End If

then run

wscript run.vbs cmd /C mkdir "C:\Temp\TmP"

And vuola!

BreteP
  • 71
  • 1
  • 7
  • This isn't exactly programming-related... – AStopher Mar 04 '15 at 19:55
  • From where are you calling this? Is it from an installer? – rene Mar 04 '15 at 19:58
  • It's a specific proprietary application and there is no way to import any dll or create custom code. Only thing it supports is to execute command with parameters. – BreteP Mar 04 '15 at 20:05
  • It sounds like you are bound to what that application allows you to run. I'm still confused to what the requirements are. If your question is "how to make a folder without command line", and you can't write code, then what would your options be? – gunr2171 Mar 04 '15 at 20:09
  • If you know some programming language, why not write a small program to take in some argument and then create a directory? – AStopher Mar 04 '15 at 20:11
  • 1
    I'm guessing you can't run the command `mkdir "somepath"` in the program, without having to call `cmd /C` – gunr2171 Mar 04 '15 at 20:16
  • That proprietary application just call [CreateProcess](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) in the winapi, right? Or is there another abstraction layer that you are aware of? – rene Mar 04 '15 at 20:16
  • - I can't run mkdir without cmd and yes, it has abstraction layer so no CreateProcess, it's a pretty tricky. So far I've tested cmd /C mkdir ... which works, but the customer doesn't like the blink of cmd window (me neither). – BreteP Mar 04 '15 at 20:34
  • @BreteP the solution that worked for you is basically my Option 2 but then without starting the cmd again, The FileSystemObject can create folders. – rene Mar 05 '15 at 10:02

1 Answers1

3

If you can't control how your process is created you are basically at the mersey of the implementers of that code. The winapi CreateProcess does run *.bat and/or *.cmd files but via %ComSpec% /c, which is normally cmd.exe /c. But then, .bat files are no .good here exactly because cmd runs them, which opens a console

Here are a few options that might or might not work in your case:

Option 1 (idea from @HarryJohnston)

Use the following powershell command

powershell -WindowStyle Minimized -NoLogo -NoProfile -NonInteractive -Command mkdir c:\your\path\tocreate > %temp%\scratch.txt

Option 2

Have the script interpreter run the following file:

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Create a new folder
oFSO.CreateFolder WScript.Arguments.Item(0)

which you start with the following command:

wscript mkdirsil.vbs "c:\path\to\create" //NoLogo

//Nologo Prevent logo display: No banner will be shown at execution time

Option 3

Compile the following C# Winforms program that keeps its main window hidden. You need to deploy the executable and I don't know if that is feasible.

using System;
using System.Windows.Forms;
using System.IO;

namespace SilentMkDir
{
    public class Program:Form
    {
        public Program(string dir)
        {
            this.SuspendLayout();
            this.ClientSize = new System.Drawing.Size(0, 0);
            this.ControlBox = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "MakeDir";
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.Load += (s, e) =>
            {
                try
                {
                    Directory.CreateDirectory(dir);
                }
                catch (Exception all)
                {
                    MessageBox.Show(all.Message);
                }
                this.Close();
            };
            this.ResumeLayout(false);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] path)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Program(path[0]));
        }
    }
}

This commandline should produce an*.exe for you, asuming above code is in mksil.cs

csc mksil.cs /r:System.IO.dll /r:System.Windows.dll

Or you could create something similar in C/C++.

Option 4 (idea @eryksun)

With Python installed you could do

pyw -c "import os; os.mkdir(r'C:\Path\To\Dir')"

since pyw.exe is a GUI app

You are basically looking for an exe that is a Windows GUi application instead of a Console application. The following batch command scans the system folder for such executables (put it on one line) but none were found. (notice this needs to run from the commandline, from a script file use %%a instead of %a. dumpbin comes with the VC++ compiler

for %a in (%SystemRoot%\System32\*.exe) do 
      @(dumpbin /headers %a | 
        find "subsystem (Windows GUI)" > nul && 
        echo %~nxa)

Option Not Possible

Initially I hoped start could work but @HarryJohnston pointed out that it is an internal command of cmd and not an exe.

start /B /min cmd /c mkdir

Starts a separate window to run a specified program or command.

but with the /B (no new window) and /MIN (minimized) you might get away with it.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Problem is that I can use only default windows executables so only one possible way is to start command: cmd /C mkdir ... (it can't start wit start, or just mkdir) Acceptable solution would be to hide this window via some cmd parameter but I didn't fiugure out how. – BreteP Mar 04 '15 at 21:47
  • 2
    I think `powershell.exe` has the necessary functionality. – Harry Johnston Mar 04 '15 at 22:00
  • Powershell won't accept the `>nul` redirection (generates a "FileStream will not open Win32 devices such as ..." error) but you could redirect to a temporary file or something. Hopefully not necessary anyway; the application the OP is working with must do something with standard output, and it *might* be something sensible. :-) – Harry Johnston Mar 04 '15 at 23:54
  • @HarryJohnston, the system creates and shows a console (conhost.exe) for powershell.exe, if it doesn't inherit one. Probably under normal circumstances the window gets minimized too fast to see it, but I expect it'll be visible when the system is busy. – Eryk Sun Mar 05 '15 at 00:06
  • Rats, so it does. And wscript.exe doesn't have an option to provide code to run on the command line. Well, I'm out of ideas. – Harry Johnston Mar 05 '15 at 00:23
  • @HarryJohnston, maybe you can find something useful in this list of GUI executables: `for %a in (%SystemRoot%\System32\*.exe) do @(dumpbin /headers %a | find "subsystem (Windows GUI)" > nul && echo %~nxa)`. I don't see anything. With Python installed I'd just use `pyw -c "import os; os.mkdir(r'C:\Path\To\Dir')"`, since pyw.exe is a GUI app. I'm surprised PowerShell doesn't have an equivalent. – Eryk Sun Mar 05 '15 at 01:30
  • maybe worth a try: are `bat`files accepted as "default windows executables"? – Stephan Mar 05 '15 at 10:11
  • @rene, `CreateProcess` runs .bat and .cmd files via `%ComSpec% /c`, which is normally `cmd.exe /c`. But then, .bat files are no good here exactly because cmd runs them, which opens a console. – Eryk Sun Mar 06 '15 at 02:55
  • The little script to list GUI executables was written for the command prompt; in a batch file it needs to reference the variable as `%%a` instead of `%a`, since it gets doubly parsed. Also, `dumpbin` is a VC++ tool. Unfortunately Windows isn't distributed with a tool that inspects PE/COFF binaries. – Eryk Sun Mar 06 '15 at 03:15