@ECHO OFF SETLOCAL SET "sourcedir=c:\sourcedir" PUSHD %sourcedir% FOR /f "tokens=1*" %%a IN ( 'dir /b /ad "*_*_*-*-* *.*"' ) DO ( ECHO MD %%a ECHO MOVE "%%a %%b" .\%%a\ ) POPD GOTO :EOF
This should complete the required task - or at least show the necessary instructions.
If you are satisfied with the issued commands, install sourcedir in the required root directory and delete the two echo keywords to activate.
The message "already exists" generated by MD when trying to recreate an existing directory can be canceled by adding 2>nul to the MD line.
Similarly, a single file move report can be canceled by adding >nul to the MOVE line.
2>nul suppresses error messages (attempting to create an existing directory is an error), while the message "files moved" is a normal output message, hence the difference.
Addition - how it works.
First, PUSHD sets the current target directory.
The output of the DIR command is indicated by the FOR/F symbol. The tokens=1* sentence indicates that the first token (1) is assigned to the nominated meta-variable ( %%a ) and implicitly the second token (*) - %%b is just the next alphabet. The current * means everything after those token numbers explicitly mentioned . There is no delims clause, so default separators are used (set SEPARATORS, SPACE ,; TAB .
DIR targets the mask *_*_*-* *.* , So only files matching this mask will be found - where * means any number of any characters -. Since the mask is "quoted" , spaces are included in the mask. Without quotes, two separate masks will be specified. The /b option creates a list in basic form, that is, only names, without headings or summaries. The /ad switch suppresses any directory names that can be set on the mask.
Therefore, for LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf DIR lists LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf and FOR/F tokenizes as LAST_FIRST_7-24-1936 to %%a and Diagnostic - Topography 11-18-10_1.pdf to %%b , using SPACE as the delimiter.
You can then restore the file name by reinserting a space between %%a and %%b . Any file name containing a delimiter must be specified to group characters and signals that they are not separated elements. The move target completes with \ to indicate "This is the directory name."
POPD restores the original registered directory.