批处理:将变量和字符串连接起来以形成输出路径 [英] Batch: concatenate variable and string to form output path

查看:494
本文介绍了批处理:将变量和字符串连接起来以形成输出路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,用户在其中输入文件的路径,然后从该路径中提取文件名.

I have a batch file where a user enters a path to a file and I extract the filename form that path.

我使用该文件名来创建一个具有相同名称的文件夹.

I use that filename to create a folder of the same name.

在该文件夹中,我想创建一个log.txt文件,我在批处理文件中调用的进程可以将其日志消息写入其中.

In that folder, I want to create a log.txt file that the process I am calling in my batch file can write its log messages to.

代码如下:

set /p pathFoFile="Enter path fo file: "
FOR %%i IN ("%pathFoFile%") DO (
    set outputFolder=%%~ni
)

someprocess -p1 blabla -p2 blabla -p3 %outputFolder% > %outputFolder%\log.txt

最后一部分%outputFolder%\log.txt似乎是问题所在,因为当我刚放置> log.txt

The last part, %outputFolder%\log.txt, seems to be the problem, since it works when I just put > log.txt

但是,当我回声:

echo %outputFolder%\log.txt

它会打印正确的路径.

如何使用文件夹名称并创建此log.txt文件?

How can I use the foldername and create this log.txt file?

编辑

我得到的路径可能是这样的:

The path I get may look like this:

..\some\folder\thefile.egf

或者只是

thefile.egf

或绝对路径.

我提取

thefile

,并希望致电:

someprocess -p1 blabla -p2 blabla -p3 thefile > thefile\log.txt

应相对于批处理文件创建包含log.txt的文件夹thefile.路径中没有驱动器号.

the folder thefile containing log.txt should be created relative to the batch file. There are no drive letters in the path.

使用%outputFolder%\log.txt"%outputFolder%\log.txt"时出现的错误是系统找不到路径.

The error I get when using %outputFolder%\log.txt or "%outputFolder%\log.txt" is that the system cannot find the path.

推荐答案

如果路径中有空格,请使用引号.

Use quotes in case you have spaces in path.

someprocess -p1 blabla -p2 blabla -p3 "%outputFolder%" > "%outputFolder%\log.txt"

这篇关于批处理:将变量和字符串连接起来以形成输出路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