Solved

How can I handle double perecent in file name when using VB File.Copy method

Posted on 2016-09-05
14
67 Views
Last Modified: 2016-11-28
I am copying a series if files using the system.io.file.copy method. The source filenames were generated by a third party package and some files contain a double percent sign in the name e.g.5w%%s02!.doc. When the code encounters a file like this it throws an error "Could not find a part of the path".

Is there any way I can "escape" the double percent to allow the copy to continue?
0
Comment
Question by:petesims
  • 7
  • 5
14 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785608
how's your codes look like?
0
 

Author Comment

by:petesims
ID: 41785636
dim strSource as string = "\\serverpath\5w%%s02!.doc
dim strNewFile as string

if file.exists(strSource) then
    strNewFile  = "NewName" & Path.GetExtension(strInput)
    strDestFile = Path.Combine("\\DestPath\", strNewFile)
    File.Copy(strSource, strDestFile, True)
else
    MsgBox(strSource & " not found")
end if

The File.Exists finds the file OK but the File.Copy fails with the  "Could not find a part of the path" error.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785659
is strDestFile a valid path?

what if you doing a break point for line:

File.Copy(strSource, strDestFile, True)

and see what's the value of strDestFile ?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:petesims
ID: 41785698
Hi Ryan,

Thanks for jumping in to help me with this problem. The issue is not the code, I can successfully copy files. The issue is related to the two % characters in the file name. I suspect it is caused by the File.Copy method using an old fashioned DOS copy command to do the copy. In DOS the % character is used to identify a variable (for example %varname) when running a DOS command. I know that to "escape" the % in DOS you used two %'s but using this does not work for this scenario. My question is "how do modify the file name to accept the %'s as a valid part of the filename.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785715
>>how do modify the file name to accept the %'s as a valid part of the filename
What I'm thinking is... if it's a valid Windows files, then the .NET codes should able to read it.

also make sure your Visual Studio got modify permission to path as what was returned from strDestFile  ?
0
 

Author Comment

by:petesims
ID: 41785733
The problem is more complex than that. The VB File.exists function can find the file. As I said before the problem is in interpreting the %% in the file name when copying. This has always been a problem in the DOS Copy and Xcopy commands. I suspect that Microsoft has used similar code for the VB File?Copy function. I,ve been fighting this issue for the past 30 years and was hoping someone had a solution..
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785750
can you provide an example how the output of strDestFile looks like?

>>File.Copy(strSource, strDestFile, True)
quick try... what if you delete the existing file before copy over the new file. will that work?
0
 

Author Comment

by:petesims
ID: 41785808
Hi Ryan,

You are not understanding the problem. Thanks for trying to help but you are not anywhere near my problem. I'll try another topic.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785810
i just try to diagnose the issue to get your problem resolved. unfortunately that seems not the approach you prefer. good luck!
0
 

Author Comment

by:petesims
ID: 41785862
Hi Ryan, I am happy with what you tried to do but it was not the right approach to my problem. When you have been around I.T. as long as I have i hope you too will understand that computing is not an exact science, it is more like art. Keep up your good work and always keep an open mind when trying to diagnose problems. I clocked up 50 years in IT in May and I'm still learning. 🙂
0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 41787406
The problem is: There are - for evolutionary reasons - different libraries (Win32 API) handling files having different limitations.

When you run into a problem with too long paths or special chars, then you need to explicitly use one of the Win32 API methods.

Start with the CopyFile methods from kernel32: CopyFile and CopyFileEx.

When these don't work, then use the Shell file methods: SHFileOperation.
0
 

Author Comment

by:petesims
ID: 41787504
Shell file methods win again. Thanks ste5an
0
 

Author Closing Comment

by:petesims
ID: 41795131
Shell file methods win again. Thanks ste5an
0

Featured Post

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.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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