Unable to drag and drop a filename containing spaces onto a batch file

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
I wrote a simple batch file that takes two filenames as parameters and renames one of the files (.srt) so it matches the other (.mkv). The two filenames are selected in a windows explorer window and dragged and dropped onto the filename of the .bat file on the desktop. It works well, unless one of the filenames contains a space character. In that case, in the opened cmd-window the error message "The filename, directory name or volume label syntax is incorrect" is displayed. The batch file itself does not even start. So, to illustrate: Movie1.mkv with Subtitles2.srt correctly results in the latter being renamed to Movie1.srt. But Movie1.mkv with Subtitles space 2.srt does not work, nor does Movie space 1.mkv with Subtitles2.srt. Does anyone have any idea why?
OS: Windows XP Pro SP3 up-to-date
 

DaveA

David
Trusted Advisor
Spam Fighter
Joined
Nov 16, 1999
Messages
16,895
If I remember right a file name with a space(s) must be enclosed with quotes!
 

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
Thanks for your reaction. The problem is that I cannot control that, because I use drag and drop from the windows explorer window. Like this: I open a windows explorer window and navigate to the folder containing the two files. I resize the windows explorer window so that the icon of the batch file on my desktop is visible. I click on the first file, then ctrl-click on the second file. They are now both "highlighted". Now I drag and drop them onto the icon. The batch file runs with the filenames in %1 and %2; if there are no spaces in the filenames it works fine. Windows XP is in charge of how the parameters are passed; there is no way for me to add quotes to the filenames.
 

DaveA

David
Trusted Advisor
Spam Fighter
Joined
Nov 16, 1999
Messages
16,895
Can't you include the quotes so that ALL file names are placed between the set of quotes?
 

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
I would have to patch Windows to do that, but I would not know how or where. Also, it would be a circumvention of the problem. I have read at several places on the web that Windows is itself supposed to enclose the filename in double quotes if it sees that the filenames contains one or more spaces, before passing it on as a parameter.

Please note that this is a problem with DRAG AND DROP, where Windows is responsible for the conversion of the selected filenames into parameters. I think the question therefore is: why is Windows, during drag and drop, failing to enclose the filename(s)-containing-spaces in double quotes?
 

DaveA

David
Trusted Advisor
Spam Fighter
Joined
Nov 16, 1999
Messages
16,895
You should be able to add the quotes to the BATCH file not the OS.

Batch files are really DOS level commands and not Windows level.
 

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
The problem is that the batch file is not executed AT ALL, not even the very first word on the very first line. All that appears in the opened cmd-window is the error message, followed by the command prompt e.g. C:\path>

Should anyone want to try for themselves, here is the batch file. Drag and drop Test1.mkv and Test2.srt together onto it and the result will be Test1.mkv en Test1.srt. Drag and drop Test1.mkv and Test space2.srt together onto it and MY result is the error message and nothing renamed.

@ECHO off

IF [%1]==[] GOTO :_param_fout
SET _file_long1=%1
SET _file_short1=%~nx1
IF [%2]==[] GOTO :_param_fout
SET _file_long2=%2
SET _file_short2=%~nx2
IF NOT [%3]==[] GOTO :_param_fout

IF /I %_file_short1:~-4%==.mkv (
SET _mkv_file_short=%_file_short1%
) ELSE (
IF /I %_file_short2:~-4%==.mkv (
SET _mkv_file_short=%_file_short2%
) ELSE (
GOTO :no_mkvfile
)
)

IF /I %_file_short1:~-4%==.srt (
SET _srt_file_long=%_file_long1%
) ELSE (
IF /I %_file_short2:~-4%==.srt (
SET _srt_file_long=%_file_long2%
) ELSE (
GOTO :no_srtfile
)
)

SET _mkv_name=%_mkv_file_short:~0,-4%
REN "%_srt_file_long%" "%_mkv_name%.srt"

GOTO :the_end

:_param_fout
ECHO Parameter fout
PAUSE && GOTO :the_end
:no_mkvfile
ECHO Geen MKV-bestand aanwezig
PAUSE && GOTO :the_end
:no_srtfile
ECHO Geen SRT-bestand aanwezig
PAUSE && GOTO :the_end

