1

I am trying to create a batch file which scans a folder for files

for /R D:\path\import_orders\xml_files\ %%f in (*.xml) do(
    copy %%f "\\destination"
    if errorlevel 0 move %%f "D:\path\import_orders\xml_files\archive\"
)

FIXED - But it won't work. If I execute it, it only prints the first line of code. It works now. I added a space after the "do (" and now it executes.

1) is my second command ok? I want to move the copied file in an archive if everything went well with the first command.

2) how should i change the loop to work only on the files in the given directory and not the subdirectories in it?

6
  • 1
    if !errorlevel! equ 0 move... Commented Oct 2, 2015 at 8:39
  • so you want copy without overwriting? Commented Oct 2, 2015 at 8:39
  • try with copy /y copy "%%~ff" "\\destination" Commented Oct 2, 2015 at 8:46
  • @npocmaka it should skip the copy if the file is already there. Commented Oct 2, 2015 at 8:52
  • @darkfang what's the difference with my code? Sorry but I've never used batch files for more advanced things. Commented Oct 2, 2015 at 8:53

2 Answers 2

3
  • You are missing a space between the do and the parenthesis

  • The construct if errorlevel n is evaluated to true for any errorlevel value equal or greater to n, so if errorlevel 0 will be true for any non negative errorlevel value. You should use if not errorlevel 1

  • It is a good habit to quote all paths just in case something could include a space or a special character

for /R "D:\path\import_orders\xml_files" %%f in (*.xml) do (
    copy "%%~ff" "\\destination"
    if not errorlevel 1 move "%%~ff" "D:\path\import_orders\xml_files\archive\"
)

To avoid the directory recursion, just change the for loop, removing the /R (that asks for recursion) moving the start folder tip to the file selection pattern.

for %%f in ("D:\path\import_orders\xml_files\*.xml") do (
    copy "%%~ff" "\\destination"
    if not errorlevel 1 move "%%~ff" "D:\path\import_orders\xml_files\archive\"
)

But in any case copy command does not ask for confirmation if target file exists. If you don't want to overwrite the existing file you have some options

Use the switches of the copy command

You can use /-y so copy command will ask for confirmation before overwritting the file, and to automate the process you can pipe the answer to the question

echo n|copy /-y "source" "target"

This is the approach in the npocmaka's answer. This approach should work without problems but

  • It is necessary two create two cmd instances to handle each of the sides of the pipe, and to do it for each of the source files, so it will slow the process

  • It could fail if the code is executed on a locale where the overwrite question is not waiting a N character as a negative answer.

First check for file presence

You can use the builtin if exist construct to first check if the target file is present

if not exist "\\destination\%%~nxf" copy "%%~ff" "\\destination"

where %%~nxf if the name and extension of the file being processed

So, the final code could be

for %%f in ("D:\path\import_orders\xml_files\*.xml") do (
    if not exist "\\destination\%%~nxf" copy "%%~ff" "\\destination"
    if not errorlevel 1 move "%%~ff" "D:\path\import_orders\xml_files\archive\"
)
Sign up to request clarification or add additional context in comments.

6 Comments

in the comments it's mentioned that the copy should be without overwriting.
Thanks a lot! The problem is that as you can see from the second command the folder \archive is a subfolder of xml_files. I've tried your example and the for is looping also through the archived fiels. Any way to avoid it from recursively search for files in subfolders?
@nowhere, I think I modified the answer while you were reading. It is included at the end.
@MCND Great answer. Thanks for the help.
@nowhere, I were re-reading the answer and I realized there is two non defined points. If a file is present in target and it is skipped, is this an error and the move should also be skipped? What to do if the source file is also present in archive?
|
1
for /R "D:\path\import_orders\xml_files\" %%f in (*.xml) do (
    (echo n|copy /-y "%%~ff" "\\destination"|find /i "0 file(s) copied." >nul 2>&1)||(
       move "%%~ff" "D:\path\import_orders\xml_files\archive\"
    )
)

edit without searching subdirectories:

for %%f in ("D:\path\import_orders\xml_files\*.xml") do (
    (echo n|copy /-y "%%~ff" "\\destination"|find /i "0 file(s) copied." >nul 2>&1)||(
       move "%%~ff" "D:\path\import_orders\xml_files\archive\"
    )
)

3 Comments

Thanks! Will this also check the subfolders recursively? Cause I'm looking only for the files in the given dir "\xml_files\".
@nowhere - yes. It will copy without overwrite and if the copy is sucessfull it move the file.
Thanks for the help, i selected MC's answer just cause it's better explained. voted up anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.