Solved

need help writing a vbscript to move files and rename them

Posted on 2008-10-02
8
956 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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