Unable to drag and drop a filename containing spaces onto a

January 4, 2013 at 05:10:01
Specs: Windows XP
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

See More: Unable to drag and drop a filename containing spaces onto a

Report •


#1
January 4, 2013 at 06:42:48
Because the space is treated as a delimiter. You need to enclose the file-name in double quotes before you do any processing on it.

Stuart


Report •

#2
January 4, 2013 at 15:08:31
Thanks for your reply. I think I need to explain some more: There is no way that I can control how the filename is treated. I only highlight 2 files in a Windows Explorer window (where they are not in quotes) and DRAG AND DROP them onto my batch file. Windows is the guy who converts the selected filenames into parameters and passes them to the destination program, in this case a batch file as %1 and %2 but it could be any program, as probably all programs accept parameters from a "calling" program (which in this case is Windows itself). I read elsewhere on the web that Windows is supposed to enclose the filename in double quotes, if - and only if - it sees that the filename contains a space character, BUT IT DOESN'T! (Despair here). Perhaps my Windows is broken and I need to reinstall. :-((

Report •

#3
January 4, 2013 at 16:15:07
It doesn't always enclose the file in double quotes, it depends where the file came from. You need to insert the double quotes in the batch file before it attempts the rename function.

Stuart


Report •

Related Solutions

#4
January 4, 2013 at 16:54:04
Thanks again. But if you open the Windows Explorer and and look at any filename, you will see that there are no double quotes. Anywhere. And you could not get them in there, by any means, because the names of the files on the disk are what they are, most without any spaces in them, but some with.

The origin of the files does not matter; at the time of the DRAG AND DROP (this is the crucial aspect: the drag and drop) the files are on the hard disk and Windows passes the fileNAMES as parameters on to the program that the filenames are dropped on. Filenames containing one of more spaces it should enclose in double quotes before passing them on, but it doesn't. At least not on my pc.

There is nothing I can do in my batch file, because the error (please see my original posting) occurs BEFORE the very first line is loaded, interpreted and executed.


Report •

#5
January 4, 2013 at 18:29:40
Try this instead go to a command line. (Navigate to the directory your batch and the two files are in) type

Batch name.bat "firstname" "second name"

:: mike


Report •

#6
January 4, 2013 at 18:36:39
The whole idea is that, by using drag and drop, I avoid having to type the filename(s)... That is why I wrote the batch file in the first place. ;-)

Report •

#7
January 5, 2013 at 02:41:01
Does the error occur before of after the drop. If it happens before the drop, which I doubt, there is little you can do about it except follow mikielinus's suggestion. If the error occurs after the drop, then ou edit the batch file.

A batch file is is a simple text file. Any text editors will do it. All you have to do is insert double right after the bit of code that relieves the drop.

Stuart


Report •

#8
January 5, 2013 at 03:53:57
Two posts back I wrote "There is nothing I can do in my batch file, because the error (please see my original posting) occurs BEFORE the very first line is loaded, interpreted and executed."

Until I have actually dropped, i.e. let go of the left mouse button after the files are on top of the batch file's icon, nothing has happened yet. The dragging is in process. The drag and drop action is not yet complete and I could e.g. press Esc or drag the files elsewhere. So immediately after the drop, Windows attempts to pass control to the batch file. But although a cmd-window is opened, the batch file never starts. Because of the error.

So in summary, 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 a blank line, followed by a line with the command prompt e.g. C:\path>

Should anyone want to try this 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


Report •

#9
January 5, 2013 at 06:05:07
It works on a spaced .mkv file here, but not if the .srt file has a space. Win7. If you can drag over a spaced filename successfully within Explorer, I would say Windows' drag and drop function is working. There's a difference is the logic between the .mkv and .srt blocks. Is that intentional?

Report •

#10
January 5, 2013 at 06:16:14
I made these changes, and it seems to be working here whether spaces are present in either file name or not. I really don't know why it's working though.

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

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


Report •

#11
January 5, 2013 at 06:55:00
Thank you for participating. The batch file is to always name the .srt file after the .mkv file. The first argument of the rename command has to be "long", because the batch file itself never runs from the map where the files are. So the rename must have the format: ren "%srtfilename-INCLUDING-driveletter-and-path-and-extension%" "%mkvfilename-WITHOUT-driveletter-and-path-and-extension%.srt", if you see what I mean. Could you please give it another try and tell me what you find?

Report •

#12
January 5, 2013 at 12:29:40
Believe me, I wish I had your batch knowledge. But before the changes I made, the spaces were no problem in the .mkv file but were when in the .srt file. After I made my change everything works perfectly. Here's a result grid:

1: tex1.mkv, text1.srt (no spaces) => tex1.mkv, tex1.srt

2: tex 1.mkv, text1.srt (space in .mkv file) => tex 1.mkv, tex 1.srt

3: tex1.mkv, text 1.srt (space in .srt file) => tex1.mkv, tex1.srt

4: tex 1.mkv, te xt 1.srt (space in both files) => tex 1.mkv, tex 1.srt

I don't know why that would be different from your original, as the logic seems to be the same. Are there some reserved variable names that were being trampled on?


Report •

#13
January 5, 2013 at 15:46:57
I have rewritten the batch file, making it as lean as I could. It follows below, with a few statements temporarily added for debugging purposes. My latest test were: - Test1.mkv with Test2.srt -> works as intended; - All other combinations -> same error as before.
However, there was ONE occasion just now with only one of the file names containing a space that resulted in the batch file running (really! I am certain) and displaying the file name without the space as parameter1 and parameter2 as empty. Very strange indeed and I was unable to repeat it. Perhaps it has to do with which of the dropped files is processed first; if it is the one without the space then the batch file does run but is passed only that one, but the other file name does not make it to the parameter list, so it reports that only one file name was passed, although two were dragged and dropped. Regardless, still no luck here when there is a space in (at least) one of the file names.

Detail: I drop the file names onto a shortcut on the desktop that points to the batch file that lives in C:\Batchfiles. The two files are both in E:\uTorrent\Downloads\Complete\Films.

@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


Report •


Ask Question