【cmd】IF ELSE 复制(copy)文件问题

本文详细介绍了在CMD批处理文件中,使用IF ELSE命令复制带有特殊字符的文件时遇到的问题及解决方法。通过在COPY命令的路径参数中添加双引号或者转义特殊字符,可以确保批处理文件正确执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cmd中复制文件COPY命令一般都不会有问题,但是如果把COPY放在IF ELSE中可能导致批处理文件无法运行。

测试场景

文件夹结构如下:

test
|—folder1
|—|—a(b).txt
|—folder2

选择是否从folder1文件夹复制a(b).txt文件到folder2文件夹。

测试1

不进行选择交互,直接复制,脚本如下:

@echo off & setlocal EnableDelayedExpansion

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.* 

copy !currentDir!\folder1\a(b).txt !currentDir!\folder2

保存为test.bat文件后执行结果:

已复制         1 个文件。
请按任意键继续. . .

copy复制语句似乎没有问题。

测试2

修改以上脚本,添加选择交互:

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否复制(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy !currentDir!\folder1\a(b).txt !currentDir!\folder2
)else (
    echo 没复制
)

pause

保存为test.bat文件后执行,发现一闪而过,看不到什么报错。

解决

经过多次测试,发现将copy中的路径用双引号(“”)包裹起来就可以了。

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否复制(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy "!currentDir!\folder1\a(b).txt" !currentDir!\folder2
)else (
    echo 没复制
)

pause

由此可见,应该是路径中某些符号没有转义导致的,目测是文件名中的(),修改脚本,用^()转义:

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否复制(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy !currentDir!\folder1\a^(b^).txt !currentDir!\folder2
)else (
    echo 没复制
)

pause

运行结果:

是否复制(0:否,1:是): 1
已复制         1 个文件。
请按任意键继续. . .

总结

虽然测试1中直接执行复制没问题,但是,将同样的语句放入IF ELSE中居然无法执行,还很难定位问题在哪里,所以解决方法是最好把路径放在双引号(“”)里面,就不用担心这个问题了,如果不这样就在IF ELSE中的路径把特殊字符转义。附上CMD中特殊字符转义说明。

Character to be escapedEscape SequenceRemark
%%%May not always be required in doublequoted strings, just try
^^^May not always be required in doublequoted strings, but it won’t hurt
&^&May not always be required in doublequoted strings, but it won’t hurt
<^<May not always be required in doublequoted strings, but it won’t hurt
>^>May not always be required in doublequoted strings, but it won’t hurt
^’Required only in the FOR /F “subject” (i.e. between the parenthesis), unless backqis used
`^`Required only in the FOR /F “subject” (i.e. between the parenthesis), if backq is used
,^,Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
;^;Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
=^=Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
(^(Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
)^)Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
!^^!Required only when delayed variable expansion is active
“”Required only inside the search pattern of FIND
\\\Required only inside the regex pattern of FINDSTR
[\[Required only inside the regex pattern of FINDSTR
]\]Required only inside the regex pattern of FINDSTR
\\\Required only inside the regex pattern of FINDSTR
.\.Required only inside the regex pattern of FINDSTR
*\*Required only inside the regex pattern of FINDSTR
?\?Required only inside the regex pattern of FINDSTR

参考:http://www.robvanderwoude.com/escapechars.php

将表格快速转换为HTML,Markdown格式:http://www.tablesgenerator.com/

更多
c-xuan.com/2017/11/28/20171128001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码路刺客

您的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值