批处理文件 - 在命令行中将路径名作为参数传递

Batch file - passing path name as parameter in command line

我只想将扩展名为 .txt 的文件从源复制到目标。我想在命令行中提供路径名 - source 和路径名 - destination 作为参数。这是我的批处理文件 Script.bat:

@ECHO OFF
setlocal 
set /p source =
set /p destination=
FOR %%f IN (*.txt) DO XCOPY "%source%"\"%%f" "%destination%" /m /y /d /s

我想在 cmd 中调用这个批处理文件:

cmd> Script.bat "SourceFolder" "DestinationFolder"

但是不行! 谢谢!

试一试。它可能不完全正确,但应该可以帮助您入门。

@echo off
setlocal 
set /p source = 
set /p destination= 
xcopy /m /y /d /s "%~source%\*.txt" "%~destination%\"

假设 /m /y /d /s xcopy 开关的正确性,此脚本可以运行:

@echo off
setlocal
if "%~1"=="" goto :error empty 1
if "%~2"=="" goto :error empty 2
set "source=%~1"
if not exist "%source%\" goto :error not exist 1
set "destination=%~2"
if not exist "%destination%\" goto :error not exist 2
xcopy "%source%\*.txt" "%destination%\" /m /y /d /s

goto :eof
:error
echo wrong parameters %*
goto :eof

另请参阅下一个资源:Command Line arguments (Parameters)