1

因此,我多次尝试寻找解决方案已经导致一百万种方法来查找正在执行的 bat 文件的文件夹,但是我想要做的是找到传递给 bat 文件的文件名的文件夹。

例子:

C:\Temp\runthis.bat "C:\Blah\Ah Argh\rage.txt"

我想在那个 bat 文件中得到一个简单的字符串“C:\Blah\Ah Argh\”,或者我也可以得到一个字符串“rage.txt”

编辑解释原因:查看另一个 txt 文件中的文件名,该文件是 ftp 服务器的目录列表,以验证文件是否成功上传到它。然后,如果成功,我需要将文件移动到原始文件夹 \uploaded\ 的子文件夹,但我们设置了许多这些文件夹,因此我无法对其进行硬编码。

谢谢

4

2 回答 2

3
@echo off
The file path is %~dp1
The file name is %~nx1

参数修饰符与 FOR 变量相同。

在命令提示符下键入“HELP CALL”以获取参数修饰符的完整列表。

于 2012-12-08T00:50:26.833 回答
1
@echo off
if %1X==X echo Syntax: %0 "path"

rem  The for loop doesn't actually loop. You can split strings with it, but in 
rem  this case we don't. So there is only one iteration in which %%X will 
rem  contain the full path.

rem  Pass it %1, which is the first parameter. Note the quotes, which are 
rem  required if you don't add quotes around the parameter and optional (but 
rem  still valid) when you do.

for /F "delims=|" %%X in ("%1") do (

  rem  FOR LOOP variables can be used with certain modifiers, preceeded by a 
  rem  tilde. In this case I'm using d and p, which stand for drive and path,
  rem  effectively trimming the file name from the path.

  echo %%~dpX

  rem The ~n modifier selects the file name only. ~x is for extension

  echo %%~nxX
)
于 2012-12-07T23:52:20.000 回答