1

I have set up a batch file to be default to open .txt files. In an earlier question I found out that %1 gives me the path of the file which was actually calling the batch file. The Problem is: if the file name contains white space, it gets interpreted as multiple parameters.

Example:

opening file "C:\Users\Desktop\space true.txt"

%1 gives:"C:\Users\Desktop\space" and then %2 gives: "true.txt"

How could I get just the full file path with the name and white space without trying to do a loop to attempt to get the full path by combining %1%2%3%4...

UPDATE-----------------------

Sorry there was a miss communication. The code below is working. The trick was to put "%*" instead of "%1"

here the code:

@echo on
set var= "%*"
c:
cd "C:\Users\MyText Editor"
start javaw -jar "MyTextEditor.jar"%var%
pause

I do the whole changing the path, because the file which I double click and the the the batch file are in different directories. I had to change it to this.

UPDATE 2 --------------------------

The solution which worked best for me was from this fine gentlemen dbenham.

@echo off
pushd "C:\Users\MyText Editor"
start javaw -jar "MyTextEditor.jar" %*

The only complain I have is, that there is a case, where %* does not return the path with quotes. So I am searching for a final solution. Something like this "%~*" But this doesn't work.

Thanks in advance

Community
  • 1
  • 1
Haeri
  • 621
  • 1
  • 12
  • 27

5 Answers5

0

When calling your batch file, you must enclose your parameter in quotes if there is spaces in it.

E.g.: Batch.cmd "C:\Users\Desktop\space true.txt"

Eric

  • Well I don't personally call the batch file.. Like I said, I have set the batch file to be the standard "program" to be opened when dealing with .txt files.... I just double click on .txt files now... so there is no way I can put the path in ""... – Haeri Feb 17 '15 at 22:55
0
%*

Here's a list of characters.

&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
Serenity
  • 146
  • 2
  • Ok very useful. Thank you! Will accsept as solution as soon as I can implement it xD – Haeri Feb 17 '15 at 22:59
  • Could I ask you to maybe take a glance at my code in the updated op? I have had to change it several times now, because I discovered more and more little errors like whites paces and double quotes and other stuff. Since you seem like someone who understands cmd commands well, maybe you could give me a feedback and correct any dumb errors? – Haeri Feb 17 '15 at 23:08
0
@echo on
set var= "%*"
c:
cd "C:\Users\MyText Editor"
start javaw -jar "MyTextEditor.jar"%var%
pause

Becomes removing redundant commands

start "" javaw -jar "C:\Users\MyText Editor\MyTextEditor.jar" "%*"
pause

Echo is already on unless turned off by you.

We don't put things into variables for no reason, and it's already in %*. It just makes convoluted code and removes meaning from the name of the variable.

When programming (unlike typing) we don't change paths (and cd /d C:\Users\MyText Editor does drive and folder anyway).

We specify full path on the command line. This makes your meaning quite clear.

The main problem was there was no space between .jar and %var% and start command the first quotes on the line are assumed to the CMD's window title. I would code the path to javaw and not use start. Start is asking the Windows' graphical shell to start the file, not CMD.

Here's a batch file that starts vbs files. I don't specify path to cscript as it's a Windows' command.

It's complexity is to make use fairly idiot proof and easy for others.

@echo off


Rem Make sure filter.vbs exists
set filter=
set filterpath=
Call :FindFilter filter.vbs

Rem Add filter.bat to the path if not in there, setx fails if it's already there
setx path %~dp0;%path% 1>nul 2>nul




Rem Test for some command line parameters
If not "%1"=="" goto main

echo.
echo -------------------------------------------------------------------------------
echo.
echo   Filter.bat

echo   ==========
echo.
echo     The Filter program is a vbs file for searching, replacing, extracting, and 
echo     trimming console output and text files.
echo.
echo     Filter.bat makes Filter.vbs easily usable from the command line. It 
echo     controls unicode/ansi support and debugging.
echo.
echo           Type Filter Help or Filter HTMLHelp for more information.
echo.
cscript //nologo "%filter%" menu
Goto :EOF

:Main


echo %date% %time% %~n0 %* >>"%~dp0\FilterHistory.txt"



rem echo Batch file ran
rem echo %*
Rem /ud Unicode and Debug
If %1==/ud FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //u //x %%j&Goto :EOF

Rem /u Unicode
If %1==/u FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //u %%j&Goto :EOF

Rem /d Ansi Debug
If %1==/d FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //x %%j&Goto :EOF

Rem -ud Unicode and Debug
If %1==-ud FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //u //x %%j&Goto :EOF

Rem /u Unicode
If %1==-u FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //u %%j&Goto :EOF

Rem -d Ansi Debug
If %1==-d FOR /F "tokens=1*" %%i IN ("%*") DO cscript "%filter%
" //nologo //x %%j&Goto :EOF

Rem ANSI
cscript "%filter%
" //nologo %*&Goto :EOF

Goto :EOF

:FindFilter

If Exist "%~dpn0.vbs" set filter=%~dpn0.vbs&set filterpath=%~dp0&goto :EOF

echo find filter 1
If Not "%~dpnx$PATH:1" == "" set filter=%~dpnx1&set filterpath=%~dp1&goto :EOF

echo find filter 2
If Exist "%temp%\filter.vbs" set filter=%temp%\filter.vbs&set filterpath=%temp%&goto :EOF

copy "%~dpnx0" "%~dpn0.bak"
if not errorlevel 1 (
    echo creating "%~dpn0.vbs"
    goto :EOF
)

copy "%~dpnx0" "%temp%\filter.bak" 

