0

Similiar problem as previously solved question here - Batch create folders based on part of file name and move files into that folder

QUESTION: I have 15k files that I want to extract the first word from file name to create a folder, then move all those respective files into the folder.

This is driving me crazy trying to figure out!!!

**EXAMPLE: **Using the files below, I want all files beginning with PTC to move into a new folder called PTC, then the same process with files beginning with PRIORITY.

  • PTC-Affiliate Digital Asset Refresh (160x600)
  • PTC-Affiliate Digital Asset Refresh (160x600)2
  • PTC-Affiliate Digital Asset Refresh (160x600)3
  • PRIORITY-Affiliate Digital Asset Refresh (160x600)
  • PRIORITY-Affiliate Digital Asset Refresh (160x600)2
  • PRIORITY-Affiliate Digital Asset Refresh (160x600)3

I tried revising existing code (below), but am running into 2 issues:

  1. File names use spaces, not underscore - how do I denote that in the code?
  2. When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.

CODE I AM TRYING TO EDIT TO DO WHAT I WANT:

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\[directory\]"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=-" %%a IN ('dir /b /a-d "*\_*_\*_*\_*"') DO (
   MD %%a 2\>nul
   MOVE "%%a\_%%b\_%%c\_%%d\_%%e" .%%a\\ 2\>nul
)
POPD
GOTO :EOF

Any help or suggestions would be VERY welcomed.

Thank you!

enter image description here

I tried revising existing code (below), but am running into 2 issues:

  1. File names use spaces, not underscore - how do I denote that in the code?
  2. When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.

I am not able to solve for either of these issues.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
AR6140
  • 1

1 Answers1

0

Maybe your approach of focusing on old code is not allowing you to focus on the task at hand :)

You really only care about the first word, delimited by -. So split only that, then move all files starting with the first word once off:

@echo off
SET "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "delims=-" %%i in ('dir /b /a-d "*-*"') do (
    mkdir "%%~i">nul 2>&1
    move %%i* "%%~i">nul 2>&1
)
popd

however, there is a chance (though you did not specify this) that there are other files that you might want to skip in the directory, then might do:

@echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for %%a in ("*-*(*x*)*") do for /f "delims=-" %%i in ('dir /b /a-d "%%~a"') do mkdir "%%~i">nul 2>&1 && move "%%a" "%%~i"
popd

and if you feel like some regex and be more specific with your query, then findstr will help by enforcing wildcards, spaces and/or alphabetical or numeric character ranges as in the extract from below. findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$" :

@echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "tokens=1,*delims=-" %%a in ('dir /b /a-d ^| findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$"') do (
    mkdir "%%~a">nul 2>&1
    move "%%a-%%b" "%%~a"
)
popd
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 1
    I cannot thank you enough for this help... I have NO idea what I'm doing here, just trying to figure it out from similar posts. I really, really appreciate you helping me with this. – AR6140 Mar 25 '22 at 12:51
  • How would I modify the script to move the files into a version of the original folder? For example, instead of moving the files into the "PRIORITY" folder, it would move them to a new folder I created called "PRIORITY - NEW" – AR6140 Mar 28 '22 at 18:13