如何在批处理文件中使用变量为复制命令提供根文件夹的路径?
set destination= "c:\destination folder"
mkdir %destination%
SET source = "C:\source folder\"
SET source_full = "C:\source folder\simple file.txt"
rem this doesn't work
copy %source%\"simple file.txt" %destination%
rem this works
copy %source_full% %destination%
答案1
抱歉,最初看错了...设置变量时不要使用空格,那么这个...
copy %source%\"simple file.txt" %destination%
变成这样
copy %source%"simple file.txt" %destination%
请注意,您的源中已经有一个“\”,而您在引号外又添加了一个。因此,调用该复制命令实际上是对额外的斜线做了一些奇怪的事情。
答案2
显然你不能在变量和=
C:\Users\glennjackman>set destination= "c:\destination folder"
C:\Users\glennjackman>echo %destination%
"c:\destination folder"
C:\Users\glennjackman>SET source = "C:\source folder\"
C:\Users\glennjackman>echo %source%
%source%
C:\Users\glennjackman>SET source= "C:\source folder\"
C:\Users\glennjackman>echo %source%
"C:\source folder\"
并且看起来引用应该包含变量名:
C:\Users\glennjackman>set "destination=c:\destination folder"
C:\Users\glennjackman>SET "source=C:\source folder"
C:\Users\glennjackman>copy "%source%\simple file.txt" "%destination%"
1 file(s) copied.