Use CreateProcess to run bat with spaces - batch-file

The CreateProcess documentation states that in order to run batch file one has to use cmd.exe /C <path to bat>. In fact it can run batch files just fine unless path to a script contains spaces (Microsoft confirms that in the description of the MS14-019 vulnerability). I wonder if it's possible to escape spaces in the path to batch file to make CreateProcess work. Adding quotes doesn't help, CreateProcess fails with the error:
cannot spawn "<path to batch>": No such file or directory
Update
A workaround is to use short file names as pointed by #jac in the comments. I still wonder why enclosing a path in double quotes works for normal executables but doesn't work for batch files.

There is no escape character for CreateProcess.
Since the code is broken and you can't fix it, you'll have to work around the problem. For example, create a junction point to the target directory and launch the batch file via the junction point path, or use short paths as jac suggested. (Do note that not all volumes will necessarily have short paths enabled, but if you are dealing with the system volume it is probably safe to assume that they will be.)

Related

How to Open Files in Directories with Spaces using CMD as Admin?

I'm writing a script to automate software installation and optimization using batch files. It needs administrator privileges.
When admin rights are given to cmd, I had to switch from "%cd%\program.exe" to "%0\..\program.exe". However, after doing that, I can't get a file to open if it is in a directory with spaces.
Subsequently, I removed the quotation marks at the beginning like this:
%0\..\program.exe"
This caused the directory problem to go away but now Programs with spaces won't open and using xcopy will give me a parse error.
I need it to open both files with spaces and files inside of directories with spaces.
Please help me solve this problem. Thanks in advance.
%0 should already be doublequoted, but is the drive path name and extension of the running batch-file. You really want to use this:
"%~dp0…\program.exe"
… represents an actual directory sequence, not a relative path
To get a better idea of what is going on open a Command Prompt window, enter Call /? and read its output.
Note: the p in %~dp0 already has a trailing backslash, so you don't need to append one yourself.

read from a .txt file in BATCH, without using literal path

Basically, i would like to use the type command, but I can't provide the actual path.
Currently my attempt was
type "./TESTS/Test1.txt"
but I'm assuming that since it's a relative path, it can't work.
I've run into the same issue with copy and xcopy.
I have been unable to solve this issue or find anything online.
Is there way to do this?
EDIT:
To clarify, I am trying to get my .bat file, to read the contents of a .txt file located in a subfolder (meaning the subfolder and the .bat file are in the same folder), and print it to the console.
Since you've now edited your question but seemingly not provided feedback on my earlier comment, here it is as an answer.
Windows and it's command interpreter, cmd.exe uses the backslash \ as its path separator.
Although many commands seem to accept the forward slash interchangeably, Type isn't one of those.
Additionally .\ is relative only to the current working directory, and in cmd.exe is unnecessary, though valid.
The following should therefore work as you intended:
Type TESTS\Test1.txt
Type "TESTS\Test1.txt"
Type .\TESTS\Test1.txt
Type ".\TESTS\Test1.txt"
If the location you are using is being received in the batch file with the forward slashes, you could set it to a variable, then expand that variable substituting the forward slashes for backward slashes:
Set "Variable=./TESTS/Test1.txt"
Type "%Variable:/=\%"
It may be necessary, depending upon the code we cannot see, to navigate to the batch file directory first, since it may not necessarily be the current working directory at the time of the invokation of those commands.
To do that use:
CD /D "%~dp0"
%~dp0 provides the folder, where your batchfile resides (including the last \) (does of course only work inside a batch file). So:
type "%~dp0Test\test1.txt"
is exactly what you want: <folder_where_my_batchfile_is\><subfolder_Test>\<File_test1.txt>
independent of your "working folder" (where the batchfile might have cd or pushd to).
Wouldn't it basically work by using %CD%? Like, TYPE "%CD%/Subfolder/Test1.txt"? %CD% is the windows variable for "Current Directory" and should be set to whatever directory the batch file is working in and since you're trying to access a folder within the same directory this should work. You're question is quite unclear, however, and I hope I'm not misinterpreting.

Find multiple files from the command line

Description:
I am searching a very large server for files that is on a different server. right now I open command prompt and type
DIR [FILE NAME] /S/4
This returns the server location of the file with some other stuff that is not really needed.
Question:
I have a lot of files to search and one by one input into the above command could take forever. Is there a way I could input all of the names of all the files and only search once and the search results would only need to show file name and location?
First, I hope you don't mean DOS, but rather Windows cmd or batch.
You can certainly write a script that will run your DIR command once per file being sought.
But what you most likely want instead is to search once and print the path of each file found. For this you can use PowerShell's FindChildItem or the improved one posted here: http://windows-powershell-scripts.blogspot.in/2009/08/unix-linux-find-equivalent-in.html
It will be something like:
Find-ChildItem -Name "firstfile.txt|secondfile.txt|..."
Another approach is to install msys or cygwin or another Linux tools environment for Windows and use the Linux find command.

Batch Launches exe with arguments

I'm trying to convert this line into Batch how would one start this exe with the arguments!?
Run("""C:\Program Files\Pro Inc\v420\commonfiles\TCL\bin\intel\cron.exe"" ""C:\Program Files\Pro Inc\v420\CommonFiles\Launcher\dank.thc"" -runae")
how does one do this? Thanks!
What seems to be the problem? This should do, I guess (if you are talking about Windows batch files):
"C:\Program Files\Pro Inc\v420\commonfiles\TCL\bin\intel\cron.exe" "C:\Program Files\Pro Inc\v420\CommonFiles\Launcher\dank.thc" -runae
That is, just specify the name of the executable to invoke, including the path if necessary (enclosed in double quotes if the name and/or the path contain spaces and/or other special characters), followed by a list of arguments. An argument may need to be quoted too if there's a chance it would be misinterpreted as several arguments otherwise (or, likewise, if it contains characters with special meaning).

How do I view or rename a file with missing extension?

I have a strange file in my file system without the extension part. The filename is "15.". The weird thing is that it is not one of those without the dot part (like just "15"), but the one with the dot but no extension ("15.") -- it is literally an illegal filename in windows, and not sure how did it get created in the first place.
I know it is a text file and it is about 15KB in size; however, due to the weirdness in name, I can't open it with any application -- I've tried to open in notepad, wordpad, etc., have tried the 'type' command to spit it out on commans shell, tried to shell-open enclosing filename in quotes, and so on -- all methods result in a 'file not found' error except the notepad, which says '15.txt' is not found.
Due to the nature of the issue and the way search engines optimize the search, it is extemely hard to search for an answer in any of the search engines online. So, I just wanted to put this question out there and see if anybody had to deal with a similar issue and have found any way to rename the file or even to change the extension.
Filenames that are valid in NTFS but cannot be used from Windows can be created when accesing disks or shares from other operating systems like Linux.
If you don't have a Linux installation at hand, then get hold of a "live" CD, boot Linux, and change the filename.
That may sound like a hassle, but Windows-only solutions (moving stuff around, deleting the directory) are even worse.
Use REN: http://ss64.com/nt/ren.html
It is a command prompt command (run > cmd > cd wherever > ren 15. 15.txt )

Resources