• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

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
0
NQMids
Asked:
NQMids
  • 4
  • 3
1 Solution
 
nayernaguibCommented:
Assuming that the PDF file name length is constant, the following script should work:

_________________________________________________________

'First determine CD ROM drive letter
Const CDROM = 4
Dim CDdrive
For Each d in CreateObject("Scripting.FileSystemObject").Drives
  If d.DriveType = CDROM Then
    CDdrive=d.DriveLetter & ":\"
  End If
Next

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

Set pdffolder=objfso.GetFolder("D:\PDFTemp\")
Set pdffiles=pdffolder.Files

'Rename files to WR*.pdf
For Each pdffile In pdffiles
      objFSO.MoveFile "D:\PDFTemp\" & pdffile.Name, "D:\PDFTemp\" & "WR" & Mid(pdffile.Name, 3, Len(pdffile.Name)-2)
Next

Dim num
num=0

'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

'Move renamed Files
objFSO.MoveFile "D:\PDFTemp\*.pdf" , "\\ms-wr-sysnas\Temp\"

'Eject CD drive
Eject CDdrive

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

_________________________________________________________

_______________

  Nayer Naguib
0
 
NQMidsAuthor Commented:
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.
0
 
NQMidsAuthor Commented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nayernaguibCommented:
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
0
 
nayernaguibCommented:
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
0
 
NQMidsAuthor Commented:
Thank you for the Colour page fix Nayer.

Any luck with the read only problem.

Many Thanks
0
 
NQMidsAuthor Commented:
Thank you for help Nayer.

Everything works perfect now.

Many Thanks again
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now