I have about 15k files in a flat folder. All are named something like blah blah - whatever.png. Basically, I'd like to move them all into folders named blah blah. I don't want to rename any of them, just bump them into their respective folders. I imagine I need to do this with some kind of regex, but I can't seem to workout how to apply regex in this context.

  • I just tried for %A IN (*.docx) DO move "%A" "^[^-]*[^ -]/%A" And it didn't work :/ – LincM Sep 19 '18 at 6:14

The for command does not support regular expressions.

Assuming all files contain a - character, you could probably use this code in a batch file:

for /f "delims=- tokens=1* usebackq" %%a in (`dir /b *.png`) do (
    if not exist "%%a" mkdir "%%a"
    move "%%a-%%b" "%%a"

delims=- will split the filenames on -, giving you the directory name which will be created if it doesn't exist yet. Next, the original filename, %%a-%%b, will be moved to that directory.

  • I tried that and the output was to create directories named blah blah - (including the hyphen), and it didn't move anything (presumably because the naming convention of the directory didn't align). I've tried munging through it a bit, but haven't been able to make that work fully. – LincM Sep 19 '18 at 22:44
  • @LincM That is strange. By definition the - shouldn't be part of %%a, because the filename is split on it. Could it be that it's not really a hyphen but another utf dash symbol? – Berend Sep 20 '18 at 7:38
  • I don't believe it's another UTF. What is might do is use this as a batch instead of as a command and see if it works differently. – LincM Sep 20 '18 at 8:04
  • @LincM, yes this code was meant as a batch. I'll add that to my answer – Berend Sep 20 '18 at 8:13
  • Ah, okay well that works. I was hoping to be able to understand the structure of how one may do this directly through commandline instead of running a batch, but this achieves what I'm after, thanks! – LincM Sep 20 '18 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.