Solved

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

Posted on 2016-09-05
14
71 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 65
VB.Net - TypeInitializer Error 25 31
Store results in vb.net 3 24
Help with preventing selection from a combobox 11 34
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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