Solved

need help writing a vbscript to move files and rename them

Posted on 2008-10-02
8
953 Views
Last Modified: 2012-06-27
Hello Experts -
I need help writing a vbscript to move and rename files from 2 network locations.  Basically, on one network drive, I have various project numbers and within each project number, there is a set of the same directories.

 i.e.
h:\0000\documents
h:\0000\codes
h:\0000\images

h:\0001\documents
h:\0001\codes
h:\0001\images

etc. for about 100 different project numbers, the directories within the project numbers stays the same

I need to move all files in  h:\project number\images  to another location on the network - i:\projectdata\files\

Then, I want to rename the moved files on i:\projectdata\files\  with the year_month_projectnumber_nextnumberofimages.jpg
with the year and month being the date it was created.

So a file from h:\0000\images\test.tif   will be moved to i:\projectdata\files\2008_10_0000_01.jpg and another file moved from h:\0001\images\file1.jpg  will be moved to i:\projectdata\files\2008_10_0001_01.jpg

If it can be done using DOS commands, I am open to that also.

The environment is all Windows XP .

Thanks for your assistance. Any help will be greatly appreciated.


0
Comment
Question by:polshek
8 Comments
 
LVL 20

Expert Comment

by:ltlbearand3
ID: 22630689
I don't know that I totally follow what you need.  I have attached some vb script code as a starting point for you.

This code will loop through all files in a directory and copy them to a new location with a new name.  The new name follows this format:

YYYY_MM_CCCC_01

Where YYYY is four digit year
MM is two digit month
CCCC is a four digit counter

Just copy this code and paste it in notepad.  Save with a .vbs extension and run.
- Bear

Dim strStartDir, strEndDir, dtYear, dtMonth, intCnt, strCnt

Dim objFSO, objFolder, objFiles, objFile, strNewName

Set objFSO = CreateObject("Scripting.FileSystemObject")
 

' *****

' Set you Defaults Here

' *****

strStartDir = "H:\"	' Directory where the files currently Exist

strEndDir = "I:\"		' Directory where you want to copy the Files

dtYear = Year(now())	' Grab the Current Year

dtMonth = Month(now())	' Grab the Current Month

intCnt = 0				' Set File Counter
 
 

Set objFolder = objFSO.GetFolder(strStartDir)	' Get Data for the Folder

Set objFiles = objFolder.Files	' Grab List of Files in Folder

For each objFile in objFiles	' Loop through all files in Folder

	intCnt = intCnt + 1	' Increment Counter

	

	' Pad Counter with zeros for File Rename

	select Case Len(intCnt)

		Case 1

			strCnt = "000" & Cstr(intCnt)

		Case 2

			strCnt = "00" & Cstr(intCnt)

		Case 3

			strCnt = "00" & Cstr(intCnt)

		Case 4

			strCnt = "0" & Cstr(intCnt)

		Case Else

			strCnt = Cstr(intCnt)

	End Select

	

	' Set Name for new file

	strNewName =  strEndDir & "\" & dtYear & "_" & dtMonth & "_" & strCnt & "_01." & objFSO.GetExtensionName(objFile)

	

	' Copy from old location to new location with New name

	objFSO.CopyFile objFile.Path, strNewName, true

	

Next
 

' Uncomment if you want to see a DONE message box

' wscript.echo "Done"

Open in new window

0
 
LVL 9

Expert Comment

by:dlb6597
ID: 22631003
Here is a possible DOS batch solution. First, in your example you are renaming test.tif to 2008_10_0000_01.jpg I am assuming that is a typo and you meant for test.gif to be the source?

I believe the attached batch DOS file will do what you want...copy all the files from the 100 or so h:\project#\images to a single i:\projectdata\files folder with the filename being year_month_project#_n where n increments for each document in each images folder. This batch won't pad n with a leading zero as your example shows...your above example would end up being 2008_10_0000_1.jpg

