Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Copying subdirectories  (Read 2985 times)

0 Members and 1 Guest are viewing this topic.

natron

    Topic Starter


    Newbie

    Copying subdirectories
    « on: February 03, 2010, 03:05:06 PM »
    I am trying to start in a parent directory lets say for example I want to start my .bat program in the C:\CMD directory. I would like to loop through all folders within that directory. Go inside each folder and if the folder name = x copy folder to a certain directory if foldername = y copy it to a different directory.

    I have been able to loop through the files in my example directory but I could never seem to get to the child directory. I hope this makes sense to everyone if it does let me know and I will explain further.

    Thanks.

    BillRichardson



      Intermediate

      Thanked: 15
      Re: Copying subdirectories
      « Reply #1 on: February 04, 2010, 10:00:15 AM »
      I have been able to loop through the files in my example directory but I could never seem to get to the child directory.

      Post your complete batch file.  We need see what you done and the error report.
      Bill Richardson

      gpl



        Apprentice
      • Thanked: 27
        Re: Copying subdirectories
        « Reply #2 on: February 04, 2010, 10:36:48 AM »
        This will give you a hint,
        Code: [Select]
        HELP FOR
        returns this
        FOR /D %variable IN (set) DO command [command-parameters]
            If set contains wildcards, then specifies to match against directory
            names instead of file names.

        Are you sure that you want to enter the directory at each step, when you know the name surely you then know what to do with it ?

        natron

          Topic Starter


          Newbie

          Re: Copying subdirectories
          « Reply #3 on: February 04, 2010, 12:32:45 PM »
          Ok this is what I am looking to do:

          I want to loop through every file in lets say the C:\CMD directory. Here are my folders within that directory:
          C:\CMD\Example1
          C:\CMD\Example2
          I want to open up each folder and see what folders are with in Example1 and Example 2.

          Lets say within Example1 directory there is a Test1 and Test2 folder.
          Within Example2 directory there is a Test1 and Test3 folder.

          I want to be able to loop through each folder in the C:\CMD directory, look within each folder. Then say if folder name = Test1 copy that directory to x location, if folder name = Test2 copy directory to y location and so forth.

          Below is a sample of what I wrote. But it always returns the Example folder names instead of the Test folder names.

          FOR /D %%G IN ("*") DO cmd /c FOR /D %%F IN ("*") DO cmd %%C IN (*) DO IF %%G == "TEST1" xcopy /s %%G C:\copy\

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Copying subdirectories
          « Reply #4 on: February 04, 2010, 12:44:14 PM »
          You code4 is hard to understand and it is not clear what you want to do.
          It is easier to follow if you can make in is steps .
          For example:
          DIR TEST? /b/s
          Does the command give you a list of files and path-names of interest
          Is so, we can work from there.?