echo Error %errorlevel%
if not errorlevel 1 (
    echo creating "%temp%\filter.bak"
    Goto :EOF
)
Goto :EOF
Serenity
  • 146
  • 2
  • No, the space was there in the definition of `var`. Also, the quotes in `"%*"` is the actual source of the problem. See [my answer](http://stackoverflow.com/a/28573172/1012053) – dbenham Feb 17 '15 at 23:26
  • `c:\Path to Java\JavaW.exe -Jar "C:\Users\MyText Editor\MyTextEditor.jar" "%1"` just putting this is the registry will do. No batch file needed. – Serenity Feb 18 '15 at 03:36
  • Yes, I came to the same conclusion in my edited answer not long before your comment. – dbenham Feb 18 '15 at 05:55
0

The following is not quite correct - I thought the file associations would put quotes around the file name like drag and drop does. But I was mistaken

This line is the source of your problem:

set var= "%*"

When files are dragged onto your batch script, or if a text file is double clicked, any file name(s) containing space will automatically be enclosed within quotes.

When you add your own additional quotes, it defeats the purpose of the quotes - the space is no longer quoted.

For example, a string like "name with space.txt" is treated as a single arg, but with added quotes, ""name with space.txt"" becomes three arguments.

There is no need for your var variable. You can use %* directly in your START command.

@echo on
pushd "C:\Users\MyText Editor"
start javaw -jar "MyTextEditor.jar" %*
pause

I'm not sure the above works properly if multiple files are passed. I suspect you may want the following:

@echo on
pushd "C:\Users\MyText Editor"
for %%F in (%*) do start javaw -jar "MyTextEditor.jar" %%F
pause

There is one potential problem. Windows has a bug in that file names containing & are not automatically quoted as they should. See "Droplet" batch script - filenames containing ampersands for more info.

EDIT - The following should work

OK, I did some tests and I believe your best bet is to modify the command associated with .txt files.

I tested association changes via the command line. This must be done via an elevated command prompt with admin rights. On my machine I go to the Start menu, click on "All Programs", click on "Accessories" folder, right click "Command Prompt", and select "Run as administrator", then click "Yes" to allow the program to make changes to the system.

The following command will show which file type needs to be modified

assoc .txt

On my machine it reports .txt=txtfile, so txtfile is what must be modified using FTYPE.

I believe the following should work for you:

ftype txtfile="C:\pathToYourScrpt\yourScript.bat" "%1"

Obviously you would need to fix the path to your batch script :-)

Once you have made the change, the filename will automatically be quoted every time your script is invoked via a file association.

Your batch script can then look like the following, and it should work no matter how it is invoked (excepting drag and drop with file name containing & but no space):

@echo off
pushd "C:\Users\MyText Editor"
for %%F in (%*) do start javaw -jar "MyTextEditor.jar" %%F

It seems to me you should be able to eliminate the batch script and configure FTYPE TXTFILE to open your java editor directly. I should think something like the following:

ftype txtfile="c:\pathToJava\javaw.exe" -jar "C:\Users\MyText Editor\MyTextEditor.jar" "%1"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thank you for your answer. You posted just before I had a chance to update op. The fix I needed was just "%*". But as you say I discovered, that the quotes actually mess up everything when dragging the file onto the batch. But when opening normally, %* returns the path without the quotes, so I needed them in there. Well I saw that "%~1" fixes the quote-issues, but I can't get a "%~*" to work... And I have to pass it to a temporary variable, because the batch file and the actual .txt file are in different directories.. – Haeri Feb 17 '15 at 23:39
  • ok. Well I am impressed. Your code works pretty well, except: %* without quotes only works if I drag the .txt file onto the batch file. If I double click on .txt, the path string does not contains quotes, which messes up java. I saw that a simple "~" like "%~1" fixes those problems, but this doesn't work with "%~*".. Any ideas?? – Haeri Feb 17 '15 at 23:55
0

Your problem is really that the syntax of your set command is wrong. In a batch file, a set command looks like this:

set "var=%1"

That will give you your variable exactly as received. If the user quoted it, then the variable's value will have quotes around it. To remove the quotes, you'd put a ~ in front of the number:

set "var=%~1"

Notice how the quotes go around the entire assignment, and not just around the value you are assigning. It is not set var="%1".

If you use set var= "%*", you haven't really fixed the fundamental problem that your syntax is wrong. Plus, often you really do want %1 and not the entire command line.

Here is an example script to test various quoting behaviors:

@echo off
set var="%*"
echo 1.  var="%%*"  --^> [%var%]  (wrong)

set "var=%*"
echo 2.  "var=%%*"  --^> [%var%]

set "var=%1"
echo 3.  "var=%%1"  --^> [%var%]

set "var=%~1"
echo 4.  "var=%%~1" --^> [%var%]

set "var=%~2"
echo 5.  "var=%%~2" --^> [%var%]

set "var=%~3"
echo 6.  "var=%%~3" --^> [%var%]

And here is the output of that script. Note how arg1, arg2, and arg3 are all quoted:

C:\batch> all_args.cmd "arg 1" "this is arg 2" "arg 3"

1.  var="%*"  --> [""arg 1" "this is arg 2" "arg 3""]  (wrong)
2.  "var=%*"  --> ["arg 1" "this is arg 2" "arg 3"]
3.  "var=%1"  --> ["arg 1"]
4.  "var=%~1" --> [arg 1]
5.  "var=%~2" --> [this is arg 2]
6.  "var=%~3" --> [arg 3]

You can see that numbers 4, 5, and 6 correctly pulled out their quoted arguments and saved the value into var. You typically want to save the argument without quotes, and then quote it when you use it in your script. In other words, your script should look like this:

@echo on
set "var=%~1"
c:
cd "C:\Users\MyText Editor"
start javaw -jar "MyTextEditor.jar" "%var%"
pause
indiv
  • 17,306
  • 6
  • 61
  • 82