0

I am struggling to create a batch file that I can use in any folder, which will copy the current folder and all its contents to another location. The objective is to allow me to make very quick backups of any current folder by running the batch file. Batch files are new to me, but programming isn't. Here is my code.

@echo off
title Copy This Directory
:: get current directory
 set startdir=%cd%
 echo.startdir = %startdir%
:: copy the test folder
 xcopy "%startdir%" "F:\Temp Backup\" /s /y
 pause

This copies the files and folders that are inside the source folder to the new destination, but I want to copy the folder itself together with its contents. ie if the batch file is in a folder called "FromFolder", I want "FromFolder" to appear as a folder in "F:\Temp Backup\" and all its contents in "F:\Temp Backup\FromFolder". I've found lots of info about copying files, but nothing about copying a single directory. I can copy a single file, but when I use the same code and change the file name to a folder name, it copies the folder's contents and not the folder itself. Could someone let me know what I have missed please.

Codeabike
  • 1
  • 1
  • 1
  • So you are planning on running the batch file from inside the folder you want to backup? Or are you planning on doing a drag and drop batch file? Regardless of that, essentially what you are trying to do is get the parent folder of a folder path. This is the essential syntax for getting the parent directory: `for %%G in ("%cd%\.") do set parent=%%~nxG`. – Squashman Nov 16 '17 at 18:46
  • ...or perhaps `"%~dp0."` – Compo Nov 16 '17 at 19:16
  • That helps me get the name of the parent folder. – Codeabike Nov 16 '17 at 19:31
  • Accidentally pressed return and it saved it before my full answer. I want to have a batch file inside a folder that copies the folder that contains it when I double click. Your code helps me get to the name of the parent folder. My real problem is how do I make xcopy copy that single folder and not just the files inside it. So far if I try to copy a folder using the folder name in xcopy, it copies the contents of the folder but not the folder itself. I want to copy the folder and its contents so that the destination then contains the folder which in turn contains the folder's contents. – Codeabike Nov 16 '17 at 19:40
  • @Codeabike, you have the name of the parent folder. So what do you think you need to do with that information in your batch file? 1) Create the directory in your destination folder. 2) Use that folder path in the Xcopy. – Squashman Nov 16 '17 at 20:36

3 Answers3

0
@echo off

rem set destination dir
set destDir=C:\tmp\fizz bang

rem get the current dir
set currDir=%cd%

rem set current dir to parent
pushd ..

rem get the parent dir path
set parDir=%cd%

rem replace the parent path part of the dir with the destination
rem see also: https://stackoverflow.com/a/5821366
rem also note the trailing backslash to tell xcopy it's a directory
CALL set destPath=%%currDir:%parDir%=%destDir%%%\

rem copy
xcopy /e "%currDir%" "%destPath%"

rem go back where we started
popd

pause
Chaz
  • 319
  • 1
  • 6
  • Thanks, these address getting the folder name. My question is how do I copy a directory and it's contents. The batch file equivalent of drag and drop a folder rather than dragging it's contents. ie "xcopy A:\From B:\To\" copies the contents of "From" into "To", whereas I want to copy the directory "From" itself and its contents. Maybe I need to create the destination directory "B:\To\From" as a first step and then copy the contents to it with "xcopy A:\From B:\To\From\" as a second step, but I had expected it to be possible to do it all in one step. – Codeabike Nov 16 '17 at 20:26
  • Have you run the script? Note that it moves to the parent of the current directory, then copies the directory and all it's contents. – Chaz Nov 16 '17 at 20:44
  • Thanks Chaz. At first I was getting an invalid number of parameters error when I tried it because my destination folder had spaces in it and I needed inverted commas. When I edited the destination to have no spaces it worked well, thanks. In the mean time had I tried the two step approach (I posted that here too) which also worked and handles filenames with spaces. – Codeabike Nov 16 '17 at 21:38
  • Mine handles spaces fine as well. – Chaz Nov 17 '17 at 17:56
0

I decided to go with creating a new folder at the destination and as a second step copying the contents to it. Here's what I did.

@setlocal enableextensions enabledelayedexpansion
@echo off
title Copy This Directory
:: get current directory
@echo off
set startdir=%cd%
set temp=%startdir%
set folder=
:loop
if not "x%temp:~-1%"=="x\" (
    set folder=!temp:~-1!!folder!
    set temp=!temp:~0,-1!
    goto :loop
)
echo.startdir = %startdir%
echo.folder   = %folder%
:: Create new folder if needed
md "F:\Temp Backup\%folder%"
:: copy the contents
xcopy "%startdir%\*.*" "F:\Temp Backup\%folder%\" /s /y
pause
endlocal && set folder=%folder%
Codeabike
  • 1
  • 1
  • 1
  • I gave you one line of code to get the parent folder. What was wrong with using it? – Squashman Nov 16 '17 at 21:30
  • Thanks. My difficulty was not getting the folder names, I had already found and tested how to do that. My difficulty was copying the folder as well as the contained files. – Codeabike Nov 16 '17 at 22:12
  • I would have thought that was the easy part. Ever here of the acronym **K.I.S.S.**? – Squashman Nov 16 '17 at 22:15
  • If I knew how to do it I wouldn't have posted and I guess I just didn't find a fragment of code that didn't answer my question useful. – Codeabike Nov 16 '17 at 22:25
0

If you are running the batch file from the directory you want to backup you can literally do this in a handful of lines of code.

@echo off
title Copy This Directory
for %%G in (".") do set folder=%%~nxG
md "F:\Temp Backup\%folder%" 2>nul
xcopy "*.*" "F:\Temp Backup\%folder%\" /s /y
Squashman
  • 13,649
  • 5
  • 27
  • 36