Script to create folder based on filename and move file into that folder.

I have a folder with 1000's of files named by 2 different numbers.  The first number is the acct number.  The second is a billing number. for example 12557_61106677.pdf.  There will be lots of files that start with 12557_.   I would like to create a script that will scan the folder and create folders based on the account number and then move all the pdf files into their respective folder.
LVL 4
dcolvard2008Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 1 with the location of the folder containing the files.

Running the script (e.g. with cscript scriptname.vbs) will echo the actions to be carried out.  Once you have tested it and are certain it will do what you intend, remove the apostrophes from the beginnings of lines 16 and 20 to create the folders and move the files.


strFolder = "c:\files"
strDelim = "_"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
 
For Each objFile In objFolder.Files
    strName = objFile.Name
    
    If InStr(strName, strDelim) > 0 Then
        arrName = Split(strName, strDelim)
        strNewFolder = strFolder & "\" & arrName(0)
        
        If Not objFSO.FolderExists(strNewFolder) Then
            WScript.Echo "Create folder " & strNewFolder
            'objFSO.CreateFolder strNewFolder
        End If
        
        WScript.Echo "Move " & objFile.Name & " to " & strNewFolder
        'objFile.Move strNewFolder & "\"
    End If
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
Here's a BAT file version that should work.

~bp
@echo off
set BaseDir=c:\temp\EE25751521
for /f "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf"') do (
  for /f "tokens=1-2 delims=_." %%B in ("%%~nA") do (
    if not exist "%%~B" md "%%~B"
    move "%%A" "%%~B">NUL
  )
)

Open in new window

0
dcolvard2008Author Commented:
The VB Script worked perfect and the batch file worked but had to be put in the same directory otherwise it created folders in whatever directory it was run from
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

dcolvard2008Author Commented:
What if we want to do the same thing except we first want to seperate them in folders by year they were created.  Would that be difficult?
0
Bill PrewCommented:
This should correct the need to be in the same folder, and add the year split apart logic.

~bp
@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\temp\EE25751521
for /f "tokens=1-4*" %%A in ('dir /a-d /tc "%BaseDir%\*.pdf" ^| findstr "^[0-9]*/[0-9]*/[0-9]*"') do (
  for /f "tokens=1-3 delims=/" %%F in ("%%A") do set YearCreated=%%H
  for /f "tokens=1-2 delims=_." %%F in ("%%~nE") do (
    if not exist "%BaseDir%\!YearCreated!\%%~F" md "%BaseDir%\!YearCreated!\%%~F"
    move "%%E" "%BaseDir%\!YearCreated!\%%~F">NUL
  )
)

Open in new window

0
dcolvard2008Author Commented:
That works great, but I think I need it to be modified date.  I put all my files in 2010 but most of these where created in previous years.
0
Bill PrewCommented:
Just change the time field on the DIR command to /TW.  You can see the various options for the DIR command via DIR /? at a command prompt.  Here's the section on the /T parm.

/T   Controls which time field displayed or used for sorting timefield
      C  Creation
      A  Last Access
      W  Last Written

~bp
0
dcolvard2008Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.