0

There are a lot of instructions how to copy certain files from one folder to another folder and preserving the folder structure. I can do this with xcopy [srcFolder][Filename] [DestFolder] /S /K /Y.

However, what I need is searching for a certain folder name and then copy that folder along with all it's content to the destination folder. So let's say I have the following:

SrcFolder   
   -Folder 1
          -MySubfolder
             -File1_1.txt
             -File1_2.jpg
          -AnotherSubfolder
             -File1_3.txt
    
   -Folder 2
          -MySubfolder
             -File2_1.txt
             -File2_2.jpg
          -AnotherSubfolder
             -File2_3.txt

Now I would like to copy all "MySubfolder" folders incl their content. So at the end my target directory will look like this

DstFolder
   -Folder 1
      -MySubfolder
         -File1_1.txt
         -File1_2.jpg
   -Folder 2
      -MySubfolder
         -File2_1.txt
         -File2_2.jpg

How can I achieve this? Note that I am on Windows 7 so I don't have access to robocopy

3
  • Do you have access to PowerShell on your Windows 7 instance? Search for PowerShell solutions for this task with Get-ChildItem using -include "*MySubfolder*" -recurse or something along those lines. Should be a simple trivial matter to get setup with PowerShell. Sep 29 '20 at 22:36
  • I have seen the get-childitem command but everything was about files. So I tried your "MySubfolder" option and that got me further. So I did Get-ChildItem -Path $src -include "[DirectoryName]" -recurse | foreach { copy-item -Path $_ $dst -recurse } However, that gives me the error copy-item:AnItem with the specified name ... already exists. The destination folder is empty, though so it really doesn't exist.
    – Hu F.
    Sep 29 '20 at 23:29
  • Try foreach { copy-item -Path $_ $dst -recurse -force } and don't forget about -ErrorAction SilentlyContinue too as it might help with that error. Otherwise you can wrap in an If(!(Test-Path $_)){ copy-item -Path $_ $dst -recurse -force } or something like that. Good job, keep digging at it, you are doing great! This answer might be somewhat helpful too for those things I mention: superuser.com/questions/1562774/…. Sep 30 '20 at 0:37
0

Well you can use anything C to Java, VBS, PS or good ol' batch scripts. If you want to go down the nostalgia road, here we go:

CurrentDir
  + Love Letters
    my_love.txt
    breakup.txt
    new love.txt
  + Confidential
    + FBI
      + JFK Murder
        + eye witness report.txt
    + CIA
      + Area 51
        + UFO.jpg

script.cmd

@ECHO OFF

@SETLOCAL
@SET SearchPath=%1
@SET SearchForDir=%2
@SET DestinationDir=%3

@FOR  /f "delims=" %%a in ('dir /B /S /A:D "%SearchPath%" ^|findstr /e "%SearchForDir%"') DO @SET DirPath=%%~a

@xcopy "%DirPath%" "%DestinationDir%/%SearchForDir%" /S /K /Y /I

echo DirPath %DirPath%

@ENDLOCAL

@ECHO ON

(minor note, I made the search case sensitive so that MySubFolder would appear like that, otherwise, since Windows is case insensitive you might get all the stuff you want in a directory called mysUbFoldEr if that was your search string)

You can either call it via command line using parameters:

script.cmd <search_path> <dir_to_search_for> <destination_path> 

For example

script.cmd . "FBI" "D:\Public"

Or you can just manually set the local envvars and run it manually with hardcoded values. Your choice.

If you want a shorter version, just replace the envvars with the parameter vars. I only did it so it'd be easier to follow.

2
  • Thanks a lot for that script. However, it gives me the error "FINDSTR: Cannot open Folder. Invalid number of parameters. Please note that 'Folder' is part of the SearchForDir, i.e. "My Folder". I put the second argument between quotes and I see you also accounted for that in your script so I am not sure why it only takes the second part of "My Folder".
    – Hu F.
    Sep 30 '20 at 20:30
  • Well I did test it beforehand and with the dir structure I showed you as example (confidential and love letters) it worked fine. Spaces in paths are usually an issue with old batch scripts. Have you tried to hardcode values in the vars just for testing purposes?
    – TheNomad
    Nov 26 '20 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.