-1

I have a folder with a bunch of .avi and .wav files. Their names are as follows:

{NameX}_{DateX}

I need an expression that creates folders called {Name1}, {Name2}, etc, and then copies the respective files into those folders.

Thanks in advance!

Edit: Sorry, here's what I tried so far.

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
 'dir /b /a-d "*_*_*_*_*_*_*_*.avi"'
 ) DO (  
 MD %%a
 MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF

It didn't work though, and I don't know why.

Here's a concrete filename, I think it might help.

hl2_2014_12_26_04_05_12_268.avi
  • I'm a beginner in batch as I never felt the need to use it, but I messed about with it a bit with the help of [this thread](http://stackoverflow.com/questions/19992530/batch-create-folders-based-on-part-of-file-name-and-move-files-into-that-folder) I cobbled together (more like slightly edited) this: `@ECHO OFF SETLOCAL SET "sourcedir=c:\sourcedir" PUSHD %sourcedir% FOR /f "tokens=1*" %%a IN ( 'dir /b /a-d "*_*_*_*_*_*_*_*.avi"' ) DO ( MD %%a MOVE "%%a %%b" .\%%a\ ) POPD GOTO :EOF` – Tiago Veloso Jan 02 '15 at 20:46
  • @TiagoVeloso - Edit your question and include this code as a starting point for help. – Jason Faulkner Jan 02 '15 at 20:49
  • I did it now; I didn't notice the comments didn't keep formatting. – Tiago Veloso Jan 02 '15 at 20:54

1 Answers1

0

You are really close.

You are just missing a delimiter in your FOR statement (if you omit the delims parameter it will use a space as the default) to break the name apart by an underscore. Then you need to "reassemble" the file name back in your MOVE statement by combining the tokens.

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
PUSHD %sourcedir%

REM Use the underscore _ as a delimiter.
FOR /f "tokens=1,* delims=_" %%a IN ('dir /b /a-d "*_*_*_*_*_*_*_*.avi"') DO (
    REM Name the folder based on what is before the first token.
    MD %%a
    REM To get the full filename, concatenate the tokens back together.
    MOVE "%%a_%%b" .\%%a\
)
POPD
GOTO :EOF
Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33
  • I am attempting to do the same thing, but with spaces in the files for example: Key Bing Information_2019-01-01.csv. Any idea how I can modify this script? – Yogwhatup Feb 07 '19 at 17:44