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

Author Topic: Move files to folders with same name in Windows  (Read 2307 times)

0 Members and 1 Guest are viewing this topic.

kovacsa

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Move files to folders with same name in Windows
    « on: July 11, 2018, 02:52:53 PM »
    I use following command to copy files to folders with same name.
    Code: [Select]
    for /f %x in ('dir /ad /b') do move %x*.* %x\I use move %x*.* %x to move the files to given directory. It doesn't work for files, which has spaces in their filenames, but not sure what to change. I already tried to use quotes around arguments, but doesn't helped.

    Mark.



      Adviser
    • Forum Regular
    • Thanked: 66
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 10
    Re: Move files to folders with same name in Windows
    « Reply #1 on: July 11, 2018, 06:04:42 PM »
    your move should be move "%x*.*" "%x\"  to cater for folder names with spaces.
    and if running from within a batch file, all your %'s should be %%

    but as written, I doubt it'll do what you want.
    the /b switch says only show folder name - nothing else, so you'd get something like;
    Windows
    Program Files
    Users

    and the move will equate to move Users*.* Users\ which I doubt is the effect you were after - but on that I'm not sure what you are trying to achieve.

    kovacsa

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: Move files to folders with same name in Windows
      « Reply #2 on: July 11, 2018, 11:39:32 PM »
      I have folders with names like following.

      Quote
      Imagefiles - 2015 trips
      Imagefiles - 2016 trips
      Imagefiles - goodphoto

      Filenames are in a format like these:
      Quote
      Imagefiles - 2015 trips - France - 01.jpg
      Imagefiles - 2015 trips - Sweden - 10.jpg
      Imagefiles - 2016 trips - Sweden - 11.jpg

      for /f %x in ('dir /ad /b') do move "%x*.*" "%x\" just cuts the first part of name of folder and says, "Cannot move multiple files to a single file" and syntax errors in some cases.

      Mark.



        Adviser
      • Forum Regular
      • Thanked: 66
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 10
      Re: Move files to folders with same name in Windows
      « Reply #3 on: July 12, 2018, 12:37:12 AM »
      do that dir /ad /b at a command prompt and you just get folders without anything else.
      where you have "%x*.*" that with would parse to "Imagefiles - 2015 trips*.*" and then you try to move it to "Imagefiles - 2015 trips\"

      still don't know what you are trying to achieve - do you want to move all your folder up one level?  or into the same folders?  ???

      the first move argument should probably be "%x\*.*" for starters.

      and is this done from within a .BAT file?

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1108
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 8
      Re: Move files to folders with same name in Windows
      « Reply #4 on: July 12, 2018, 07:32:47 PM »
      The thread title says 'Move files to folders with same name" But the folder and file names you've provided are not the same- You want to move files that have names that begin with the same name as a folder into that folder. That's a seemingly minor detail that changes the solution a lot.

      Code: [Select]
      for /f %x in ('dir /ad /b') do move "%x*.*" "%x\"
      for /f uses space as a delimiter by default. As a result You'll only see the first part of the dir output line line up to the first space. basically you'll go through every folder- but the %x variable will only ever be "Imagefiles" for the set of folders you mentioned. you need to add "DELIMS=" to indicate that you don't want to split the text:

      Code: [Select]
      for /f "DELIMS=" %x in ('dir /ad /b') do move "%x*.*" "%x\"


      You can also get a simpler command by using for /D which will iterate over directories:

      Code: [Select]
      for /d %P in (*.*) do move "%P*.*" "%P"
      I was trying to dereference Null Pointers before it was cool.

      kovacsa

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: Move files to folders with same name in Windows
        « Reply #5 on: July 18, 2018, 02:19:46 AM »
        Thank you for your help and also for explanation!