:the_end
EXIT
 

throoper

Trusted Advisor (deceased)
Joined
Jan 20, 2007
Messages
5,176
OK. I think this works (at least it does for me).
Remove the quotes from this line in the batch.
REN "%_srt_file_long%" "%_mkv_name%.srt"
so it instead reads
REN %_srt_file_long% %_mkv_name%.srt

In my test it took Test1.mkv and Test 2.srt and I ended with Test1.mkv and Test1.srt
 

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
Thanks very much. I have tried it straight away but unfortunately, I still get the error message.

BTW, I have rewritten my batch file, to be as lean as possible in order to have the minimum number of potential problematic statements. The result stays the same, though, both with and without the double quotes in the rename command. It is now this (with a few statements temporarily added for debugging purposes):

@ECHO OFF

ECHO First parameter is %1
ECHO Second parameter is %2
ECHO Third parameter is %3
PAUSE
ECHO ON

IF [%1]==[] GOTO :_param_error
IF [%2]==[] GOTO :_param_error
IF NOT [%3]==[] GOTO :_param_error

REM Get the .mkv file's name without path or extension
IF /I [%~x1]==[.mkv] (
SET _mkvfile=%~n1
) ELSE (
IF /I [%~x2]==[.mkv] (
SET _mkvfile=%~n2
) ELSE (
GOTO :_no_mkvfile_passed
)
)

REM Get the .srt file's name with path and extension
IF /I [%~x1]==[.srt] (
SET _srtfile_full=%1
) ELSE (
IF /I [%~x2]==[.srt] (
SET _srtfile_full=%2
) ELSE (
GOTO :_no_srtfile_passed
)
)

REN "%_srtfile_full%" "%_mkvfile%.srt"

GOTO :_the_end

:_param_error
ECHO Parameter fout
PAUSE && GOTO :_the_end
:_no_mkvfile_passed
ECHO Geen MKV-bestand aanwezig
PAUSE && GOTO :_the_end
:_no_srtfile_passed
ECHO Geen SRT-bestand aanwezig
PAUSE && GOTO :_the_end

:_the_end
PAUSE
EXIT

Do you get your different results with this one too?
 

throoper

Trusted Advisor (deceased)
Joined
Jan 20, 2007
Messages
5,176
I get the same results with your new code as I did with the old (it works) by changing the
REN "%_srtfile_full%" "%_mkvfile%.srt"
to
REN %_srtfile_full% "%_mkvfile%.srt"

I discovered the last part ("%_mkvfile%.srt") needed the quotes if the mkv filename has a space in it.
 

throoper

Trusted Advisor (deceased)
Joined
Jan 20, 2007
Messages
5,176
Just a thought.
If you're editing the original batch file, I wonder if there's a problem with the file itself rather than the code.
Maybe try creating a new batch file with the modification I made.
Other than that, I have no idea why it's working perfectly for me and not you.
 

raktg

Thread Starter
Joined
Jan 3, 2013
Messages
8
OK, I have created the test of tests, to eliminate any and all possibility that the actual contents of the batch file plays a role in the problem:

@ECHO OFF

ECHO First parameter is %1
ECHO Second parameter is %2
ECHO Third parameter is %3
PAUSE

This code is in two new files, in C:\Spaces_test_in_root_of_C.bat with a shortcut on the Desktop and C:\Documents and in Settings\RAK\Bureaublad\Spaces_test_on_Desktop.bat, directly on the Desktop without a shortcut.

Four small new dummy files: D:\File1_mkv_without_spaces.mkv, D:\File2 mkv with spaces.mkv, D:\File3_srt_without_spaces.srt and D:\File4 srt with spaces.srt.

Tests A) - Dragged onto C:\Spaces_test_in_root_of_C.bat via the shortcut
File names Result Passed in %1 Passed in %2 Passed in %3
1 with 3 -> correct 1 3 nothing
1 with 4 -> error msg unknown unknown unknown
2 with 3 -> error msg unknown unknown unknown
2 with 4 -> error msg unknown unknown unknown

