How to delete in VisualNEO Win a file that has an invalid name?
1. Problem
Windows restricts certain characters in filenames, such as
< (less than)
> (greater than)
: (colon – sometimes works, but is actually NTFS Alternate Data Streams)
” (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
(source: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file)
VisualNeoWin follows Windows. This means that the filecommands in VisualNeoWin do not work on files with filenames that have illegal characters:
FileErase “my>bad<string.txt”
Run “cmd” “/c DEL my>bad<string.txt”
Dim fso
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile ‘my>bad<string.txt’ , TRUE
The file my>bad<string.txt will in all these three cases not be deleted. How to solve this problem? Via pattern matching!
BTW the illegal character problem is rare but not uncommon as Google tells us…
2. Pattern matching via “?”
The DOS wildcard “?” matches only one character (in contrast to “?” in regular expressions, meaning 0 or 1).
my?bad_string.txt matches bv. my|bad_string.txt
my?bad?string.txt matches bv. my|bad:string.txt
etc.
Unfortunately, FileErase “my?bad?string.txt” in VisualNeoWin does not work. But the following commands will delete files with bad names like my|bad:string.txt, my|bad|string.txt, my\bad/string.txt etc.
Run “cmd” “/c DEL my?bad?string.txt”
In VBScript:
Dim fso
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile ‘my?bad?string.txt’ , TRUE
The wildcard “?” replaces illegal characters in the file name, so DEL and DeleteFile can approach the corresponding file. Notice that we do not rename the original file!
3. Practical example
With my mcYoutubeDownloader (https://reiniermaliepaard.nl/yt_downloader/mcYoutube-dl.zip), I downloaded a Bach Cantata (for personal use!). In the current version of mcYoutubeDownloader 1.5 was the result a file with a filename with an illegal character, here the pipe character. Although a bit modified, basically it’s like
Bach – Cantata BWV 48 – Sato | Netherlands Bach Society.webm
In the next version of mcYoutubeDownloader 1.6, the filename of the downloaded file will not contain illegal characters anymore
Based on the previous information, we’ve two strategies within VisualNeoWin to delete this file:
Strategy 1:
SetVar “[file_to_delete]” “Bach – Cantata BWV 48 – Sato | Netherlands Bach Society.webm”
SetVar “[illegal_characters]” “<;>;:;[#34];/;\;[#124];?;*”
StrParse “[illegal_characters]” “;” “[illegal_char]” “[no]”
Loop “1” “[no]” “[i]”
StrReplace “[file_to_delete]” “[illegal_char[i]]” “?” “[file_to_delete]” “”
EndLoop
Run “cmd” “/c DEL /p [#34][file_to_delete][#34]” “Normal” “” “”
I added the switch /p to prompt for input. So you can decide to delete the file or not.
The code can be shorter when you have a VisualNeoWin version that can handle regular expressions:
SetVar “[file_to_delete]” “Bach – Cantata BWV 48 – Sato | Netherlands Bach Society.webm”
StrRegexReplace “[#91]<>:[#34]/\[#124]?*[#93]” “[file_to_delete]” “?” “True” “[file_to_delete]”
Run “cmd” “/c DEL /p [#34][file_to_delete][#34]” “Normal” “” “”
Strategy 2: VBScript
SetVar “[file_to_delete]” “Bach – Cantata BWV 48 – Sato | Netherlands Bach Society.webm”
Call “Functions\VBScript\DeleteFile”
The VBScript function:
name_file_to_delete = publication.nbGetVar( “[file_to_delete]” )
Function RenameFile(FileName)
With New RegExp
.Pattern = “[/\\:*?””<>|#{}%&~]”
.Global = True
RenameFile = .Replace(FileName, “?” )
End With
End Function
Dim fso
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile RenameFile(name_file_to_delete)
4. Short file name
A 8.3 filename (also called a short filename or SFN) could give also a solution. To find a short name, use in VisualNeoWin the command dir /x and save the output into a text file like fsn.txt via the redirection operator >:
Run “cmd” “/c dir /x > fsn.txt” “Normal” “” “”
Process some operation on fsn.txt. However, I found out that dir /x not always generate a short name. So this system-dependent solution is not safe.
5. Pattern matching via “*”
The DOS wildcard “*” matches any sequence of characters or any number of characters. To delete all files with the .part extension, whether they have illegal characters in their filenames or not, you can use the command:
Run “cmd” “/c DEL *.part”
But be cautious with the wildcard “*” and know what you’re doing!
Tip: more on “*” and “?” in my article FileExists has a wonderful alternative!
I hope this helps. Thanks for reading.
Reinier
Comments