-3

I have a directory with files directly in it C:\test1.
I want to copy all these files to C:\test2 only if the file does not exist in any subdirectory in C:\test2.

Need to write a Windows batch file which can do this. Any help is appreciated.

I have tried using the following, but it does not searches recursively in destination directory:

For %%F In ("C:\test1*") Do If Not Exist "C:\test2\%%~nxF" Copy "%%F" "C:\test2\%%~nxF"
Mofi
  • 46,139
  • 17
  • 80
  • 143
Ajay Gupta
  • 464
  • 1
  • 5
  • 10
  • 1
    Add your attempt into your question post! Anyway, instead of `if not exist "C:\test2\%%~nxF" copy ...`, try `> nul dir /S /B /A:-D "C:\test2\%%~nxF" || copy ...`. – aschipfl Oct 11 '18 at 16:47
  • 2
    You could use `XCopy` with an empty `/D` option, _which would only copy all files except for unchanged existing ones_. You could probably even use `RoboCopy` with the `/XN` and `/XO` options too. Enter `XCopy /?` or `RoboCopy /?` at the Command Prompt for usage information and the additional options you may require. – Compo Oct 11 '18 at 16:48

2 Answers2

1

Examples make it easier for everyone to understand the requirements for a task.
So let me start with an example.

Source directory C:\test1 contains following files:

  • Test1.txt
  • Test2.txt
  • Test3.txt

Destination directory tree C:\test2 contains following directories and files:

  • Subfolder1
    • Test2.txt
  • Subfolder2
    • Subfolder3
      • Test3.txt

For this example just file Test1.txt should be copied to directory C:\test2 because Test2.txt and Test3.txt exist already in subfolders of C:\test2.

So the directory tree C:\test2 should look as follows after batch file execution:

  • Subfolder1
    • Test2.txt
  • Subfolder2
    • Subfolder3
      • Test3.txt
  • Test1.txt

This can be achieved with:

@echo off
for %%I in ("C:\test1\*") do (
    dir "C:\test2\%%~nxI" /A-D /B /S >nul 2>nul
    if errorlevel 1 copy "%%I" "C:\test2\" >nul
)

It is also possible to do that with a single command line:

@for %%I in ("C:\test1\*") do @dir "C:\test2\%%~nxI" /A-D /B /S >nul 2>nul || copy "%%I" "C:\test2\" >nul

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • copy /?
  • echo /?
  • for /?
  • if /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
1

If you wanted to push ahead into PowerShell, this code might help. When you are satisfied the the files will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.

Ok, I admit that I do not have a test1 and test2 directory for testing, but this will probably get you started.

$targetdir = 'C:\test2'

Get-ChildItem -File -Path 'C:\test1' |
    ForEach-Object {
        if ((Get-ChildItem -File -Recurse -Path $targetdir -Filter $_.Name).Count -eq 0) {
            Copy-Item -Path $_.FullName -Destination $targetdir -WhatIf
        }
    }

If you save the script above as Copy-IfNone.ps1, then you can run it from a cmd shell using:

powershell -NoProfile -File .\Copy-IfNone.ps1
lit
  • 14,456
  • 10
  • 65
  • 119