Solved

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

Posted on 2016-09-05
14
58 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 49

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 49

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
 

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 49

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 49

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 49

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now