1.

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

Answer» <html><body><p>I use following command to copy files to folders with same name.<br/> Code: <a>[Select]</a>for /f %x in ('dir /ad /b') do move %x*.* %x\I use <em>move %x*.* %x</em> to move the files to <a href="https://interviewquestions.tuteehub.com/tag/given-473447" style="font-weight:bold;" target="_blank" title="Click to know more about GIVEN">GIVEN</a> 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 <em>move </em>should be<em> move "%x*.*" "%x\"  </em>to cater for folder names with spaces.<br/>and if running from within a batch file, all your %'s should be %%<br/><br/>but as written, I doubt it'll do what you want.<br/>the /b switch says only show folder name - nothing else, so you'd get something like;<br/>Windows<br/>Program Files<br/>Users<br/><br/>and the move will equate to <em>move Users*.* Users\ </em>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.<br/><br/> Quote</p><blockquote>Imagefiles - 2015 trips<br/>Imagefiles - 2016 trips<br/>Imagefiles - goodphoto</blockquote> <br/>Filenames are in a format like these:<br/> Quote<blockquote>Imagefiles - 2015 trips - France - 01.jpg<br/>Imagefiles - 2015 trips - Sweden - 10.jpg<br/>Imagefiles - 2016 trips - Sweden - 11.jpg<br/></blockquote> <br/>for /f %x in ('dir /ad /b') do move "%x*.*" "%x\" just cuts the first part of name of folder and says, "Cannot move <a href="https://interviewquestions.tuteehub.com/tag/multiple-1105557" style="font-weight:bold;" target="_blank" title="Click to know more about MULTIPLE">MULTIPLE</a> 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.<br/>where you have "%x*.*" that with would <a href="https://interviewquestions.tuteehub.com/tag/parse-2210717" style="font-weight:bold;" target="_blank" title="Click to know more about PARSE">PARSE</a> to "Imagefiles - 2015 trips*.*" and then you try to move it to "Imagefiles - 2015 trips\"<br/><br/>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?  <br/><br/>the first move argument should probably be "%x<strong>\</strong>*.*" for starters.<br/><br/>and is this done from within a .BAT file?<br/>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 <em>begin</em> with the same name as a folder into that folder. That's a seemingly minor detail that changes the solution a lot.<br/><br/> Code: <a>[Select]</a>for /f %x in ('dir /ad /b') do move "%x*.*" "%x\"<br/>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:<br/><br/> Code: <a>[Select]</a>for /f "DELIMS=" %x in ('dir /ad /b') do move "%x*.*" "%x\"<br/><br/><br/>You can also get a simpler command by using for /D which will iterate over directories:<br/><br/> Code: <a>[Select]</a>for /d %P in (*.*) do move "%P*.*" "%P"<br/>Thank you for your <a href="https://interviewquestions.tuteehub.com/tag/help-239643" style="font-weight:bold;" target="_blank" title="Click to know more about HELP">HELP</a> and also for explanation!</body></html>


Discussion

No Comment Found