Batch File to Create Folders and Move Files

July 19, 2011 at 12:33:23
Specs: Windows 7, Core2Duo/4GB
Good afternoon,

I've been reading through this forum all morning trying to cobble together what I am trying to do and I am just not familiar enough with Windows programming to be able to write this one.

I need the file to cycle through the html files in a directory, chop off all the text before the first underscore (_), create a folder by that name if it does not exist, then move all the files in the directory with that same beginning to that folder.

Format of files:
Generalized: COMPNAME_MMDDYYYY_HHMMSS

As COMPNAME will vary, I need it to be parsed on the occurrence of the first underscore.

So I will start with a list of files like:
COMP3_12345678_123456.html
COMP2_12345678_123456.html
COMP1_12345678_123456.html
COMP1_12345678_456789.html

After the file, I should have:
COMP1 (the folder)
COMP1_12345678_123456.html
COMP1_12345678_456789.html
COMP2
COMP2_12345678_123456.html
COMP3
COMP3_12345678_123456.html

Any help is much appreciated!


See More: Batch File to Create Folders and Move Files

Report •


#1
July 19, 2011 at 15:23:40
Test this, the directory names must be changed to suit your requirements. If you are satisfied with the outcome of testing change Copy to Move.

Good luck.

@echo off

for /f "tokens=*" %%1 in ('dir /b %temp%\*.html') do (
            for /f "tokens=1 delims=_" %%A in ("%%1") do (
                if not exist "%temp%\%%A\" md "%temp%\%%A"
                copy "%temp%\%%1" "%temp%\%%A\">nul
  )
)


Please come back & tell us if your problem is resolved.


Report •
Related Solutions


Ask Question