2

I am trying to fix the following problem through a batch file.

test is a parent folder, which contains further subfolders test1, test2, test3. All these subfolders test1, test2, test3 contain some .docx files and a further subfolder archive.

  • Look for existing .docx files in subfolders test1, test2, test3.
  • Copy them to a desired folder named destination.

The following code works fine for the first part of problem:

for /R "C:\test" %%f in (*.docx) do xcopy %%f "C:\Users\%USERNAME%\Desktop\destination\"  /exclude:c:\test\not_required.txt

Now I want to move the .docx files from subfolders test1, test2, test3 to their respective subfolder archive. Till now I could only built the following code for the second part of my problem.

for /d /r "c:\test" %%a in (*) do (
if /i "%%~nxa"=="archiv" set "folderpath=%%a"
move "C:\test\test1\test1.docx" "%folderpath%" 
)

As you can see I have given for source a static file move C:\test\test1\test1.docx. I am not sure how to use more variables inside loops and Unfortunately it is not working as desired. A piece of advice from some experts would be highly appreciated.

  • Do you want to move the files to the archive directory before or after the copy? – DavidPostill Jul 2 '17 at 10:56
  • From my understanding I have to give a specific path as a destination for move command. Does it works with wildcards? – Talha Rehman Jul 2 '17 at 11:03
0

The problem with your approach is that the For /R will also dive into the archive folders so this has to be avoided.

If your folder structure has no varying depth you can use a for /d with a wildcard to get only direct subfolders of C:\test

With this sample situation:

> tree /F
C:\
├───Test
│   ├───test1
│   │   │   Example_8192.docx
│   │   │   Example_32457.docx
│   │   │
│   │   └───archive
│   ├───test2
│   │   │   Example_14218.docx
│   │   │   Example_20916.docx
│   │   │
│   │   └───archive
│   └───test3
│       │   Example_12174.docx
│       │   Example_9168.docx
│       │
│       └───archive
└───Users
    └───UserName
        └───Desktop
            └───Destination

This batch using for variable modifiers :

:: C:\Copy+Archive.cmd
@Echo off
For /D %%D in ("C:\test\*") do (
    Echo Processing %%D
    For %%F in ("%%~fD\*.docx") do (
        Echo Copying "%%~fF" "C:\Users\%USERNAME%\Desktop\Destination\"
        xcopy "%%~fF" "C:\Users\%USERNAME%\Desktop\Destination\" >Nul && (
        Echo Moving  "%%~fF" "%%~dpFarchive\"
        Move  "%%~fF" "%%~dpFarchive\" >Nul
        )
    )
)

will produce this result

> tree /f
C:\
│   Copy+Archive.cmd
│
├───Test
│   ├───test1
│   │   └───archive
│   │           Example_8192.docx
│   │           Example_32457.docx
│   │
│   ├───test2
│   │   └───archive
│   │           Example_14218.docx
│   │           Example_20916.docx
│   │
│   └───test3
│       └───archive
│               Example_12174.docx
│               Example_9168.docx
│
└───Users
    └───UserName
        └───Desktop
            └───Destination
                    Example_8192.docx
                    Example_32457.docx
                    Example_14218.docx
                    Example_20916.docx
                    Example_12174.docx
                    Example_9168.docx

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.