Solved

VB Script help

Posted on 2011-02-14
15
87 Views
Last Modified: 2014-06-27
Hi, i have the following script which isn't working. Can someone please help me in finding the problem...

 My Script
The script works for the first and last line (Which ive highlighted in red), but misses everything inbetween.
Can someone please tell me what i have done wrong?
Thanks
0
Comment
Question by:Chrisneilhopkins
  • 7
  • 6
15 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34887104

Given that you're overwriting arrFileNames each time, what are you actually expecting to happen?

Chris
0
 

Author Comment

by:Chrisneilhopkins
ID: 34887125
Hi Chris,

I am not very good at VB Scipt at all. I am trying to create folders for all the names entered on all lines. Do i need
arrFileNames = Array on each line or not? at the moment the script creates folders for the names in the first and last line, but i want it to create folders for all of the lines.

Thanks
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34887137

Hmmm well, it should only do anything at all for the last line. But I'm making assumptions, can you post your script?

Chris
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:Chrisneilhopkins
ID: 34887159
Here is the script,
Apologies, it doesn't seem to be pasting correctly so i have also attached it as a txt file...
' This is the root folder where the individual customer folders are,
' and is also the same folder where the folders will be created
' for each of the file names
strRootFolder = "C:\Dump\Qlikview\Retail\Source"
' This is the list of files in each folder that will be moved to the
' new folders in the root folder above
arrFileNames = Array("ProductSale.csv", "POSTransactionHeader.csv", "POSTransactionDetail.csv", "AgeCheckSale.csv", "CashierDiscount.csv") 
arrFileNames = Array("CashierPayment.csv", "CashierSale.csv", "CashierTender.csv", "DealCheckHead.csv", "DealCheckLine.csv", "DealHeader.csv")
arrFileNames = Array("DealProductLine.csv", "DealReportItem.csv", "DealReportList.csv", "DealReportMain.csv", "DealReportTransaction.csv")
arrFileNames = Array("DealSale.csv", "DepartmentSale.csv", "EFTCardSale.csv", "GoodsReceivedNotesDetail.csv", "GoodsReceivedNotesHeader.csv") 
arrFileNames = Array("OrderHeader.csv", "POSPaymentTransaction.csv", "POSTransactionVAT.csv", "PriceReductionLine.csv", "StoreCommodityGroup.csv") 
arrFileNames = Array("StoreDepartment.csv", "StoreEmployee.csv", "StoreFinancialStatistic.csv", "StoreIncidentDetail.csv", "StoreProduct.csv")
arrFileNames = Array("HHUGapCheck.csv", "HourlySale.csv", "OrderDetail.csv", "StoreProductIngredients.csv", "StoreProductStock.csv", "StoreReasonCode.csv")
arrFileNames = Array("StoreStockMovement.csv", "StoreStockOut.csv", "StoreStockTransfer.csv", "StoreSupplier.csv", "StoreSystemDetail.csv")
arrFileNames = Array("StoreVATRate.csv", "TillPayment.csv", "TillTender.csv", "VATSale.csv")

' First check if the folders for each file name exist to copy them into
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Right(strRootFolder, 1) = "\" Then strRootFolder = Left(strRootFolder, Len(strRootFolder) - 1)
For Each strFileName In arrFileNames
	If objFSO.FolderExists(strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1)) = False Then
		objFSO.CreateFolder strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1)
	End If
Next

' Now go through the customer folders
For Each objSubFolder In objFSO.GetFolder(strRootFolder).SubFolders
	For Each strFileName In arrFileNames
		strNewName = strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1) & "\" & objSubFolder.Name & strFileName
		If objFSO.FileExists(objSubFolder.Path & "\" & strFileName) = True Then
			If objFSO.FileExists(strNewName) = True Then objFSO.DeleteFile strNewName, True
			objFSO.MoveFile objSubFolder.Path & "\" & strFileName, strNewName
		End If
	Next
Next

MsgBox "Done" 

Open in new window

test1.txt
0
 

Author Comment

by:Chrisneilhopkins
ID: 34887164
Hmm, seems to have pasted ok actually. :)
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34887198
I've dumped it into a code block for you, the CODE button on the formatting bit is very useful :)

So, I would only expect the script to do anything for the last set of file names, that's what you have left in arrFileNames at the end of the first block. Do you want arrFileNames to include every single one of the listed names?

If so, you want this:
arrFileNames = Array("ProductSale.csv", "POSTransactionHeader.csv", "POSTransactionDetail.csv", _
  "AgeCheckSale.csv", "CashierDiscount.csv", "CashierPayment.csv", "CashierSale.csv", "CashierTender.csv", _
  "DealCheckHead.csv", "DealCheckLine.csv", "DealHeader.csv", "DealProductLine.csv", "DealReportItem.csv", _
  "DealReportList.csv", "DealReportMain.csv", "DealReportTransaction.csv", "DealSale.csv", "DepartmentSale.csv", _
  "EFTCardSale.csv", "GoodsReceivedNotesDetail.csv", "GoodsReceivedNotesHeader.csv", "OrderHeader.csv", _
  "POSPaymentTransaction.csv", "POSTransactionVAT.csv", "PriceReductionLine.csv", "StoreCommodityGroup.csv", _
  "StoreDepartment.csv", "StoreEmployee.csv", "StoreFinancialStatistic.csv", "StoreIncidentDetail.csv", "StoreProduct.csv", _
  "HHUGapCheck.csv", "HourlySale.csv", "OrderDetail.csv", "StoreProductIngredients.csv", "StoreProductStock.csv", _
  "StoreReasonCode.csv", "StoreStockMovement.csv", "StoreStockOut.csv", "StoreStockTransfer.csv", "StoreSupplier.csv", _
  "StoreSystemDetail.csv", "StoreVATRate.csv", "TillPayment.csv", "TillTender.csv", "VATSale.csv")

