Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename file from folder name and move file 1

Status
Not open for further replies.

mdmatt

Technical User
Dec 17, 2007
19
US
Hi all,
I have files in a poor folder structure that I would like to rename and restructure. There are about 20,000 files and the files are in 5 main sub folders that have folders and folders...there is consistency in the folder structure, so I think that I should be able to do this, I just do not know how to.

I would like to have all the files renamed to the name of the folder that they are in, then moved into the appropriate main sub folder.
The structure is as follows:

Main sub folder\date folder\name folder\generic_file_name.pdf

ex. PLANS\20061105182142\12345\page0.pdf

I would like it to be:
Main folder\[name folder].pdf

ex. PLANS\12345.pdf

All of my files need to have the name of the folder that they currently reside in, then "jump" up 2 folders, possibly deleting the date and name folders upon completion. I am using Windows XP pro / Office 2003.

Does anyone know how I can perform this action?
Thank you for any help you can provide.

Matt



 
I have done similar file management tasks using VB scripting. It is not too difficult if you have a basic understanding of VB. If you haven't already done so, you might have better luck posting this somewhere else like the Visual Basic or VBA forum.

The only issue I see with what you asking to do is that all of the files can't have the same name. Unless they are each in a separate folder, you will have to add a suffix to each file name, FolderName001.pdf, FolderName002.pdf, etc., using a suffix that can accomodate 20,000 files or however many is in the largest directory.

Let me know if you want to know more.
 
Thanks, I will try to post in the VB forum as well. every file will have a unique name.
 
Actually there is a Programmers VBscript forum too.
 
Matt,

Here is a sample script which copies files from one folder to another and then renames the copied files using the folder name, NewFolder_1, NewFolder_2, etc.

*************************************
'VB Script to copy .pdf files from NewFolderA to NewFolderB
'and the rename them using the folder name.

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'This code uses the FileSystemObject, (FSO) , GetFolder and Copy methods
'The files in the folder are treated as a collection. Each file is copied and then renamed.

'1. Save a copy of the file, overwriting any existing files with the same name.
Set colPDF_Files = objFSO.GetFolder("C:\NewFolderA").Files
For Each objFile In colPDF_Files
objFile.Copy "C:\NewFolderB\", True
Next

'2. Rename the files using the .name property
Set colPDF_Files = objFSO.GetFolder("C:\NewFolderB").Files
intSuffix=1
For Each objFile In colPDF_Files
strNewFilename = "NewFolderB_" &intSuffix
ObjFile.name= strNewFilename&".pdf"

Wscript.Echo "Filename is:"&strNewFilename
intSuffix=intSuffix+1
Next


'Clean up and quit.
Wscript.Echo "Finished."
Set objFSO = Nothing
Set colPDF_Files=Nothing
Set WshShell = nothing
WScript.Quit
*************************************
If you need more help, start a thread in the VB Scripting forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top