Solved

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

Posted on 2016-09-05
14
76 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 52

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 52

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 52

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 52

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 52

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 34

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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