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]