RingTailedLemur
RingTailedLemur

Reputation: 1

Batch-file backup in windows, recreating nested folders/files

I currently have a batch file which reads a filelist from folder A, if it's over a certain date copies it to folder B and deletes the original.

The problem is that I would like the batch file to recreate the file structure inside folder A, and I'm not quite sure how to do this. Here's the code:

echo off
echo REM :- THE FOLLOWING FILES ARE OLDER THAN 7 DAYS AND CAN BE MOVED TO BACKUP -:
pause
forfiles /p c:\test\one /s /m *.gif  /c "cmd /c dir /b/s/t:w @path"
echo.
echo REM :- CONTINUE TO MOVE THOSE FILES TO THE BACKUP LOCATION -:
pause
forfiles /p c:\test\one /s /m *.gif  /c "cmd /c mkdir c:\test\two\@relpath"
echo.
pause
forfiles /p c:\test\one /s /m *.gif  /c "cmd /c copy /-y @path c:\test\two\@relpath"
pause
echo.
echo REM :- THE FOLLOWING FILES EXIST IN THE BACKUP LOCATION -:
echo.
pause
dir "c:\test\two" /a/s/b /o:gn
echo.
echo REM :- CONTINUE TO DELETE THOSE BACKUPS -:
pause
rd /s "c:\test\two"
echo.
pause

The problem is that @relpath seems to take the whole path and filename, so in folder B I end up with each filename inside a folder of its own name (e.g. 'filename.gif' inside a folder of the same name). How can I strip the filename from the path, create a file structure in folder B based on that, and then copy the file to the correct place?

Thanks

Upvotes: 0

Views: 874

Answers (2)

Jeff D
Jeff D

Reputation: 3

You could write it like:

forfiles /p c:\test\one /s /m *.gif /c "cmd /c mkdir c:\test\two\@relpath\.."

This would back up the resulting path to the parent folder preventing the filename from being used.

Upvotes: 0

Bali C
Bali C

Reputation: 31241

You can use xcopy to recreate the file structure with the /T switch. You will also need to use the /E switch to copy empty directories and subdirectories.

At a cmd prompt type xcopy /? for help on all the switches. Hope this helps!

Upvotes: 2

Related Questions