简体   繁体   English

批处理文件以复制文件

[英]Batch file to copy files

Need to have a batch file that will actually check if the file name has "abc" in its name and if its there , then it will initiate a copy. 需要一个批处理文件来实际检查文件名是否在其名称中有“abc”,如果它在那里,那么它将启动一个副本。 So suppose folder /test has 5 files 所以假设文件夹/测试有5个文件

apt.text 
mar.text 
may.text 
Jan.text 
abc_xyx1234.text

So since "abc" appears in string only in the last file , it should pick only last one. 因此,由于“abc”仅在最后一个文件中出现在字符串中,所以它应该只选择最后一个。 I tried 我试过了

@echo off 
for /f %%a in (' dir /b /a-d ^| find /v /i "abc" ') copy "%%a" 
pause

but it didn't work out. 但它没有成功。

You have no destination for your copy. 您的副本没有目的地。

The find /v /i will find those filesnames that do not ( /v ) match abc find /v /i将找到那些没有/v )匹配abc

It's probably easier to use 它可能更容易使用

copy *abc* destination

I think part of your problem is the following: 我认为您的部分问题如下:

Even though forward slashes work sometimes , Windows' canonical path separator is the backslash \\ . 即使正斜杠有时工作, Windows的规范路径分隔符也是反斜杠\\ Using forward slashes as path separators confuses programs which use the forward slash as parameter prefix, and most if not all of the Microsoft Windows tools do so, including FIND and FINDSTR . 使用正斜杠作为路径分隔符会混淆使用正斜杠作为参数前缀的程序,并且大多数(如果不是全部)Microsoft Windows工具都会这样做,包括FINDFINDSTR

Also, if you're talking about /folder - which should be \\folder as explained above - you're talking about a directory in the root of the current drive . 另外,如果您正在谈论/folder - 如上所述,它应该是\\folder - 您正在谈论当前驱动器根目录中的目录 I'm not sure you really mean that. 我不确定你真的是那个意思。

Apart from that, Magoo's suggestion is still kind of valid, but it omits the path "problem". 除此之外,Magoo的建议仍然有效,但它忽略了“问题”的路径。

So let's use absolute directories to clarify: 所以让我们使用绝对目录来澄清:

I'll assume your source directory is c:\\work\\source and contains the file set you've stated in your question. 我假设您的源目录是c:\\work\\source并包含您在问题中声明的文件集。 I'll assume your target directory is c:\\stackoverflow\\target . 我假设您的目标目录是c:\\stackoverflow\\target So you can do the following from anywhere on your system: 因此,您可以从系统的任何位置执行以下操作:

copy c:\work\source\*abc* c:\stackoverflow\target

If you're doing this from an arbitrary folder on drive c , you could omit the drives: 如果您是从驱动器c上的任意文件夹执行此操作,则可以省略驱动器:

copy \work\source\*abc* \stackoverflow\target

And, if you're inside the source folder, you could use Magoo's answer: 而且,如果你在源文件夹中,你可以使用Magoo的答案:

copy *abc* \stackoverflow\target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM