Help with moving filetype recursively

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

Help with moving filetype recursively

#1 Post by machone » 22 Jan 2015 17:16

I've got a top level folder with many subfolders. Inside each of those subfolders are several .bak files.
How do I go about running a batch script from the top level folder, which will find all .bak files in each subfolder and then move those .bak files into a "backup" subfolder inside each of those folders? (The "backup" subfolder may or may not already exist)

For example:

toplevel\subfolder1\filename.bak
toplevel\subfolder2\anotherfile.bak

should become:

toplevel\subfolder1\backup\filename.bak
toplevel\subfolder2\backup\anotherfile.bak

I've been using this script to copy files recursively into subfolders, but when I change 'copy' to 'move' I get "The syntax of the command is incorrect"

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ( ' dir "*.bak" /b /s /a-d ' ) do (
echo processing "%%a"
set "name=%%~nxa"
pushd "%%~dpa"
copy /b /y "%%a" ".\backup\!name:~0!"
popd
)


Not quite sure how to proceed here...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with moving filetype recursively

#2 Post by foxidrive » 22 Jan 2015 19:12

Test this on a folder tree of test files.

I removed the /y switch in the move command as it will overwrite existing files. Put it back if you want that to happen.

Code: Select all

@echo off
for /f "delims=" %%a in ( ' dir "*.bak" /b /s /a-d ' ) do (
echo processing "%%a"
md "%%~dpa\backup" 2>nul
move "%%a" "%%~dpa\backup"
)
pause

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Help with moving filetype recursively

#3 Post by machone » 22 Jan 2015 23:34

Thanks, will give it a try!

Post Reply