Link to home
Start Free TrialLog in
Avatar of NQMids
NQMidsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Complicted rename & move files

I'm trying to create a VBS script to do the following.

Copy PDF pages from CDROM to a temp location on local machine.
Rename the first two characters from what ever they may be to WR.
List all of the colour pages. (P = Colour M = Mono)
Show how many pages in total in publictaion.
Move pages to a UNC path.
Eject CDRom
Say the Job is complete.

Example file names
BM1AA080506M002.pdf (Mono Page)
BM1AA080506P001.pdf (Colour Page)

The BM tends to very a lot but needs to be renamed to WR.

As you can tell I’m not really a programmer. Here is what I have come up with so far.
All help gratefully received.

Please could you comment any work so I can learn and understand how it all works.

Many thanks in advance

'###########################################
'
'      NAME: PageImport.vbs
'
'      Date: 17/05/06
'
'##########################################

'Copies All PDF's From CD To Temp Loaction
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "E:\*.pdf" , "D:\PDFTemp\" , OverwriteExisting

'Lists All Files In Temp Loaction
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '\\PDFTemp\\'")
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

'Moves renamed Files into CTP System
Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.MoveFile "D:\PDFTemp\*.pdf" , "\\ms-wr-sysnas\Temp\"

'Eject CD ROM Drive
Const CDROM = 4
For Each d in CreateObject("Scripting.FileSystemObject").Drives
  If d.DriveType = CDROM Then
    Eject d.DriveLetter & ":\"
  End If
Next

Sub Eject(CDROM)
  Dim ssfDrives
  ssfDrives = 17
  CreateObject("Shell.Application")_
    .Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub

WScript.Echo "Page Import & Rename Finished."

Wscript.quit
ASKER CERTIFIED SOLUTION
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NQMids

ASKER

Excellent

Thank you very much Nayer it worked a dream.

The file names will allways be the same length.

Many Thanks

Is it possible to list the colour pages in one window instead of individual windows.
Avatar of NQMids

ASKER

Nayer

I have just noticed the files are read only when they end up in the final location.

Is there away to stop this as it is causing problems.

Many Thanks
You are welcome. :-)

As for your question, yes! Replace

______________________________________________

'List color pages and count the total number of pages
For Each pdffile in pdffiles
     If Mid(pdffile.Name, 12, 1)="P" Then
          Wscript.Echo pdffile.Name
     End If
     num=num+1
Next

'Display total number of pages
Wscript.Echo "Number of pages: " & num

______________________________________________

with

______________________________________________

Dim names
names=""

'Determine color pages and the total number of pages
For Each pdffile in pdffiles
     If Mid(pdffile.Name, 12, 1)="P" Then
          names=names & Chr(9) & pdffile.Name & Chr(10) & Chr(13)
     End If
     num=num+1
Next

'Display color pages and total number of pages
Wscript.Echo "Color pages: " & Chr(10) & Chr(13) & names
Wscript.Echo "Number of pages: " & num

______________________________________________

_______________

  Nayer Naguib
Add the following after the line "num=num+1":

     If (pdffile.Attributes And 1)=1 Then
         pdffile.Attributes=pdffile.Attributes - 1
     End If

Or you can add the above lines to the body of the first "For Each" loop.

_______________

  Nayer Naguib
Avatar of NQMids

ASKER

Thank you for the Colour page fix Nayer.

Any luck with the read only problem.

Many Thanks
Avatar of NQMids

ASKER

Thank you for help Nayer.

Everything works perfect now.

Many Thanks again