-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
3
  • 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 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 Commented Jan 2, 2015 at 20:46
  • @TiagoVeloso - Edit your question and include this code as a starting point for help. Commented Jan 2, 2015 at 20:49
  • I did it now; I didn't notice the comments didn't keep formatting. Commented Jan 2, 2015 at 20:54

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.