Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help copying folder structure with bat
New Posts  All Forums:Forum Nav:

Help copying folder structure with bat

post #1 of 7
Thread Starter 
Hi folks, I've been working on a "simple (to you , not to me) bat file that will identify, list, and copy files in a destination directory that do not exist in the source, to a folder named deleted. I can identify the extra files, create a dated folder and copy them there. However, I can't figure out how to maintain the file structure. It seems like I need some for /d or for /r command but I don't quite understand how they work. For reference, the below will find and copy files in subdirectories, but it does not copy the subdirectory. From what little I have been able to figure out searching the web, I need some sort of loop but I don't know how to do it.

I'd love some help. Ideally, a folder named deleted would be added to every root and subdirectory within that root, and the extra files in each destination folder would be copied to the dated folder within that folder. I'd also like each delete folder to be named based on its parent, as in .\\images\\images_deleted

Code:
echo Copies files in destination missing from source to folder "Deleted".

@echo off

::variables
set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%
set source=b:\est1
set dest=d:\est2

setlocal EnableDelayedExpansion 
SETLOCAL ENABLEEXTENSIONS


if exist "%dest%\\extra.txt" del %dest%\\extra.txt

if exist "%dest%\\deleted\\%folder%" goto :copy else do (
   mkdir %dest%\\deleted\\%folder%
)                                                           

:copy

robocopy "%source%" "%dest%" /e /xd %dest%\est2\\deleted /xc /xn /xo /xl /nc /NP /NS /NJH /NJS /FP /L /log:%dest%\\extra.txt   ::this outputs a txt file with full path names of only extra files

for /f "tokens=*" %%a in (%dest%\\extra.txt) do (
  XCOPY /y "%%a" %dest%\\deleted\\%folder%
)
:: ^^^does not copy folder structure but copies all files in all folders to dated folder, even if subdir is already present in deleted\\%folder%.::
::  Only works with "tokens=*"::

robocopy %source% %dest% /mir /xd "%dest%\\deleted"

@pause
My folder structure explanation:
I want to have two directories or drives, one a manually mirrored copy of the other. Aside from the _deleted folders and their contents, I have this structure now using robocopy.

Source:
d:\\data
-------\\images
--------------\\*.jpg, etc.
-------\\documents
---------------\\*.txt, etc

Destination:
g:\\data
-------\\images
--------------\\*.jpg, etc
--------------\\images_deleted\\
------------------------------\\date_deleted\\previouslydeleted.jpg
--------\\documents
---------------\\*.txt, etc
---------------\\documents_deleted
------------------------------\\date_deleted\\previousdeleted.txt


Hope all that made sense and thanks in advance!
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
post #2 of 7
i could write a unix shell script to do that in just a couple lines. if you can install cygwin I would recommend that any day over dos. http://en.wikipedia.org/wiki/Cygwin

if you HAVE to do it in dos, good luck. the for and find commands are both really funny
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #3 of 7
Thread Starter 
Thanks for the offer and I would appreciate it. I am very, very new to any kind of scripting or coding language. I'd appreciate it if you could do this for me and I have no problems installing cygwin. I'd still like to learn how to make the dos work though, for my own edification, but I am ready and interested in learning anything I can at this time. I do not find languages intuitive but I believe I can understand them if they are explained well.

Thanks!
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
post #4 of 7
Thread Starter 
Well, I have continued to play with this but rather unsuccessfully since I'm a total noob. Here is what I've come up with though:

If I use for to list all folders within destination root directory, that list can be compared to the existing folders in source root. This code works for this:
Code:
FOR /D %%G in (%dest%\\*) do echo %%G>>%dest%\\dir.txt  
set dir=%dest%\\dir.txt
The issue is that this contains my deleted folder. If I use for and find to list all strings in the first txt file, the broken nature of find produces the right file list, but it does so twice.

Code:
for /f %%a in (^"%dir%^") do (
   find /i /v "deleted" %dir% %%a>>%dest%\\dir2.txt
   set dir2=%dest%\\dir2.txt
)
produces:
Code:
---------- D:\\TEST2\\DIR.TXT
d:\est2\ry  
d:\est2\ry space  

