1.

Solve : Help needed in copying files using batch script?

Answer»

Hi Guys....
I have a requirement..I am trying with windows batch script.If any one has easy way to achieve please let me know..


Assume I have a file with following contents... I need to copy all the
files in the text file in to another folder with same directory structure

If I want to move the all the files given below to D: drive with same
directory structure

R:\corejava\CoreJavaTraining\src\org\jl\ArrayExample.java
R:\corejava\CoreJavaTraining\src\org\jl\list_of_files.txt
R:\corejava\CoreJavaTraining\src\org\jl\New Text Document.txt
R:\corejava\CoreJavaTraining\src\org\jl\OverriddingExample.java
R:\corejava\CoreJavaTraining\src\org\jl\ProducerConsumerGame.java
R:\corejava\CoreJavaTraining\src\org\jl\StringExample.java
R:\corejava\CoreJavaTraining\src\org\jl\Subclass1.java
R:\corejava\CoreJavaTraining\src\org\jl\Subclass2.java
R:\corejava\CoreJavaTraining\src\org\jl\Subclass22.java
R:\corejava\CoreJavaTraining\src\org\jl\SuperClass.java
R:\corejava\CoreJavaTraining\src\org\jl\test - Copy.bat
R:\corejava\CoreJavaTraining\src\org\jl\test.bat
R:\corejava\CoreJavaTraining\src\org\jl\ThreadExample.java
R:\corejava\CoreJavaTraining\src\org\jl\ThreadRunnable.java
R:\corejava\CoreJavaTraining\src\org\jl\VolatileExample.java

Expecting output:

D drive need to have all the above files with same directory structure as above

D:\corejava\CoreJavaTraining\src\org\jl\ArrayExample.java
D:\corejava\CoreJavaTraining\src\org\jl\list_of_files.txtIf you using Windows 7 you can use Robocopy with the /IF switch to include files from a list.

More info here: https://techjourney.net/robocopy-syntax-command-line-switches-and-examples/for /f "[emailprotected]" %%a in (textfile) do move /y %%a d:\Quote from: erobby on November 15, 2015, 12:12:59 AM

for /f "[emailprotected]" %%a in (textfile) do move /y %%a d:\
What is that supposed to do, exactly?
reads the text file using @ as a delimiter in the event there are spaces in the file name and moves the files to the D: Drive

if you are running it from the command line and not in a batch file it should be
for /f "[emailprotected]" %a in (textfile) do move /y %a d:\OK. But why not use "delims=" which is the standard way of grabbing the whole line?

Possible relevance:
what delim to use to to read text file line by line
Quote
Not really, as "tokens=*" and "delims=" produce NEARLY the same result. As with "tokens=*" the delims are still active, they remove leading spaces and TABs from each line – jeb Jul 22 '11 at 11:29
Quote from: erobby on November 15, 2015, 10:22:46 AM
reads the text file using @ as a delimiter in the event there are spaces in the file name and moves the files to the D: Drive

As noted in posts above "delims=" is USEFUL

- and if there are spaces, then won't your code break if the loop variable is unquoted?Using ROBOCOPY is probably the best solution, as it's able to create the direcory structure at the DESTINATION if it doesn't already exist. COPY and MOVE (on my system, Win8.1/64) simply error out with "The system cannot find the path specified." if the destination folders do not already exist.

Also, the issue of using FOR and "[emailprotected]" has several problems as well, including things like handling spaces (easily fixed, though), and what happens if one of the source files has an @ character in the name? Of course, there's work-arounds/solutions to these problems.. But they should be AWARE in the first place, not just blindly use a FOR construct that seems to work "most of the time.."


Discussion

No Comment Found