Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-05
14
Medium Priority
?
81 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
[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
  • 7
  • 5
14 Comments
 
LVL 53

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 53

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 53

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 53

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 53

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 35

Accepted Solution

by:
ste5an earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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