---------- D:\\TEST2\\DIR.TXT
d:\est2\ry  
d:\est2\ry space
It seems like the solution to the problem is being able to limit a for command. If I could say, "do this for all folders listed in the fileset except for folders with *\\deleted\\*", this would be easier. I could then loop that output into a robocopy or xcopy command- something I also don't know how to execute correctly.


Thoughts or help appreciated.
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
post #5 of 7
Why not use xcopy instead of copy.
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
post #6 of 7
Thread Starter 
Good question. While it is likely due to my lack of knowledge, I can't get xcopy to follow the directory tree w/o prompting to create new folders, even with /y that supposedly turns prompting off. I also can't exclude a directory with xcopy but need robocopy for that. I've tried using if exist to ignore folders, but you can't use wildcards under if.
A further issue with robocopy is that it reads a file as a directory and errors out. I've tried all manner of qoutes w/o luck.
I think im having issues due to my lack of knowlege on how to properly set a string in text to a variable as well as how to pass variables into commands.

Here is where I am stuck n I have file.txt with folder names, some of which have spaces.
a:\\deleted
a:\\folder1
a:\\folder folder
a:\\deleted_date
a:\\deleted\\folder_date.

I want to copy each folder that does not exist on B:\\ and does not contain the word deleted. In the example, a:\\ (%dest%) is a backup of source b:\\.
Code:
 
For /f "usebackq delims=" %%a in (file.txt) do (
   if /i not exist %source%\\%%~nxa robocopy %%a %dest%\\deleted\\%date%\\%%~nxa_%date% /e /xd "deleted" 
)
Does not seem to read things right. Also, the /xd does not seem wildcarded correctly.

Help?
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
Alpha v.2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740be @ 3.63 (16.5 x 220) ASUS M3A78-CM XFX HD4850 Crucial 2x2GM DDR2-800 6-6-6-18 
Hard DriveOptical DriveOSMonitor
WD Black 500GB OS/Programs, WD 1Tb + 800 GB data Sony DVD-RW Win 7x64 Professional Dell 22" 
PowerCase
Antec Earthwatts 650 Antec 300 w/5 fans 
  hide details  
Reply
post #7 of 7
i started on a shell script to do this the other day.. it turned into more than a couple lines it's about halfway done, but it's going to take me some time to create some good test data and make sure i've got all the bases covered.

1985 msdos, again i flat out refuse to help. i didn't even read.

have you thought about an alternate solution? a virtual disk with D: and E: ? rsync? source control? surely you're not the first person that wanted to do something like this. I'm sure there's an 'industry standard' solution out there to sync two dirs and put files that got deleted somewhere.

for now you could write an inefficent script
move dest-dir/deleted <somewhere else>
delete dest-dir/<every file present in source_dir> (idk about this in dos)
move dest-dir/* dest-dir/deleted
copy source-dir/* dest-dir
move <somewhere else>/deleted dest-dir

just for kicks here's the script i've got so far
Code:
#!/bin/bash

set -e

if [ $# -ne 3 ] ; then
  echo "Usage: ${0} <source_dir> <dest_dir>"
fi

SOURCE_DIR="${1}"
DEST_DIR="${2}"

DATE_FORMAT="+%Y-%m-%dT%H-%M"
DATE=`date "${DATE_FORMAT}"`
TRASH_DIR="${DEST_DIR}/deleted/${DATE}"

moveToTrashIfDeleted()  {
  if [ ! -e "${SOURCE_DIR}/${1}" ] ; then # have to filter out the 'deleted' dir
if [ ! -d ${TRASH_DIR} ] ; then
  mkdir -pv "${TRASH_DIR}"
fi
mv -v "${DEST_DIR}/${1}" "${TRASH_DIR}"
  fi
}


find "${DEST_DIR}" -exec moveToTrashIfDeleted {} +  #i dont think this -exec flag works right
cp -av "${SOURCE_DIR}" "${DEST_DIR}"
exit 0
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help copying folder structure with bat