Open in new window

That way, all of the file names are stored in a single array, rather than it overwriting each time.

All _ does it let us carry the command on over multiple lines. There so my eyes don't hurt :)

Chris
0
 

Author Comment

by:Chrisneilhopkins
ID: 34887209
Ahhh,  I tried to get it working on multiple lines to start with but it kept throwing back errors. I didn't realise a _ had to be put at the end of each line.
Thats great, thanks very much for your help...

:)
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34887238

No problem, yell if it doesn't play :)

Chris
0
 

Author Comment

by:Chrisneilhopkins
ID: 34888212
Hi Chris,
Just another question...

the script i attached earlier also moves files from one dir to another. At the moment it overwrites any existing files..
How would i get it to append instead of overwriting?

Thanks
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34888524

Not so easy, it depends on the content of the files. Do we have to drop a header row if it's appending?

Chris
0
 

Author Comment

by:Chrisneilhopkins
ID: 34888829
Chris,
No, none of the files have headings. The heading row is added by the software we are using to generate the reports we require.
So all it has to do is append to the existing file (if present)
Thanks
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34888957

In theory, like this. Please make sure you test this first, I haven't.

Chris
' FSO Constants

Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8

' This is the root folder where the individual customer folders are,
' and is also the same folder where the folders will be created
' for each of the file names

strRootFolder = "C:\Dump\Qlikview\Retail\Source"

' This is the list of files in each folder that will be moved to the
' new folders in the root folder above

arrFileNames = Array("ProductSale.csv", "POSTransactionHeader.csv", "POSTransactionDetail.csv", _
  "AgeCheckSale.csv", "CashierDiscount.csv", "CashierPayment.csv", "CashierSale.csv", "CashierTender.csv", _
  "DealCheckHead.csv", "DealCheckLine.csv", "DealHeader.csv", "DealProductLine.csv", "DealReportItem.csv", _
  "DealReportList.csv", "DealReportMain.csv", "DealReportTransaction.csv", "DealSale.csv", "DepartmentSale.csv", _
  "EFTCardSale.csv", "GoodsReceivedNotesDetail.csv", "GoodsReceivedNotesHeader.csv", "OrderHeader.csv", _
  "POSPaymentTransaction.csv", "POSTransactionVAT.csv", "PriceReductionLine.csv", "StoreCommodityGroup.csv", _
  "StoreDepartment.csv", "StoreEmployee.csv", "StoreFinancialStatistic.csv", "StoreIncidentDetail.csv", "StoreProduct.csv", _
  "HHUGapCheck.csv", "HourlySale.csv", "OrderDetail.csv", "StoreProductIngredients.csv", "StoreProductStock.csv", _
  "StoreReasonCode.csv", "StoreStockMovement.csv", "StoreStockOut.csv", "StoreStockTransfer.csv", "StoreSupplier.csv", _
  "StoreSystemDetail.csv", "StoreVATRate.csv", "TillPayment.csv", "TillTender.csv", "VATSale.csv")

' First check if the folders for each file name exist to copy them into
Set objFSO = CreateObject("Scripting.FileSystemObject")

If Right(strRootFolder, 1) = "\" Then strRootFolder = Left(strRootFolder, Len(strRootFolder) - 1)

For Each strFileName In arrFileNames
  If objFSO.FolderExists(strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1)) = False Then
    objFSO.CreateFolder strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1)
  End If
Next

' Now go through the customer folders
For Each objSubFolder In objFSO.GetFolder(strRootFolder).SubFolders

  For Each strFileName In arrFileNames

    strNewName = strRootFolder & "\" & Left(strFileName, InStr(strFileName, ".") - 1) & "\" & objSubFolder.Name & strFileName

    If objFSO.FileExists(objSubFolder.Path & "\" & strFileName) = True Then

      If objFSO.FileExists(strNewName) Then 

        ' Needs testing. Merge contents of source file with the existing strNewName file

        Set objSourceFile = objFSO.OpenTextFile(objSubFolder.Path & "\" & strFileName, FOR_READING)
        Set objDestinationFile = objFSO.OpenTextFile(strNewName, FOR_APPENDING)

        Do While objSourceFile.AtEndOfStream <> True
          objDestinationFile.WriteLine objSourceFile.ReadLine
        Loop

        Set objSourceFile = Nothing
        Set objDestinationFile = Nothing

        ' Delete the source file (would have been moved otherwise)
        objFSO.DeleteFile objSubFolder.Path & "\" & strFileName

      Else

        objFSO.MoveFile objSubFolder.Path & "\" & strFileName, strNewName

      End If
    End If

  Next

Next

MsgBox "Done"

Open in new window

0
 

Author Comment

by:Chrisneilhopkins
ID: 34914824
Thanks that worked great :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro to create Bar charts 4 84
Need to rewrite code for checking if a file exists 3 66
change the windows script file to BAT 10 41
Excel 2016 loop through 6 36
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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