1.

Solve : Move files to folders with same name in Windows?

Answer»

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.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.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.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?
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"
Thank you for your HELP and also for explanation!


Discussion

No Comment Found