问题
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.
The very simple batch file (test_echo.cmd
) is as following:
@echo OFF
echo %1 > echo_arg_in_file.txt`
On the command line,
C:\rep>test_echo.cmd "C:\rep\fichier de test.txt"`
creates a file echo_arg_in_file.txt
with "C:\rep\fichier de test.txt"
written inside.
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"
)
Any explanation?
回答1:
I'm not sure about your precise environment, but if I have to bet, current active directory is the problem
Replace your test_echo.cmd
with
@echo off
for %%a in (.) do echo %%~fa
pause
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.
You can find more information here
For a quick solution,
@echo OFF
> "%~dp0\echo_arg_in_file.txt" echo %1
that will create the file in the same folder that hold the batch file
回答2:
What Windows' version. Vista can't drag and drop into a command prompt for security reasons. Restricted possibilities are on later versions (cause we all whinged).
Prior to Vista it was the same as typing the file name if dragged into the window.
If talking about a shortcut each file is one parameter (use shift
command to handle this).
来源:https://stackoverflow.com/questions/30963931/dos-batch-different-behaviour-between-command-line-and-drag-and-drop