Результаты Windows XCOPY из команды FIND

611
Bardworx

Windows 7 CMD

Я могу получить список файлов с помощью следующей команды dir /b | find "TENDER_NUM 2". Я пытаюсь скопировать все файлы, которые были возвращены при поиске, в другую папку.

Я пробовал: for /r %x in (dir /b | findstr "TENDER_NUM 2") do copy "%x" dir_to_copy\

Который не работал.

РЕДАКТИРОВАТЬ 1: сообщение об ошибке говорит unexpected |, что я предполагаю, исходит отdir/b | Findstr

РЕДАКТИРОВАТЬ 2: Да, dir_to_copy \ существует

1
Какое сообщение об ошибке вы получаете? DavidPostill 7 лет назад 0
Существует ли dir_to_copy? DavidPostill 7 лет назад 0
@DavidPostill - обновленный вопрос с обоими ответами Bardworx 7 лет назад 0
Смотри мой ответ ... DavidPostill 7 лет назад 0

1 ответ на вопрос

1
DavidPostill

Error message says unexpected |

for /r %x in (dir /b | findstr "TENDER_NUM 2") do copy "%x" dir_to_copy\ 

There are multiple errors in the above command.

  1. You need to escape the | special character using ^:

    ^| 
  2. You need to enclose dir /b | findstr "TENDER_NUM 2" with single quotes ':

    'dir /b ^| findstr "TENDER_NUM 2"' 
  3. You need to use for /f instead of for /r:

    for /f - Loop command against the results of another command.

    for /r - Loop through files (Recurse subfolders).

Use the following command from a cmd shell:

for /f %x in ('dir /b ^| findstr "TENDER_NUM 2"') do copy "%x" dir_to_copy\ 

In a batch file (replace % with %%):

for /f %%x in ('dir /b ^| findstr "TENDER_NUM 2"') do copy "%%x" dir_to_copy\ 

Further Reading

Спасибо, Дэвид, это мой первый набег в Windows CMD, и материалы для чтения очень ценятся. Bardworx 7 лет назад 0
Пожалуйста. Приятного чтения! :) DavidPostill 7 лет назад 0