0
votes

I am trying to find specific files within a folder and its sub-folders and copy them to a new folder. I used to use this batch file for this purpose but now I get this error :

The system cannot find the file specified.

Here's the batch-file content:

pushd "\\internal.company.com\path\"
md myfile
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\\internal.company.com\path\myfile"

Any input is appreciated.

UPDATE

I tried printing %%G like this:

FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G

and it works well. The problem arises with copy command which cannot read %%G as an argument.

2

2 Answers

0
votes

I'd change to

... copy "%%G" ...

which would cater for spaces in %%G.

Perhaps you should determine whether the problem is with the for/r or %%G. If you simply display %%G with an echo %%G command, then you may see some problem. If it still won't run, then the for /r is the problem.

Since you are pushding to the appropriate directory, there is no need to specify the target directory in the for/r. It can be omitted or replaced by .

or perhaps echo %cd% directly after the pushd to show whether the pushd is the cause of the problem.

0
votes

I realized that this would work for me (executed in cmd). However, it is plausible because I don't have any *.txt file in the main directory. Not the best solution but a workaround.

pushd "\\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\\internal.company.com\path\myfile"

Note: if you want to execute this as a batch file, you need to use %%f instead of %f.

This will copy the files into main folder and then moves them into the desired sub-folder.