问题描述
我正在尝试在文件中写入命令行的第一个参数,但是它可以在命令行中使用,而不能通过拖放操作来实现.
I'm trying to write the first argument of a command line in a file, but it works in command line and not with drag and drop.
非常简单的批处理文件(test_echo.cmd
)如下:
The very simple batch file (test_echo.cmd
) is as following:
@echo OFF
echo %1 > echo_arg_in_file.txt`
在命令行上,
C:\rep>test_echo.cmd "C:\rep\fichier de test.txt"`
创建一个文件echo_arg_in_file.txt
,其中将"C:\rep\fichier de test.txt"
写入其中.
creates a file echo_arg_in_file.txt
with "C:\rep\fichier de test.txt"
written inside.
但是通过将文件"C:\rep\fichier de test.txt"
拖放到批处理文件上,没有任何反应...(删除> echo_arg_in_file.txt
的测试之前已经完成,并且显示得很好"C:\rep\fichier de test.txt"
)
But with a drag and drop of the file "C:\rep\fichier de test.txt"
on the batch file, nothing happens... (the test to delete > echo_arg_in_file.txt
was done before and displays well "C:\rep\fichier de test.txt"
)
有什么解释吗?
推荐答案
我不确定您的精确环境,但是如果我敢打赌,当前的活动目录就是问题
I'm not sure about your precise environment, but if I have to bet, current active directory is the problem
用
@echo off
for %%a in (.) do echo %%~fa
pause
然后通过双击文件和拖放文件来执行文件.在这两种情况下,您都将看到已启动的cmd
进程的当前活动目录.
Then execute the file both by double clicking it and by drag/drop a file. In both cases you will see the current active directory for the started cmd
process.
为什么这样有意义?由于您没有在原始文件重定向中包含路径,因此将在当前活动目录中创建此文件,这可能不是您期望的.
Why is this relevant? As you have not included a path in the original file redirect, this file will be created in the current active directory that, maybe, could not be what you expect.
您可以在此处
要获得快速解决方案,
@echo OFF
> "%~dp0\echo_arg_in_file.txt" echo %1
将在与保存批处理文件的文件夹相同的文件夹中创建文件
that will create the file in the same folder that hold the batch file
这篇关于DOS批处理:命令行和拖放之间的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!