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
ewang1205Asked:
Who is Participating?
 
knightEknightCommented:
:This will copy the most recent file only:

@echo off
setlocal
FOR /F "tokens=*" %%I IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO  set FileToCopy=%%I
COPY/y "C:\myfolder\%FileToCopy%" test.txt
EXIT /b
0
 
knightEknightCommented:
FOR /F %%I IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO COPY "c:\myfolder\%%I" test.txt & EXIT
0
 
knightEknightCommented:
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?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
knightEknightCommented:
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

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
knightEknightCommented:
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).
0
 
knightEknightCommented:
%%I would contain the full path if /S was used with the DIR command, but in this case it is not.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That's true and a oversight by me. I've "spotted" a /S, which seems to be invisible ;-).
0
 
knightEknightCommented:
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
0
 
knightEknightCommented:
hmm, this may be a problem too, since you are creating a .txt file in the folder, which may recurse - so scratch my last. :)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
0
 
knightEknightCommented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That should cover all, I reckon.
0
 
ewang1205Author Commented:
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

0
 
knightEknightCommented:
again, what is your goal?  to create one big file with the content of all the others?  If so, this won't do it...
0
 
knightEknightCommented:
To copy the oldest file only, change  /O:-D  to  /O:D
0
 
knightEknightCommented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
ewang1205Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.