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

Author Topic: Copying folders/files with name changes  (Read 4443 times)

0 Members and 1 Guest are viewing this topic.

Thiophilos

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 10
    Copying folders/files with name changes
    « on: April 21, 2019, 10:57:41 AM »
    I want to copy folders and files to a new location at a higher level, with the folder name becoming the name of the parent folder. The files are JPG files in an Export folder. Example:

    Source

    C:\Source\A\Exports\*.jpg
    C:\Source\B\Exports\*.jpg
    C:\Source\C\Exports\*.jpg
    C:\Source\D\Exports\*.jpg
    ...

    Desired end result

    C:\Target\A\*.jpg
    C:\Target\B\*.jpg
    C:\Target\C\*.jpg
    C:\Target\D\*.jpg
    ...

    There are many sources and hundreds of folders. I'm doing this manually now and it is tedious. It would be real nice if I could run a batch file for each Source to do the copying. Currently I'm experimenting with XCopy in a batch file but am not getting very far. Suggestions appreciated.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Copying folders/files with name changes
    « Reply #1 on: April 21, 2019, 12:18:49 PM »
    May I ask if you ever work in another kind of script language?
    Yes, you can do it in batch, but it becomes rather hard to visualize for me.

    If a scrip like  Vb script were used, one could break  the task into smaller steps.After each step, inspect the output to see if it is good and move onto the next step. After it is all done, you would see how it could be  integrated.

    In a batch file, you might consider using another tool, like the robocopy because it has good error recovery.  :)
    https://www.computerhope.com/robocopy.htm
    Does that help?  :)

    Thiophilos

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 10
      Re: Copying folders/files with name changes
      « Reply #2 on: April 21, 2019, 02:03:32 PM »
      Thanks! No, I have no experience with another script language, but have some experience with VBA (in Excel).

      I'm fine with going to Robocopy but for now see it as the same as xcopy. I would need to know how to get folder paths, parse out the relevant folder name, go to each folder sequentially, and so on. I have spent time on the Web trying to get this info and haven't succeeded. Could you point to a resource?

      And I'm also ready to delve into Vb script if you recommend that, but that is terra incognita for me.

      Thiophilos

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 10
        Re: Copying folders/files with name changes
        « Reply #3 on: April 21, 2019, 02:38:21 PM »
        I decided (with some trepidation) to have a look at VBScript. Went to some introduction pages on the Web. Followed the instructions, created sample *.html files, nothing works.

        These things make me feel like I have the intelligence of a three year old. :(

        Followed the instructions in three web sites, the latest being https://www.wikihow.com/Program-with-VBScript. No, it doesn't work.
        « Last Edit: April 21, 2019, 02:56:53 PM by Thiophilos »

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Copying folders/files with name changes
        « Reply #4 on: April 21, 2019, 03:37:29 PM »
        My microphone is open
        sorry if I lead you on the wrong track with my suggestion about VBScript.
        Let's just stick with what you already know about batch file programs. The most powerful tool in back programming is arguably the FOR statement. With that statement you get some special variables that contain the drive letter, the pathname, the filename and the file extension plus some other things needed for file manipulation.
        I am rather slow to respond because of my personal limitations, meanwhile somebody else to come in here and give you need quick answer to your question.
        I will be back in a few minutes.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Copying folders/files with name changes
        « Reply #5 on: April 21, 2019, 04:05:49 PM »
        This is from dictation.
        It looks like you are trying to do two things at the same time. You want to copy a bunch of JPEG files to another directory, and at the same time rename the files so they indicate where they came from. Perhaps this is necessary in order to prevent duplicate filenames. Also, it might be necessary because you want a historical record of where the files came from. In either case, it's easier for me to think of it as being a two-step or even three-step process.
        For the first step I would imagine copying all those files and the directory tree to another location on your hard drive. It would have to be either another partition or a new directory that has a unique name.
        For the second step each file would be renamed to reflect some information about what parent directory it was found in. However, the file should still be in different directories in the new location. So the last step would be to move the files to the top-tier level of the new directory or partition you created. Moving a file is much easier than making a copy of the file. I mean to say it takes less processing time.
        After the process is done you would have all your JPEG files copied into a unique directory with filenames that indicate where they came from. The original JPEG files would still be where they were to begin with. There might be a question about your drive space, but I'm assuming that you have plenty of drive space for this project.
        Again, I have to apologize for being so slow. I do not have the speed and agility to code stuff like I used to years ago. And I found that thinking something through before you start writing code is always the best way to go.
        End of dictation.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Copying folders/files with name changes
        « Reply #6 on: April 21, 2019, 04:20:32 PM »
        In a batch file inside of the "source" folder.
        Code: [Select]
        for /D %%d in (*) do xcopy "%%d\Exports\*.*" "..\Target\%%d" /i /s
        I was trying to dereference Null Pointers before it was cool.

        Thiophilos

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Windows 10
          Re: Copying folders/files with name changes
          « Reply #7 on: April 21, 2019, 04:28:25 PM »
          Thanks, Geek. I too am time-bound just now. Just wanted to mention that I am not changing the names of the files, but of the folders. The JPGs do not change.

          And BC - looks tantalizing. I will have a close look when I get a chance.

          Thiophilos

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Windows 10
            Re: Copying folders/files with name changes
            « Reply #8 on: April 21, 2019, 11:07:18 PM »
            Thanks, BC_Programmer. I just had an opportunity to look at this, reviewed the workings of a for loop, modified the target part a bit to suit, ran it, and it works. First time, no errors. Thanks for your help. This will be very useful.


            In a batch file inside of the "source" folder.
            Code: [Select]
            for /D %%d in (*) do xcopy "%%d\Exports\*.*" "..\Target\%%d" /i /s

            Thiophilos

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Windows 10
              Re: Copying folders/files with name changes
              « Reply #9 on: April 22, 2019, 11:17:38 AM »
              In case anyone is interested, here is a bit of background on why I wanted this utility.

              I'm an amateur photographer and shoot raw files, which are processed in Lightroom (LR). Once edited to satisfaction, the files are exported out of LR as JPG files. Typically they go into a subfolder of the folder holding the raw files. There can be many variants on the file system, but this one works well for many of us. It is a good system - photos are logically stored and can be found back later.

              But there are times when one wants to pull those JPGs out of the folder tree and put them somewhere else. For instance if I am travelling, I might want to have the JPGs along but don't need or want the raw files. Copying the whole tree just to have the JPGs can be done, but that is a lot of stuff! My 2019 tree currently takes up 56 GB. The 2019 JPGs are only 156 MB.

              Of course it is easy to copy files with a particular extension out of a folder tree, but the challenge here was to retain the descriptive names of the parent folders. That is achieved in the solution provided by BC_Programmer. Thanks!

              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              Re: Copying folders/files with name changes
              « Reply #10 on: April 22, 2019, 06:25:35 PM »
              Good news...Nice Work BC...
              " Anyone who goes to a psychiatrist should have his head examined. "