Darzen
Darzen

Reputation: 1135

Copy Files using a batch file

I need to copy a set of files mentioned in the loop to another location with each file copied into a directory that is named after the file name.

Example : I need to copy file1.txt from path ABC to DEF\file1\ ; file2.txt from path ABC to DEF\file2\

I have been trying to use this following for loop to achieve the same thing. however there seems to be a syntax error with what I am doing. because of the hurry to deliver this, I could not explore much before posting it here. I am searching for solutions in parallel. Please let me know the issue with the code sample below

FOR %%file IN (de.txt en.txt es.txt fr.txt it.txt ja.txt nl.txt pt.txt zh_CN.txt) DO  
     SET name=%file%.*
    echo f | xcopy /I /Y "C:\textfiles\%name%.txt" D:\textfiles\%name%\LanguageFile.txt
DONE

Upvotes: 1

Views: 143

Answers (2)

Magoo
Magoo

Reputation: 79983

@ECHO OFF
SETLOCAL
FOR %%f IN (de.txt en.txt es.txt fr.txt it.txt ja.txt nl.txt pt.txt zh_CN.txt) DO (
  ECHO ECHO f bar xcopy /I /Y "C:\textfiles\*%%f*" u:\textfiles\%%~nf\LanguageFile.txt
)
GOTO :EOF

Simply echoes the command to be executed. If correct, change ECHO f bar to f| to actually copy

Upvotes: 0

Preet Sangha
Preet Sangha

Reputation: 65476

Try this from the command line

for %f in (de.txt en.txt es.txt fr.txt it.txt ja.txt nl.txt pt.mo zh_CN.txt) do echo xcopy "C:\textfiles\%~nf.mo" "D:\textfiles\%~nf\%~nf.mo"

or in batch file

for %%f in (de.txt en.txt es.txt fr.txt it.txt ja.txt nl.txt pt.mo zh_CN.txt) do echo xcopy "C:\textfiles\%%~nf.mo% "D:\textfiles\%%~nf\%%~nf.mo"

Upvotes: 3

Related Questions