delete spaces from a filename before using it as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dwilbank
Posts: 6
Joined: 10 Nov 2010 11:18

delete spaces from a filename before using it as variable

#1 Post by dwilbank » 10 Nov 2010 11:30

Hi.
The programmer here at work made a script where we can right click a video file and have it converted to some other format.

The procedure right now is to have no spaces in the filename.

I want to change that, even though I heard it was complicated.

Here's what I was given.

set fileonly=%~n1
set filename=%~dpn1
set filedir=%~dp1

Now I am only at the beginning of my batch script lessons, but I thought I could just add one new line at the beginning to take care of matters.

set n1=%n1: =_%
set fileonly=%~n1
set filename=%~dpn1
set filedir=%~dp1

Apparently I don't understand yet what the tilde is doing in his original code, but where am I going wrong?

And is the n1 the assumed result of a right click selection?
I never saw it set as a variable anywhere else...

Thanks for any help

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: delete spaces from a filename before using it as variabl

#2 Post by amel27 » 12 Nov 2010 02:26

you must add dbl quotes in all commands with file names, if not present:

%~n1 -> "%~n1"
%~dpn1 -> "%~dpn1"
%~dp1 -> "%~dp1"

dwilbank
Posts: 6
Joined: 10 Nov 2010 11:18

Re: delete spaces from a filename before using it as variabl

#3 Post by dwilbank » 15 Nov 2010 15:43

Thx but there is something more mysterious going on with these variables...
It didn't find the file at all...
I'll have to study some more...

Post Reply