0

I'm trying to search a string (entered by the user when prompted for a path) for a trailing quote and delete it if found.

The problem: I have a bat file that prompts the user to enter a filename and path (the first is typically done via drag/drop.) If the user enters a destination path enclosed in quotes because it contains spaces, my resulting command will look like this: compress.exe "c:\source path\"destination.zip"

That extra quote in the middle needs to go. I've found plenty of ways to search a file for a string, and I found this post here on StackOverflow that seems to apply, but doesn't seem to work in my situation.

I tried the command at the above linked path, telling it to search for \" instead of bcd, but the code expects the string it's searching to have been passed to it (as a switch) upon execution, and when I try to modify the command to search srcpath instead, the bat fails. I also tried this:

if "!srcpath:~-1"=="\"" set srcpath=!srcpath:~0,-1!

This results in: "The syntax of the command is incorrect."

How can I search a string for a trailing quote and trim it? Every method I can find doesn't seem to work when the character being searched for is the quote (slash quote: \").

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Mugsy
  • 23
  • 1
  • 6
  • 1
    If you know they are putting a leading and trailing quote just strip both of them. `for /f "useback tokens=*" %%G in ('%srcpath%') do set srcpath=%%~G`. Then your other command looks like this: `compress.exe "%srcpath%destination.zip"` – Squashman Oct 23 '18 at 03:31
  • Nice Squashman.. I too thought of this. ;^) I showed my 'hack way' because it can be used to strip all sorts of things where "~" only strips quotes. – Señor CMasMas Oct 23 '18 at 15:36

2 Answers2

0

I use a SLOPPY batch trick when working with paths just for this reason. There are more elegant ways but this works just as well and I don't ever have to remember how I pulled it off.

First of all, 90% of the time.. we just want ALL quotes stripped.. that would just be:

set srcpath=%srcpath:"=%

Then tack quotes on to the front and back of your new concatenated path string.

But if you REALLY only want the trailing one.. here is my stupid bash trick..

1. Add a "? to the end of your string
2. Then delete ""? from that string
3. Then delete "? from that string

Wella! Never fails! The trick here is that a question mark isn't a valid path char.

:: SAMPLE -- Just a trailing quote removed
set srcpath="c:\source path\"
set srcpath=%srcpath%"?
set srcpath=%srcpath:""?=%
set srcpath=%srcpath:"?=%
echo srcpath=%srcpath%

::You can use the SAME TRICK to remove a trailing backslash
Señor CMasMas
  • 4,290
  • 2
  • 14
  • 21
  • Thanks. The "=% method appears to work. I'm not quite sure how or what it does, but as long as it works. Thanks. :) – Mugsy Oct 23 '18 at 20:08
  • You are welcome but I expected to get the check since I submitted the same exact solution the day before. – Señor CMasMas Oct 23 '18 at 21:44
  • How does it work? %MY_VARIABLE:SEARCH_THING=REPLACE_THING% In this case REPLACE_THING was nothing so :"=% is replace quote with nothing. – Señor CMasMas Oct 23 '18 at 21:45
  • Sorry on the Check I couldn't tell who replied first. The one I checked said "one day ago" while your said "18 hours ago". – Mugsy Oct 24 '18 at 22:59
0

Would this work for you?

SET /P "%SRCPATH=Enter the source path "
ECHO %SRCPATH%
ECHO %SRCPATH:"=%
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thanks. This works. Is the closing **%** part of the method or can I use **"=** elsewhere? – Mugsy Oct 23 '18 at 20:10
  • The final % is required to specify the end of the variable. The third (3rd) line does a string replacement of `"` with nothing. I am not sure what you mean by "use = elsewhere." – lit Oct 23 '18 at 22:32