复制由Windows cmd中的条件选择的多个文件

时间:2022-04-16 06:25:34

I am trying to copy from one directory to another, all files that have 2 digits format (ex. 12.txt, 15.pdf, 25.doc... etc), this is in Windows.

我试图从一个目录复制到另一个目录,所有文件都有2位数格式(例如12.txt,15.pdf,25.doc ......等),这是在Windows中。

In Linux this works:

在Linux中,这适用于:

cp -t target_directory {10..99}.*

In Windows what will be the solution?

在Windows中,什么是解决方案?

2 个解决方案

#1


1  

This is ugly but should work

这很难看但应该有用

for /L %i in (10,1,99) do @copy %i.* dest_folder_here >nul 2>&1

for / L%i in(10,1,99)do @copy%i。* dest_folder_here> nul 2>&1

It tries every file in the range suppressing conditions. Adjust copy to overwrite if needed.

它会尝试范围抑制条件中的每个文件。如果需要,调整副本以覆盖。

#2


0  

I would filter for the correct files by findstr, like this:

我会通过findstr过滤正确的文件,如下所示:

for /F "delims= eol=|" %%I in ('dir /B /A:-D "??.*" ^| findstr /X "[0123456789][0123456789]\.[^.]*"') do copy "%%~I" "\path\to\destination"

I avoided to use range expressions [0-9] because they might also match ², ³.

我避免使用范围表达式[0-9],因为它们也可能匹配²,³。

If you also want to match files with no file name extension, change the search string to [0123456789][0123456789] [0123456789][0123456789]\.[^.]*.

如果您还想匹配没有文件扩展名的文件,请将搜索字符串更改为[0123456789] [0123456789] [0123456789] [0123456789] \。[^。] *。

#1


1  

This is ugly but should work

这很难看但应该有用

for /L %i in (10,1,99) do @copy %i.* dest_folder_here >nul 2>&1

for / L%i in(10,1,99)do @copy%i。* dest_folder_here> nul 2>&1

It tries every file in the range suppressing conditions. Adjust copy to overwrite if needed.

它会尝试范围抑制条件中的每个文件。如果需要,调整副本以覆盖。

#2


0  

I would filter for the correct files by findstr, like this:

我会通过findstr过滤正确的文件,如下所示:

for /F "delims= eol=|" %%I in ('dir /B /A:-D "??.*" ^| findstr /X "[0123456789][0123456789]\.[^.]*"') do copy "%%~I" "\path\to\destination"

I avoided to use range expressions [0-9] because they might also match ², ³.

我避免使用范围表达式[0-9],因为它们也可能匹配²,³。

If you also want to match files with no file name extension, change the search string to [0123456789][0123456789] [0123456789][0123456789]\.[^.]*.

如果您还想匹配没有文件扩展名的文件,请将搜索字符串更改为[0123456789] [0123456789] [0123456789] [0123456789] \。[^。] *。