BATCH programming: Getting dirname without trailing backslash

  • Thread starter Thread starter Ronny
  • Start date Start date
R

Ronny

Suppose I have a variable FILE which contains the full path to a file.
To store the name of the directory where this file is stored, I use in
my batch file the following assignment:

for %%F in ("%FILE%") do set DIR=%%~dpF

This works, but %DIR% contains the directory name with a trailing
backslash
added. Is there an easy way to get the directory name WITHOUT the
backslash?

For instance, if FILE is \\share\c:\foo\bar\baz.txt, I would like DIR
to contain
\\share\c:\foo\bar and not \\share\c:\foo\bar\
 
In
Ronny said:
Suppose I have a variable FILE which contains the full path
to a file. To store the name of the directory where this
file is stored, I use in my batch file the following
assignment:

for %%F in ("%FILE%") do set DIR=%%~dpF

This works, but %DIR% contains the directory name with a
trailing backslash
added. Is there an easy way to get the directory name
WITHOUT the backslash?

For instance, if FILE is \\share\c:\foo\bar\baz.txt, I
would like DIR to contain
\\share\c:\foo\bar and not \\share\c:\foo\bar\

Use Find to see if the last character is a back slash (or a forward flash,
for that matter). If it's there, remove it by truncating.

alt.msdos.batch.net has some real experts there. They're great for
these kind of questions but be polite and professional.

HTH,

Twayne`
 
Back
Top