Link to home
Start Free TrialLog in
Avatar of ewang1205
ewang1205

asked on

copy command in DOS

I have the following copy.  When run in different folders, it cannot find files (test*.txt).  I thing COPY %%I needs to be specifict enought to point the folder name.  How?  THanks.

FOR /F %%I IN ('DIR c:\myfolder\test*.txt /B /O:-D') DO COPY %%I test.txt & EXIT
Avatar of knightEknight
knightEknight
Flag of United States of America image

FOR /F %%I IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO COPY "c:\myfolder\%%I" test.txt & EXIT
It looks like you are trying to copy each file to a new file called test.txt - is that correct?  Are you trying to create one big file that contains all the other files?  What is your goal?
this is the part I think is problematic, depending on your goal:

  COPY "c:\myfolder\%%I" test.txt

As is, it will prompt you to over-write test.txt with each file.  I doubt that is what you really want.  If "test.txt" is a folder name, then you are okay.  If you want to over-write it each time, use COPY/Y instead of just COPY .

If you want to create one big file from all the other .txt files, do this:

  cd "c:\myfolder"
  copy *.txt  output.file
  ren  output.file  bigfile.txt

The way you use the FOR it terminates %%I at each white space, which is likely to happen in particular in folder names. To get the whole line into %%I, you can perform this:
FOR /F "tokens=*" %%I IN ('DIR c:\myfolder\test*.txt /B /O:-D') DO COPY %%I test.txt & EXIT

Open in new window

BTW, you are aware that the first file found will be copied only, because of the EXIT?
And you are aware that EXIT also terminates cmd.exe? In a batch you should use EXIT /B instead. That only terminates this batch, and will return to the calling batch file or cmd.exe.
I didn't even see the EXIT, you are correct about that.  But I believe the issue is that %%I doesn't contain the full path, so it must be specified in the COPY command (see above).
%%I would contain the full path if /S was used with the DIR command, but in this case it is not.
That's true and a oversight by me. I've "spotted" a /S, which seems to be invisible ;-).
another option is to CD into the desired folder before running the FOR:

CD "c:\myfolder"
FOR /F %%I IN ('DIR test*.txt /B /O:-D') DO COPY/y  %%I  test.txt  & EXIT
hmm, this may be a problem too, since you are creating a .txt file in the folder, which may recurse - so scratch my last. :)
And the save method is (if test*.txt might contain spaces):
FOR /F "tokens=*" %%I IN ('DIR c:\myfolder\test*.txt /B /O:-D') DO COPY "C:\myfolder\%%I" test.txt & EXIT /b
quoting the path in both places now:

FOR /F "tokens=*" %%I IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO COPY "C:\myfolder\%%I" test.txt & EXIT /b
That should cover all, I reckon.
Avatar of ewang1205
ewang1205

ASKER

Thanks for all the help.  I know what to do know.  But, I am getting a recursive copy to test.txt somehow.  Here is my command.  Thanks!!!

FOR /F IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO COPY "C:\myfolder\%%I" test.txt & EXIT /b

again, what is your goal?  to create one big file with the content of all the others?  If so, this won't do it...
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To copy the oldest file only, change  /O:-D  to  /O:D
To copy all the .txt files in that folder to one big .txt file:

copy/y  "C:\myfolder\*.txt"  output.file
ren  output.file  test.txt
kEk, your switches are reversed. /o:-D will sort for date descending, and so the last value found is the oldest.

ewang1205, your target file must not be created in c:\MyFolder, or use a different filename which is not catched by the FOR.
Thanks!