0

I have a .bat that calls a .vbs script in order to return a change directory command such as

cd /D ../../some/determined/path

But the problem seems that although the .bat file is changing directory, once execution ends, the command prompt remains where it started.

@echo cd_command: !cd_command!
cd !cd_command! 

REM Next line displays expected files for said directory
dir

My end goal is to have the original calling command prompt to change directory.

How could I make the calling command prompt actually change directory through the .bat file?

Update

Seems I'm not being clear enough, and since I intend to release the code publicly I might as well post it here in full. (Although apprehensive about doing so for some reason?)

My end goal is this: I type in Win+R to open run command. Type cmd to get the prompt. and then type this bat file (which would be added to the environmental variables) so that I can do something like:

C:\Users\Mallow> cdj 20.2
C:\Users\Mallow\Documents\20-29 Areas\20 Category\2 Id>

I have this Bat file

@echo off
setlocal enabledelayedexpansion

REM Check only one parameter was passed
if not "%~2"=="" (
    echo No more than two arguments, please
    goto :eof
)

if not "%~1"=="" (
    echo "1"
REM     REM Use vbscript to interpret float string. 
    FOR /F "usebackq tokens=*" %%r in (`CSCRIPT //NoLogo "cdj.vbs" %~1`) DO SET johnny_decimal_command=%%r
    echo command:
    @echo johnny_decimal: !johnny_decimal_command!

    REM This variable is the full command
    !johnny_decimal_command!

    REM Tests the output to see if the directory did change script level
    REM dir

) else (
    echo "2"
REM     REM No parameters passed
REM     echo Must pass in Johnny Decimal id. Example: 12.01 or 12; [Category.Optional ID]
REM     goto :eof
)

REM REFERENCES:
REM https://stackoverflow.com/questions/17880183/pass-value-from-vbscript-to-batch
REM https://stackoverflow.com/questions/15129085/how-to-return-a-string-from-a-vbscript-which-is-executed-from-a-python-file
REM https://stackoverflow.com/questions/1497985/batch-checking-the-number-of-parametersfg
REM https://stackoverflow.com/questions/3949978/why-does-this-batch-variable-never-change-even-when-set#3950000
REM https://stackoverflow.com/questions/21013428/pass-variable-from-batch-to-vbs

And this VBS Script:

Dim base_path
base_path = "C:\Users\Mallow\Documents\"

' Get passed arguments
dim oParameters
dim johnny_decimal

Set oParameters = WScript.Arguments
WScript.echo Main(oParameters.Item(0))

' Generates Johnny Decimal Command
Function Main(ByVal johnny_decimal)
    dim is_number
    dim has_category
    dim has_id
    dim johnny_command

    ' Check cdj is numeric
    if isnumeric(johnny_decimal) Then
        is_number = True
    else
        johnny_command = "echo Johnny Decimal passed is not a numerical value."
    end if


    if is_number then
        ' Check if number passed is integer or float
        if instr(1, johnny_decimal, ".") > 0 then
            has_category = True
            has_id = True
        else
            has_category = True
        end if
    end if

    if has_category Then
        dim area, category, id

        ' Prepare template for command
        johnny_command = base_path & "{area}*\{category}*\"

        ' Calculate Category and Area
        category = int(johnny_decimal)
        area = int(left(johnny_decimal,1) & "0")

        ' Put info into command
        johnny_command = replace(johnny_command, "{area}", area)
        johnny_command = replace(johnny_command, "{category}", category)

        If has_id Then

            ' Prepare template for command
            johnny_command = johnny_command & "{id}*"

            ' Calculate ID
            id = int((johnny_decimal - category) * 100)

            ' Make sure id has leading zero
            if len(id) = 1 then
                id = "0" & id
            end if

            ' Put info into command
            johnny_command = replace(johnny_command, "{id}", id)                    

        End If

        ' Prepend with change directory command and encapsulate in quotes
        johnny_command = "cd /D " & chr(34) & johnny_command & chr(34) 

    End If

    Main = johnny_command
End Function


'Set File = FSO.OpenTextFile(WScript.Arguments(0) &"\test.txt", 2, True)
' http://stackoverflow.com/questions/2806713/ddg#2806731

The VBS Script is so that I can interpret the passed arguments as numbers (as it would always be an integer or float of format [00 or 00.00]

Mallow
  • 844
  • 1
  • 13
  • 37
  • Excuse me: you want a batch to change your current folder to another one let's call it **x** folder. Then you want to run something which relies on this **x** folder. Is that correct? Anything I missed? – statosdotcom Feb 25 '19 at 03:15
  • Why do you use a batch file at all on using mainly a VBScript executed by `%SystemRoot%\System32\cscript.exe` (console version) or `%SystemRoot%\System32\wscript.exe` (Windows GUI version). Changing the current directory is also possible directly from within the Visual Basic Script. I suppose that you are using `setlocal` above the command line with `cd`. In this case explicitly or implicitly called `endlocal` restores initial current directory. Please read [this answer](https://stackoverflow.com/a/38676582/3074564) for details about the commands __SETLOCAL__ and __ENDLOCAL__. – Mofi Feb 25 '19 at 05:54
  • 2
    you are expanding on the variable, but I do not see that you have enabled `delayedexpansion`. Can you post some more of your code please? – Gerhard Feb 25 '19 at 05:56
  • In other words you need `cd /D "%~dp0..\..\some\determined\path"` above the command line with `setlocal` to change the current directory path relative to path of batch file permanently. Please note that this works only for paths starting with a drive letter. In case of batch file is stored on a network resource accessed using UNC path, the command __CD__ does not change the directory as by default Windows prevents that a UNC path accessed directory becomes the current directory. Please note further that on Windows ``\`` is the directory separator and `/` is used for parameters. – Mofi Feb 25 '19 at 05:57
  • Wait - you mean you are starting the batch file from command prompt, the batch file changes directory, but cmd remains in the directory where you started the batch file? – double-beep Feb 25 '19 at 16:58
  • Hey y'all, thanks for commenting on my question. I've updated my question to include code and expected behavior. – Mallow Feb 26 '19 at 01:14

1 Answers1

0

This fixed it

@echo johnny_decimal: !johnny_decimal_command!

!%johnny_decimal_command!
PushD .
EndLocal
Popd

Credit: SETLOCAL ENABLEDELAYEDEXPANSION causes CD and PUSHD to not persist Was easier for me to understand than the comment pointing to the node.js even though it comes out to the same things.

Mallow
  • 844
  • 1
  • 13
  • 37