Solved

VBscript FSO.MoveFolder failing when not hard coded

Posted on 2006-10-24
4
1,032 Views
Last Modified: 2008-03-06
Hi There,

I'm trying to tidy up some folder names with a vbscript.

e.g. rename folder 'msgid_123456_Filename_date_version' to 'Filename date version'

I've done all the tidying and have two variables with the before and after folder names in.

If I run the code:

objFSO.MoveFolder "c:\msgid_123456_Filename_date_version" , "C:\Filename date version"

It works fine and renames the folder no problem.

If I try to use variables it seems to fail: I've tried several ways

==========================
strBefore = "'" & strBefore & "'"
strAfter = "'" & strAfter & "'"

objFSO.MoveFolder strBefore,strAfter
==========================

==========================
strBefore = chr(34) & strBefore & chr(34)
strAfter = chr(34) & strAfter & chr(34)

objFSO.MoveFolder strBefore,strAfter
==========================

As soon as I try to use variables with the movefolder command I get "Bad filename or number" error message on the movefolder line

Also if echo strBefore and strAfter on the screen they are exactly as I want them and definately point the correct folders with no spelling mistakes.

Does anyone have any ideas??



0
Comment
Question by:texaspete
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17794461
you don't need the quotes in the filename variables.

strBefore = "c:\msgid_123456_Filename_date_version"
strAfter = "C:\Filename date version"

objFSO.MoveFolder strBefore,strAfter
0
 

Author Comment

by:texaspete
ID: 17794505
I'm not sure what you mean. in your reply you have quotes in

Besides, I'm not hard coding strBefore and strAfter. They are variables and are part of a loop so they will both be different each loop. If I leave the quotes out and one of the folder names has a space in it I get a "path not found" error on the MoveFolder line.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17794958
yes, I have quotes for the string when assigning the string to the variable.
but those double quotes are only to delimit the string, which is a syntax issue, but is not part of the value.
so, when you call objFSO.MoveFolder (strBefore, strAfter), then you have 2 variables of type string, but not 2 strings constants...

the assignment was only an example of how the value could be given to the variable, let me guess that you read the folder names from some data source/file etc.

so, my suggestion stays:
do NOT add quotes to the string in the variable, and use the variables as like in this pseudo-code

'loop on some condition
while <somecondition>
  'assign the 2 variables from somewhere...
  strBefore = <some assignment>
  strAfter = <some assignment>

  'rnu the MoveFolder command
  objFSO.MoveFolder strBefore,strAfter

wend

0
 

Author Comment

by:texaspete
ID: 17892885
fixed it!

sorry for the delay i've been on leave. It was erroring at a different line. When the script loops and checks the subfolder the rename had already happened and the folder loop variable hadn't been updated with the new name of the renamed folder.

saying that I probably would have still been messing around with quotes by now so have the points

TP
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

636 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