midiboy Posted August 23, 2011 Share Posted August 23, 2011 Hi guys,maybe someone can help ... i would like to copy directories containing a defined set of names to a location using robocopy. So instead of using 20 robocopy commands I would like to use one line using the "for" command to cylce through a specific directory and copy only those directories matching a list of names. Lets say the path is D:\Data and contains subdirectories like "001, 002, 003, 004 etc. Maybe these directories also contain subdirectories like 0001, 0002, etc.I would like to have a list of directories in a file like001,0020001and the for command should just copy the content of directories matching the above names in the list.Would that be possible or not ? Is there anyone able to help me with the solution ?Thanks a lot in advance !Alex Link to comment Share on other sites More sharing options...
jaclaz Posted August 23, 2011 Share Posted August 23, 2011 Would that be possible or not ? Yes.Is there anyone able to help me with the solution ?Yes.http://homepages.tesco.net/J.deBoynePollard/FGA/questions-with-yes-or-no-answers.html A Moderator may want to move this post to:http://www.msfn.org/board/forum/66-programming-c-delphi-vbvbs-cmdbatch-etc/Open a command prompt.In it type:DIR /B /S /AD D:\DATA [ENTER]What do you see?Now type in it:FOR /F %A IN ('DIR /B /S /AD D:\DATA') DO @ECHO I'm in a FOR LOOP - %A[ENTER]What do you see?Now go here and read these:http://www.robvanderwoude.com/ntfor.phphttp://www.robvanderwoude.com/ntfortokens.phpYou should now have all the basics needed. Try writing a batch with the info you just learned and with the robocopy command you would use manually, and we'll see what kind of enhancements (if any) we can suggest.jaclaz Link to comment Share on other sites More sharing options...
bphlpt Posted August 23, 2011 Share Posted August 23, 2011 One enhancement will be to use recursion to get to the subdirectories 0001, 0002, etc.Cheers and Regards Link to comment Share on other sites More sharing options...
jaclaz Posted August 23, 2011 Share Posted August 23, 2011 One enhancement will be to use recursion to get to the subdirectories 0001, 0002, etc.Cheers and RegardsWhat do you think the posted FOR loop example does, if not recursion? Or are you meaning the recursion (already) given by the /S parameter to DIR? jaclaz Link to comment Share on other sites More sharing options...
gunsmokingman Posted August 23, 2011 Share Posted August 23, 2011 I wrote this Demo script for you to try. You will have to add the folder name you want to check in this spot in the script.Dim Chk :Chk = Array("Folder1", "Folder2")Save As Demo_RecursiveFolder.vbsDim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> Varibles For Run TimeDim Arg, Dir, Obj, Ts, Txt, Var'-> Array To Hold Folder Name To Check ForDim Chk :Chk = Array("Folder1", "Folder2")'-> Arg To Check If Any Matching Folder Found Arg = False'-> Script ReportTxt = Fso.GetFolder(".").Path & "\Report.txt"Set Ts = Fso.CreateTextFile(Txt) Ts.WriteLine " Scan Time : " & Time() Ts.WriteLine " Scan Date : " & Date()'-> Start Recursive From Script Current Location'-> Recursive(Fso.GetFolder(".")) Or Add A Full Path'-> EG Recursive(Fso.GetFolder("C:\SOME\FOLDER\PATH")) Recursive(Fso.GetFolder("."))'-> End Of The Script Report Ts.WriteLine Var Ts.Close '-> Arg Check If Match Found If Arg = True Then'-> Open Report To Read CreateObject("Wscript.Shell").Run("Notepad " & Chr(34) & _ Txt & Chr(34)),1,True'-> To Keep Or Delete If MsgBox("Did You Want To Keep This File?",4132,"Keep Or Delete") = 7 Then Fso.DeleteFile(Txt),True End If Else'-> No Match Found MsgBox "Could Not Find Any Matching Folder In This Location" & vbCrLf & _ Fso.GetFolder(".").Path, 4128,"No Match Found" Fso.DeleteFile(Txt),True End If'-> Recursive Threw All Sub Folders Function Recursive(Folder)'-> Loop Threw Check Folder And Check For A Match For Each Obj In Chk'-> Check For Folder Name Case Insensitive If InStr(1,Folder.Path,Obj,1) Then Arg = True Var = Var & vbcrlf & _ "----------------------------------" & _ vbCrLf & "Match Found : " & Folder.Path & vbCrLf & _ "----------------------------------" & vbCrLf End If Next'-> Loop Threw Sub Folders For Each Dir In Folder.subFolders Recursive(Dir) Next End FunctionChange Name From Demo_RecursiveFolder.vbs.txt to Demo_RecursiveFolder.vbs to make activeDemo_RecursiveFolder.vbs.txt Link to comment Share on other sites More sharing options...
Yzöwl Posted August 23, 2011 Share Posted August 23, 2011 Unless you provide a genuine path with genuine directory names the solution you receive will only be generic and not specific. Generic solutions are prone to problems specific to you. Link to comment Share on other sites More sharing options...
bphlpt Posted August 24, 2011 Share Posted August 24, 2011 (edited) Or are you meaning the recursion (already) given by the /S parameter to DIR? No, I meant like this:@ECHO OFF & SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET _DirList=DesiredDirectories.txtCALL :f_CopyDefinedDirs SpecificDir1 SpecificDir2 SpecificDir3EXIT:f_CopyDefinedDirs %_DirNames%SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION:s_DoUntilNoMoreDirsToCheckIF ['%1']==[''] ENDLOCAL&EXIT /B 0IF EXIST %1 ( FINDSTR /i /x "%~n1" %_DirList%> nul && IF %ERRORLEVEL% EQU 0 (REMREMREM Do your RoboCopy thing hereREM 'You might want to check to make sure the directory is not empty of filesREM first, but that is left as an exercise for the reader'REMREM ) FOR /F "tokens=*" %%G IN ('dir /-B /AD %1') DO IF EXIST %1\%%G (CALL :f_CopyDefinedDirs %1\%%G))SHIFT & GOTO s_DoUntilNoMoreDirsToCheckNote: DesiredDirectories.txt should have each desired directory listed one per line. as in the OP's example. And yes I know that more error handling might be required depending on the directory structure he has to deal with.Cheers and Regards Edited August 24, 2011 by bphlpt Link to comment Share on other sites More sharing options...
midiboy Posted August 24, 2011 Author Share Posted August 24, 2011 Guys !You are soooo wonderful. I didnt except soo much responsive!I will look through your answers in detail, I am sure I will have some questions ...I will get back to you, thanks sooo much for your help ! Have a great day !Alex Link to comment Share on other sites More sharing options...
midiboy Posted August 24, 2011 Author Share Posted August 24, 2011 Hi again,@gunsmokingman: thanks a lot for your vbs script. I would prefer to do it within a batchfile because the process is part of a larger already existing cmd backup script. But if I dont find a good way to do it within a cmd I will come back to your solution !@Yzöwl: Attached is a screenshot of the directorystructure that I am trying to copy. Those are shadowcopy mountpoints created by DPM (Dataprotectionmanager) so one can copy files from DPM to for instance external Harddrives via non VSS aware scripts or applications. I do not have space on my external HD for all the mountpoints so my intention was to have a list of mountpoints that will then be copied from the directory structure to the USB harddisc like:vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0vol_a6f0c2c5-39d7-4e77-ae3b-b8f4fc835b8fvol_1d53783a-0dcf-4d62-8f11-19c297af3de2etc.The script should go over the complete structure below the "C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy" Source directory and copy everythiing below the mountpoints mentioned inside the list.@bphlpt:Thanks so much for your script. Before I can test it ... robocopy expects a source to copy from ... where is this source variable in your script. I am amazed at your script, I can hardly understand every part of it so please excuse if I didnt check the obvious Thanks guys and sorry for being such a lame a** with scripting .. I hope to learn more ! Alex Link to comment Share on other sites More sharing options...
jaclaz Posted August 24, 2011 Share Posted August 24, 2011 (edited) ... robocopy expects a source to copy from ... where is this source variable in your script. I am amazed at your script, I can hardly understand every part of it so please excuse if I didnt check the obvious Thanks guys and sorry for being such a lame a** with scripting .. I hope to learn more ! IMHO in order to learn you need to start from the bottom and go up (as opposed to from the top and going down).Say that you have ONLY contents oof a single folder to copy over (le'ts use this as an example):C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0WHICH robocopy command line would you use?jaclaz Edited August 24, 2011 by jaclaz Link to comment Share on other sites More sharing options...
midiboy Posted August 24, 2011 Author Share Posted August 24, 2011 Hi jaclaz,well my robocopy command would be like this:robocopy "%DPMSource%\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0" "%USB%\DPM\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0" %roboparametermirror%Variables being:DPMSource: Path to DPM Shadowcopy VolumeUSB: Path to USB driveletterrobocopyparametermirror: /E /ZB /MIR /XD "System Volume Information" /XF "*.mp*" "*.avi" "*.mkv" "*.wm*" "*.m4*" /LOG+:"%LOG%" /TEE /NP Link to comment Share on other sites More sharing options...
Yzöwl Posted August 24, 2011 Share Posted August 24, 2011 Try something like this:@ECHO offSETLOCAL enableextensions disabledelayedexpansionREM Set the two variables belowSET "LISTOF=Path to file with search list"SET "USB=Path to USB drive"SET "BRANCH=C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy"(SET RCOPTS=/E /ZB /MIR)(SET RCOPTS=%RCOPTS% /XD "System Volume Information")(SET RCOPTS=%RCOPTS% /XF "*.mp*" "*.avi" "*.mkv" "*.wm*" "*.m4*")(SET RCOPTS=%RCOPTS% /LOG+:"%LOG%" /TEE /NP)SET "DESTIS=%USB%\DPM"PUSHD %BRANCH%FOR /d /r %%# IN (*) DO ( ECHO=%%~nx# | FINDSTR/ixg:"%LISTOF%" >NUL 2>&1 && (CALL :CopyIt %%#))POPDGOTO :Eof:CopyItROBOCOPY "%*" "%DESTIS%" %RCOPTS% Link to comment Share on other sites More sharing options...
jaclaz Posted August 24, 2011 Share Posted August 24, 2011 Hi jaclaz,well my robocopy command would be like this:robocopy "%DPMSource%\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0" "%USB%\DPM\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0" %roboparametermirror%Variables being:DPMSource: Path to DPM Shadowcopy VolumeUSB: Path to USB driveletterrobocopyparametermirror: /E /ZB /MIR /XD "System Volume Information" /XF "*.mp*" "*.avi" "*.mkv" "*.wm*" "*.m4*" /LOG+:"%LOG%" /TEE /NPGood. I guess we can change/semplify it to:robocopy "%DPMSource%" "%USB_drive%\DPM\%DPMname%" %roboparametermirror%Now let's assume that you have a plain text file like the following list.txt:::Hello I am a senseless header, here only to show how you can skip lines in a FOR /F loopC:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\vol_9edf29cf-c89a-4fe5-97d6-7c9ce70c13b0And a batch file:@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET USB_drive=U:SET roboparametermirror=/E /ZB /MIR /XD "System Volume Information" /XF "*.mp*" "*.avi" "*.mkv" "*.wm*" "*.m4*" /LOG+:"%LOG%" /TEE /NPFOR /F "skip=1 tokens=* delims=" %%A IN (list.txt) DO (SET DPMSource=%%ASET DPMName=%%~nAECHO robocopy "!DPMSource!" "%USB_drive%\DPM\!DPMname!" %roboparametermirror%)What happens?What happens when you add some more source directories to list .txt and re-run the batch?jaclaz Link to comment Share on other sites More sharing options...
bphlpt Posted August 24, 2011 Share Posted August 24, 2011 @bphlpt:Thanks so much for your script. Before I can test it ... robocopy expects a source to copy from ... where is this source variable in your script. I am amazed at your script, I can hardly understand every part of it so please excuse if I didnt check the obvious jaclaz is trying to take you through the steps to learn the why and how, (kind of like school) so the next time you have a similar task you will know how to create the script for yourself. And Yzöwl obviously knows MUCH more about robocopy than I do. I would STRONGLY advise listening to them both and going through EVERY step they suggest. Consider it an honor that these teachers both are willing to spend the time with you.To answer your question, in my script, "%1" would be the robocopy source, but its completeness and appropriateness as a path is dependent on how complete the "SpecificDirx" is. "%~n1" is the specific name that matched one of the members of your list. Yzöwl did a much more thorough job than I did on breaking down the robocopy command elements and the source and destination paths to make them all easier to handle.Cheers and Regards Link to comment Share on other sites More sharing options...
midiboy Posted August 27, 2011 Author Share Posted August 27, 2011 Hi guys,again, thanks for your help on this.First I tried Yzöwl´s solution. I think it doesnt work because I am looking for names in the listfile that exist somewhere in the path not exactly at the top of the path in "C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy"When I run that script I get a long running script with thousands of lines of output like this:C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\Microsoft Hyper-V VSS Writer>(ECHO=68EFFFA4-BAA3-4533-8627-D01B4A018078 | FINDSTR/ixg:"D:\Scripts\USB_Backup\shadowcopieslist.txt" 1>NUL 2>&1 && (CALL :CopyIt C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\Microsoft Hyper-V VSS Writer\vol_fa8b864b-234c-4868-bd22-08b783b46fe6\c6f637b2-cdc2-4d4c-9e00-b48e200d296d\Full\D-Vol\secdom\Virtual Machines\68EFFFA4-BAA3-4533-8627-D01B4A018078 ) )C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\Microsoft Hyper-V VSS Writer>(ECHO=secdom | FINDSTR/ixg:"D:\Scripts\USB_Backup\shadowcopieslist.txt" 1>NUL 2>&1 && (CALL :CopyIt C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\Microsoft Hyper-V VSS Writer\vol_fa8b864b-234c-4868-bd22-08b783b46fe6\c6f637b2-cdc2-4d4c-9e00-b48e200d296d\Full\E-Vol\secdom ) )My List of files looks like this:Database BackupsDPMDBScriptsAcronisMicrosoft Exchange Writerserv22boomerang2devtest02UATWINServ32EX2010TMG2010Non VSS Datasource WriterSqlServerWriterThese are directory names that exist somewhere in the directory structure below "C:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy"Then I tried Jaclaz solution:At first I wasnt sure if the line ECHO robocopy "!DPMSource!" "%USB_drive%\DPM\!DPMname!" %roboparametermirror%Was correct or not ... shouldnt it be ECHO robocopy "%DPMSource%" "%USB_drive%\DPM\%DPMname%" %roboparametermirror%I tried both versions but the "!" instead of the "%" didnt work.But even with the second version and a list file of just one line likeC:\Program Files\Microsoft DPM\DPM\Volumes\ShadowCopy\Database BackupsI get this output:D:\Scripts\USB_Backup>FOR /F "skip=1 tokens=* delims=" %A IN (D:\Scripts\USB_Backup\shadowcopieslist.txt) DO (SET DPMSource=%A SET DPMName=%~nA ECHO robocopy "" "G:\DPM\" /E /ZB /MIR /XD "System Volume Information" /XF "*.mp*" "*.avi" "*.mkv" "*.wm*" "*.m4*" /LOG+:""G:\copylog.txt"" /TEE /XD " /NP)Seems like the variables are not being read from the list file. Hope you can help some more ... If you have some time that is !Have a nice saturday evening guys !Alex Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now