frazld
frazld

Reputation: 1

How to batch file to create folder from part of file name and move files

Batch file to create folder from part of file name and move files.

I have lots of files that need to be in their own sub folder.

They are in this format: Example

123456-ABC-XYZ
123456-DFG-XYZ
123456-HIJ-XYZ

Where I would like to create a sub folder and move the files to it by characters 8-10 or by 12-14

Used this as a base that I found on this site posted by Magoo and it works to do it by the 1st set of characters (h:\1AAATEST = my test folder

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

[Editing note from Magoo:

This is incorrect. Here's what I posted:

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

reference: Question from nearly a year ago

Notice that there are major difference beyond the change of source directory and omission of the echo keyword as narrated. OP's syntax in this question simply won't work.

]

I modified it with the same results as the original

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

Then again, and it created 4 folders from this file name

LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1

But didn't move any files.

1st folder = -
2nd = 11-18-10_1
3rd = Diagnostic
4th = Topography



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

I came up with the following:

@echo off
for /f "tokens=1-3* delims=-," %%a in ('dir /b /a-d "*-*-*"') do (
(md "%%~nb" 2>nul)
(MOVE "%%a-%%b-%%c" "%%b"))
POPD
GOTO :EOF

It works for making directory and moving files for the 1st and 2nd section of the file name, by changing the md to a & the move target dir to a for 1st part of file name & by changing the md to b & the move target dir to b for 2nd part of file name &

BUT changing the md to c & the move target dir to c for 3rd part of file name & DOESN"T work. It creates the correct dir, and it doesn't move the files but it shortens the filenamnes to XYZ.

Suggestions would be appreciated.

Upvotes: 0

Views: 412

Answers (1)

Magoo
Magoo

Reputation: 80023

I received an angry telephone call one day from someone who'd used a library I published and it had a major fault. In reality, it had been modified and republished by someone with no note as to who, when or why in the library source. That was the last time I ever published sourcecode for my commercial work.

The problem you are encountering is very simply explained.

The for /f examines LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1 and assigns LAST_FIRST_7-24-1936 to %%a and Diagnostic - Topography 11-18-10_1 to %%b

In the code as you have modified it, you are then executing MD %%b which will be interpreted as

MD Diagnostic - Topography 11-18-10_1

so batch creates those four directories, as requested.

Then the move is encountered. The destination directory is .\%%a\ which will attempt to move the file to .\LAST_FIRST_7-24-1936\ - a directory which is unlikely to exist.

So - to fix the problem, try replacing MD %%b with MD %%a as was specified in the original code.

As for your requirement for 123456-ABC-XYZ, then try

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

(this version to move to the directory with the middle block of characters - to move to the end-block, replace %%b with %%c in both the MD command and the destination of the move command)

BTW - it's very likely that the md will complain about attempting to create an existing directory. You can shut it up by appending 2>nul to the end of the md command.

Upvotes: 0

Related Questions