Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

copy command in DOS

Posted on 2011-04-25
19
Medium Priority
?
407 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:ewang1205
  • 12
  • 5
  • 2
19 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463005
FOR /F %%I IN ('DIR "c:\myfolder\test*.txt" /B /O:-D') DO COPY "c:\myfolder\%%I" test.txt & EXIT
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463024
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463045
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 71

Expert Comment

by:Qlemo
ID: 35463046
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463058
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463059
%%I would contain the full path if /S was used with the DIR command, but in this case it is not.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35463068
That's true and a oversight by me. I've "spotted" a /S, which seems to be invisible ;-).
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463076
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463080
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 35463084
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463092
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 35463126
That should cover all, I reckon.
0
 

Author Comment

by:ewang1205
ID: 35463142
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463151
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
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 35463173
: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
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463175
To copy the oldest file only, change  /O:-D  to  /O:D
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35463193
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 35463253
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
 

Author Closing Comment

by:ewang1205
ID: 35470013
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question