Tests B) - Dragged onto C:\Documents and Settings\RAK\Bureaublad (=Desktop)
File names Result Passed in %1 Passed in %2 Passed in %3
1 with 3 -> correct 1 3 nothing
1 with 4 -> diff errror* unknown unknown unknown
2 with 3 -> diff errror* unknown unknown unknown
2 with 4 -> diff errror* unknown unknown unknown

* This error message is: C:\Documents is not recognized as an internal or external command, program or batch file (in Dutch)

Interesting (well...) I think there really IS a problem with the passing on of file names containing one or more spaces. The Tests B indicate that too, although with a different message. The "C:\Document " is probably the first part, up to the first space, of "C:\Documents and Settings\RAK\Bureaublad", the desktop. The only reference to C:\Documents and Settings\RAK\Bureaublad is in the properties of the batch file on the desktop, under "Location". Perhaps this is a clue for the Experts, but I certainly have no idea what causes it. For now, I think it is probably best to ignore the Tests B results. Agree? Any other thoughts? And are you using Windows XP Pro SP3 too?
 

throoper

Trusted Advisor (deceased)
Joined
Jan 20, 2007
Messages
5,176
I honestly have no idea why you are getting different results than I am, so I'm thinking to give you the step by step of what I've done and have you duplicate it and see if you get the same results as I do.

Step1: Create a folder for the test. Mine is "C:\Documents and Settings\Smith\My Documents\new stuff\batch test".

Step2: Create the batch file. I simply opened a Notepad and pasted the following in to it, saved it as rename.bat and saved it to the batch test folder.

Code:
@ECHO ON

IF [%1]==[] GOTO :_param_fout
SET _file_long1=%1
SET _file_short1=%~nx1
IF [%2]==[] GOTO :_param_fout
SET _file_long2=%2
SET _file_short2=%~nx2
IF NOT [%3]==[] GOTO :_param_fout

IF /I %_file_short1:~-4%==.mkv (
SET _mkv_file_short=%_file_short1%
) ELSE (
IF /I %_file_short2:~-4%==.mkv (
SET _mkv_file_short=%_file_short2%
) ELSE (
GOTO :no_mkvfile
)
)

IF /I %_file_short1:~-4%==.srt (
SET _srt_file_long=%_file_long1%
) ELSE (
IF /I %_file_short2:~-4%==.srt (
SET _srt_file_long=%_file_long2%
) ELSE (
GOTO :no_srtfile
)
)

SET _mkv_name=%_mkv_file_short:~0,-4%
REN %_srt_file_long% "%_mkv_name%.srt"

GOTO :the_end

:_param_fout
ECHO Parameter fout
PAUSE && GOTO :the_end
:no_mkvfile
ECHO Geen MKV-bestand aanwezig
PAUSE && GOTO :the_end
:no_srtfile
ECHO Geen SRT-bestand aanwezig
PAUSE && GOTO :the_end

:the_end
PAUSE
Note that I had ECHO ON and used PAUSE to display the process and keep the command window open after.

Step3: Create 2 files for the test. I did this by right clicking in the batch test folder and selecting New>Text Document.
I named the first Test1.mkv (OKed the warning about changing the file type from txt to mvk).
I named the second Test file1.srt (also oking the warning and having the needed space for the test).

At this point I have the rename.bat file and the 2 test files Test1.mkv and Test file1.srt in the folder batch test.

Step 4: Holding the Ctrl key, I selected the test files. Released the Ctrl key and drag and dropped the 2 files onto the batch file.

The result was the Command opening and running the commands, ending with the file Test file1.srt being renamed to Test1.srt.

I've copied and pasted the contents of the Command window to the attached text file so you can see the progression.

To check that the location of the batch doesn't make a difference, I repeated the test using a shortcut to it on my desktop and also by moving the file itself to the desktop. In both cases I got the same results with the same output as I expected it to be.
 

Attachments

foxidrive

Banned
Joined
Oct 20, 2012
Messages
793
This style of syntax needs to be implemented:

if "%~1"=="" GOTO :_param_fout
SET "_file_long1=%~1"
SET "_file_short1=%~nx1"
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top