Bat文件查找文件夹

时间:2012-12-07 22:07:13

标签: batch-file

因此,我搜索解决方案的许多尝试都导致了一百万种方法来查找正在执行的bat文件的文件夹,但我要查找的是找到传递给bat文件的文件名的文件夹。

示例:

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

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

编辑解释原因:寻找另一个txt文件中的文件名,该文件是ftp服务器的目录列表,用于验证文件是否已成功上传到该文件。然后,如果成功,我需要将文件移动到原始文件夹\ uploaded \的子文件夹,但我们设置了许多这些文件夹,所以我不能硬编码。

由于

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分: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
)