If that is what you want, save it to the root of H: where your source folders are. This assumes that I:\projectdata\files already exists.  and TEST before removing the echo from in front of the copy command.

if there are less than 10 files in any images folder and you need the leading zero you can change %count% to 0%count% in the copy command. If there are more than 10 and you need the leading 0, this simple code won't work as is...


for /D %%i in (*.) do call :dirsub %%i

goto :EOF
 

:dirsub

set count=1

for %%j in (%1\images\*.*) do call :dirsub2 %1 %%j

goto :EOF
 

:dirsub2

echo copy %2 i:\projectdata\files\%date:~-4,4%_%date:~-10,2%_%1_%count%%~x2

set /a count=count+1

goto :EOF

Open in new window

0
 
LVL 5

Expert Comment

by:lodar123
ID: 22633023
For the date.. you want the date the file was created or copied?
0
 
LVL 5

Expert Comment

by:lodar123
ID: 22633201
Ok regardless of the date.. we can change it after.  This code works and was tested as well using same structure you were talking about. It will name the file with the "Current" date.  I can change it for the actual file creation date if its what you needed.
 
Cheers!

Dim fso

Set fso = CreateObject("scripting.filesystemobject")
 

cnt=0

xRoot="h:\"

xSource="\images"

xDestination="i:\projectdata\files\"

xYear = Year(Now())

xMonth = Month(Now())
 

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colSubfolders = objWMIService.ExecQuery ("Associators of {Win32_Directory.Name='" & xRoot & "'} Where AssocClass = Win32_Subdirectory ResultRole = PartComponent")
 

For Each objFolder in colSubfolders

    xFolder = objFolder.Name & xSource

    If fso.FolderExists(xfolder)=True Then

    	WScript.Echo xFolder & " Found!"

    

		Set FileList = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_Directory.Name='" & xFolder & "'} Where ResultClass = CIM_DataFile")
 

			For Each objFile In FileList

    			cnt=cnt+1

    			If cnt<=9 Then

    				cntvalue="0" & cnt

    			Else

    				cntvalue=cnt

    			End if

    			WScript.Echo objfile.name

    			DestinationFile = xdestination &  xyear & "_" & xmonth & "_" & Mid(objFolder.Name,4,4) & "_" & cntValue &  Mid(objfile.name,Len(objfile.name)-3,4)

    			Wscript.echo "Copying " & objfile.name & " to " & DestinationFile

    			FSO.CopyFile objfile.name, DestinationFile, true
 

			Next

		

	End If

    

    WScript.Echo "Done"

    

Next

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:polshek
ID: 22637237
Thanks lodar123. The code works great. Just one thing, I want to change  the count to be three digits instead of two. So instead of 01 to 99, it should be 001 to 099 to 196.  I attempted to fix it myself but of course, it didn't work. I've attached my poor attempt at coding it.  Can you help fix it so it works ? Thanks so much...
			For Each objFile In FileList

    			cnt=cnt+1

    			If cnt<=9 Then

    				cntvalue="00" & cnt

			Else 	If cnt>=99 Then

					cntvalue="0" & cnt

				End if

			Else

    				cntvalue=cnt

    			End if

Open in new window

0
 
LVL 5

Accepted Solution

by:
lodar123 earned 500 total points
ID: 22637272
that should fix it
For Each objFile In FileList

	cnt=cnt+1

    If cnt<=9 Then

    	cntvalue="00" & cnt

	ElseIf cnt <=99 And cnt > 9 Then

		cntvalue="0" & cnt

	Else

    	cntvalue=cnt

    End if

Next

Open in new window

0
 
LVL 5

Expert Comment

by:lodar123
ID: 22637288
oh yeah dont put the next there lol :)  I just added it to close the for loop ;p... but in your code it goes further down
0
 

Author Comment

by:polshek
ID: 22642381
thanks lodar123 for all your help. You are a real genius. Thanks again.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now