Rename File to Folder Name then move the file to a different folder

Hi experts,

I need a batch or VB script to rename a file to its folder name, on completion, to then move the file to a different folder.

Example:
Folder/ folder name: One
File name in folder 'One': dkfjsdkf (disregard extension) - only the name is updated from 'dkfjsdkf' to 'One'

The folder path is C:\Test

On completion, the file renamed from 'dkfjsdkf' to 'One' is moved to: C:\Completed

I've been working on a script after digging around, however couldn't get it to work...
strStartFolder = "C:\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objSubFolder In objFSO.GetFolder(strStartFolder).SubFolders
      RecurseFolders objSubFolder
Next

MsgBox "Done"

Sub RecurseFolders(objFolder)
      For Each objFile In objFolder.Files
            ' Go from the end of the base file name to the start to find how many numbers there are
            intNumberStart = 0
            For intPos = Len(objFSO.GetBaseName(objFile.Path)) To 1 Step -1
                  If IsNumeric(Mid(objFSO.GetBaseName(objFile.Path), intPos, 1)) Then intNumberStart = intPos
            Next
            If intNumberStart > 0 Then
                  objFile.Name = objFSO.GetFolder(objFSO.GetParentFolderName(objFile.Path)).Name & "" & Right("" & Mid(objFSO.GetBaseName(objFile.Path), intNumberStart), 3) & "." & objFSO.GetExtensionName(objFile.Path)
            Else
                  MsgBox objFile.Path & " does not have any number sequence."
            End If
      Next

      For Each objSubFolder In objFolder.SubFolders
            RecurseFolders objSubFolder
      Next
End Sub

Open in new window


Thanks in advance.
OsleyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Steve KnightIT ConsultancyCommented:
Is this just one level of dirs under c:\test or all subdirs and their subdirs etc?

For top level could use something like this in batch.  It runs down top level dirs under c:\test and then renames each file in those subdirs to the name of that dir, then moves it to c:\completed.  It shows you the commands at the moment.  Remove the two capital ECHO to make it do the work and can remove the pause line altogether to stop it pausing between each one.

Steve

@echo off
Set StartDir=c:\test
Set DestDir=c:\completed

PUSHD "%StartDir%"

for /f "tokens=*" %%d in ('dir /b /ad "%startdir%"') do (
  echo * Working on dir: %%~d
  for /f "Tokens=*" %%f in ('dir /b /a-d "%startdir%\%%~d"') do (
    ECHO rename "%%~d\%%~f" "%%~d%%~xf"
    ECHO move "%%~d\%%~d%%~xf" "%DestDir%"
    pause
  )
)

POPD

Open in new window

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Why not doing rename and move in one go? Lines 10 and 11 of Steve's script would then look like this:
    ECHO move /y "%%~d\%%~f" "%DestDir%"

Open in new window

and please remove line 12, there is no need to pause after each file found, we do not expect them to be that much.
The change above will also force the move, and overwrite an existing file in the destination folder. Steve's script will NOT overwrite anything, which might be intentional or not.
OsleyAuthor Commented:
Hi Steve,

Thanks for this. It's only one level - as near as I can tell.

I ran the script and it's perfect. You're awesome. I can't tell you how long I've been fumbling around with this.

Thanks.
Steve KnightIT ConsultancyCommented:
No problem. I wasn't sure if there was a need to do some process after the rename and before moving to completed which is why i split the two up but qlemo is right of course.

Steve
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
Microsoft DOS

From novice to tech pro — start learning today.