2

I need to create folders and relocate files to the folders based on the first eight characters of the filenames. The files will look like:

GG001652 - 5211_Infoscitex.xls
GG001652 - 5211- as of 7.31.12.pdf
GG001570 - 7575 FSR (3.31.2010).pdf
GG001570 - 7575_IC_6.30.12.xlsx
GG001570 - 7575_SF 425_6.30.12.xls

I'd like the batch to create two folders:

GG001622
GG001570

Each containing the files beginning with those 8 characters. File length and nomenclature is inconsistent, but the first 8 characters are standardized, and file types include .pdf, .doc, .docx, .xls, .xlsx and .msg

Spent a good few hours trying to modify Magoo's code in this post, but couldn't for the life of me get it to work:

Batch create folders based on part of file name and move files into that folder

Have a work deadline I need to meet, so greatly appreciate any help offered.

Tykal
  • 23
  • 3

1 Answers1

0

If the first 8 chars are always followed by space,
use a for /f to split the name with default delimiter space and token 1. See ss64.com/nt/for_f

@Echo off
for /f "delims=" %%A in (
  'DIr /B /A-D "* - *.*" ^| findstr /I "^[PG]G[0-9][0-9][0-9][0-9][0-9][0-9].-..* '
) Do for /f %%B in ("%%A") do (
    MD %%B >NUL 2>&1 
    Move "%%A" %%B
)

The first iterating for is now replaced with a more complex for /f parsing dir output wihich is filtered by a findstr with a RegEx to match the described name structure.
a P or G followed by a G followed by 6 numbers.

  • Awesome, thank you! How do I edit to account for a variable first character (either a P or G)? – Tykal Aug 16 '17 at 01:20