【发布时间】:2019-02-25 03:00:06
【问题描述】:
我有一个 .bat,它调用 .vbs 脚本以返回更改目录命令,例如
cd /D ../../some/determined/path
但问题似乎是,虽然.bat文件正在改变目录,但一旦执行结束,命令提示符仍然在它开始的地方。
@echo cd_command: !cd_command!
cd !cd_command!
REM Next line displays expected files for said directory
dir
我的最终目标是让原始调用命令提示符更改目录。
如何让调用命令提示符通过 .bat 文件实际更改目录?
更新
似乎我说得不够清楚,既然我打算公开发布代码,我不妨将其完整发布在这里。 (尽管出于某种原因担心这样做?)
我的最终目标是:输入 Win+R 打开运行命令。键入 cmd 以获取提示。然后键入这个 bat 文件(将添加到环境变量中),这样我就可以执行以下操作:
C:\Users\Mallow> cdj 20.2
C:\Users\Mallow\Documents\20-29 Areas\20 Category\2 Id>
我有这个 Bat 文件
@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
还有这个 VBS 脚本:
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
VBS 脚本使我可以将传递的参数解释为数字(因为它始终是格式为 [00 或 00.00] 的整数或浮点数
【问题讨论】:
-
打扰一下:您希望批量将当前文件夹更改为另一个文件夹,我们将其称为 x 文件夹。然后你想运行一些依赖于这个 x 文件夹的东西。那是对的吗?我错过了什么?
-
为什么在主要使用
%SystemRoot%\System32\cscript.exe(控制台版)或%SystemRoot%\System32\wscript.exe(Windows GUI 版)执行的VBScript 时使用批处理文件。也可以直接从 Visual Basic 脚本中更改当前目录。我想您在命令行上方使用setlocal和cd。在这种情况下,显式或隐式调用endlocal会恢复初始当前目录。有关命令 SETLOCAL 和 ENDLOCAL 的详细信息,请阅读this answer。 -
您正在扩展变量,但我没有看到您已启用
delayedexpansion。你能发布更多你的代码吗? -
换句话说,您需要在命令行上方使用
cd /D "%~dp0..\..\some\determined\path"和setlocal来永久更改相对于批处理文件路径的当前目录路径。请注意,这仅适用于以驱动器号开头的路径。如果批处理文件存储在使用 UNC 路径访问的网络资源上,命令 CD 不会更改目录,因为默认情况下 Windows 会阻止 UNC 路径访问的目录成为当前目录。请进一步注意,在 Windows 上,\是目录分隔符,/用于参数。 -
等等 - 你的意思是你从命令提示符启动批处理文件,批处理文件更改目录,但 cmd 仍然在你启动批处理文件的目录中?
标签: windows batch-file command-prompt