|
Answer» Well if you know that a batch file named "MyBatchFile.bat" in the location "E:\MyFolder\" will store its full path in the %0 parameter and have a value of E:\MyFolder\MyBatchFile.bat then let's get started. All i really want to do is get the path of the batch file open but I've NOTICED that 0% changes its format depending on its location. If your the path to your batch file has a SPACE some where in one of the folder names the value of 0% will have a quotes around the path but won't have quotes if there isn't a space. "E:\My Folder" or E:\MyFolder So I'm trying to test %0 with IF statements and it always errors on me if the path has a space in it and I cannot seem to get past this no matter what i do.
For Example this batch file code should end and do nothing if its placed in a directory with no spaces but will error if there are spaces. How can I get around this?
SET A=%0 IF "%A%" == "B" ECHO ITS BBBBB
so , you want to remove the quotes from short file names but keep them in long file names? quotes work on both long file and short file names...If I read this correctly, you're not sure if you have a short name or a long name. One way would be to force quotes in the name and then reduce any double quotes to single quotes. As the previous poster mentioned, quotes work with short names too.
Code: [SELECT]@echo off setlocal enabledelayedexpansion for /f "tokens=* delims=" %%i in ("%0") do ( set A=%%i set A=!A:""="! set A=!A! )
Hope this helps.
Note: The setlocal command reduces the scope of any variables to the life of the batch file.
What OS are you using? I am using XP Pro SP2 and I see no quotes if I use the ~f variable modifier. Am I missing something or have I misunderstood the question?
Quote @echo off echo this is my full path and name: %~f0
Quote this is my full path and name: D:\Test\locate batch\I have spaces in my name and path.bat
type FOR /? at the prompt for details of variable modifiers
Quote %~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a DRIVE letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file
diablo416 – Incorrect, I know that.
Sidewinder – what you said made sense, but your code was a little over my head.
Dias de verano – That is exactly what I needed!! I would have NEVER looked inside FOR command for help because I wasn’t using a FOR loop. I was just using a parameter of the batch file. I mean I know about %0 %1 %2 %3 etc. but not all of the ones you listed and are in the FOR /? And I wouldn’t have thought they could be used anywhere. So I just ended up using %~d0%~p0 but %~f0 did the job also and I would have used it but %~d0%~p0 made it even easier then I would have thought and didn’t require extra code to trim the file name off.
THANKS SO MUCH!! Quote from: MikeBAM on February 23, 2008, 01:35:16 PMbut %~d0%~p0 made it even easier then I would have thought and didn’t require extra code to trim the file name off. You can combine them, so %~d0%~p0 can be contracted to %~dp0
If your goal is to simply take away the quotes, you could just use %~0. Like: Code: [Select]SET A=%~0 IF "%A%" == "B" ECHO ITS BBBBB
|