Page 1 of 1

Help with moving filetype recursively

Posted: 22 Jan 2015 17:16
by machone
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...

Re: Help with moving filetype recursively

Posted: 22 Jan 2015 19:12
by foxidrive
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

Re: Help with moving filetype recursively

Posted: 22 Jan 2015 23:34
by machone
Thanks, will give it a try!