1

My path is pointed towards a cloud folder (located at various places on my various devices) wherein I have stored a batch file that is supposed to locate itself on the machine, and then make that directory the new working directory of that session.

Locating the file

@echo off No clutter

cls clean slate

Echo Searching Because sometimes it takes a while

dir /b /s myScripts.bat And this outputs the full directory, including the file name.

So my first problem is getting rid of the file name. This is what I've tried:

set stringOne=dir /b /s myScripts.bat

%stringOne:~0, 60%

I found a script for counting the length of a string which I'll use to automatically deduce those values later if it turns out I actually need them.

Problem is I've found that if I lower that number to 10, the dir command is shortened like so:

dir c:\users\myUsername\cloudDrive\scripts, or whatever the case may be, and instead cmd inputs dir c:\users\m and searches ALL of that recursively.

Again, what I'd like to do is have it show me a modified version of the directory it searched, NOT modify the directory search command. I'll later use the final string produced to set my working directory to, but I'm trying to take this one step at a time so I understand what is happening.

EDIT: The ultimate goal of the SCRIPT is different from my purpose for writing it. When all is said and done I should be able to type myScripts.bat into cmd, on any computer which I have appended to it's PATH my cloud folder, and it will procure the directory of that cloud folder via means of the dir command, and then change it's working directory to that of the batch file.

5
  • Can you add the ultimate goal of this script, because your approach seems to be wrong. I think you'll need to use FOR for this, but as I'm not entirely sure what you're after I can't write you an answer just yet. – LPChip Nov 20 '15 at 20:56
  • Basically I'm just trying to get better at manipulating data in cmd. Cmd can be so ugly what with the pipelines and $'s vs %var%'s and whatever /t /h /e /s /e -[things[are]]. So anyway, the variable directory of my appended path folder can be found with dir /b /s myScripts.bat. I discovered that much myself -Now that I have that information I just need to be able to modify it for input elsewhere. – Musixauce3000 Nov 21 '15 at 2:45
  • Much easier, %0 contains the path of your batch file and filename. %cd% contains just the current path. – LPChip Nov 21 '15 at 14:54
  • *opens cmd.exe *types dir /b /s myScripts.bat *types %0 >>> %0 is not recognized as an internal or external command, operable program or batch file. – Musixauce3000 Nov 21 '15 at 15:03
  • %0 only works inside batch files. – LPChip Nov 21 '15 at 15:05
1

To answer this part:

And this outputs the full directory, including the file name. So my first problem is getting rid of the file name.

In a batch file you can do

@echo off

:: locate 'myScripts.bat' and save its directory to 'myDir'
:: in case of multiple matches 'myDir' will be set to the last one
set "myDir="
for /f "delims=" %%x in ('dir /b /s myScripts.bat') do set "myDir=%%~dpx"
if not defined myDir echo not found ?? & exit /b 1

:: ready to use
echo myDir = "%myDir%"

To get a better hang of batch files, I recommend that you read the builtin help, starting with set /?, if /?, for /?, call /?.

[ EDIT ] Added "delims=" to handle paths with spaces correctly.

8
  • I have experience programming. I know how to use loops and variables, conditional statements and references, cmd's built in help is just particularly unhelpful, like most things windows based. To clear things up could you just exlpain how that for loop works? particularly the f parameter, the double modulo-x, the tilda-dpx, the not defined, the special characterless 'myDir', and the double question mark. – Musixauce3000 Nov 21 '15 at 14:51
  • Far too complicated solution for something that can be achieved much easier IMHO. But it does show the true power of the FOR command. (note: for /? gives a good set of help for how all these %%~dpx etc work.) – LPChip Nov 21 '15 at 15:11
  • @Musixauce3000 This for /f executes the command inside single quotes then parses its ouput one line at a time. The %~dpx extracts the drive and directory part of the full path, and saves it to the myDir variable. See for /? for all the details. Also, if you comment out the first line @echo off then you can see the entire batch execution step by step, which is often a handy way to debug or understand what's going on. – dxiv Nov 21 '15 at 17:46
  • @LPChip if you take out the comments, the error checking and the echo then the actual code is a one liner, and it does what the OP had asked for. – dxiv Nov 21 '15 at 17:48
  • Yes, I know, but it still is a much more complicated solution and takes time, while using %0 and strip it down to its basic is lightning fast as seen in my example. – LPChip Nov 21 '15 at 20:39
1

Lets say, you create a batch file called test.cmd, and you place the following text in it, the output will be as follows:

@echo off
echo %0
echo %cd%

set currentpath=%0

::remove any " if present
set currentpath=%currentpath:"=%

:: remove last 9 characters
set currentpath=%currentpath:~0,-9%

:: add quotes back
set currentpath="%currentpath%"

echo %currentpath%

pause

and the output will be if:

                C:\Windows\System32>%appdata%\..\..\desktop\test.cmd
%0:             C:\Users\LPChip\AppData\Roaming\..\..\desktop\test.cmd
%cd%:           C:\Windows\System32
%currentpath%:  "C:\Users\LPChip\AppData\Roaming\..\..\desktop"
                Press any key to continue . . .

The first command uses a replace function. The ~ operator means, substring replacement, by adding 0, the new string starts at position 0, the -9 means, to 9 positions from the right.

Of course the quickest and easiest way is still using %cd% to get the current directory, but as you want to find the current path of where the batchfile itself resides, the other method does just that.

A great resource for learning more about string manipulation is this site: dostips.com

1
  • Or simply set "currentpath=%~dp0". – dxiv Nov 21 '15 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.