Trailing whitespace in filename

2019-08-12 18:28发布

问题:

I tried using a batch-script from this answer to color some text in the console.

Unfortunately I had some unintended behaviour, using ´3 :s as string for the call, which created a file named: "┬┤3 ", with a trailing whitespace.

Windows (10) (harddrive using NTFS) somehow can't handle a trailing whitespace in filenames, therefore I can't get rid of it.


I tried using delete, rename, move in the Windows Explorer and Total Commander - all failed.

Using the command prompt: DEL, MOVE and others results in a "cannot find"

Using ATTRIB outputs: "target of symbolic link "┬┤3 " does not exist"


To create such a file in you current directory:

SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
    set "DEL=%%a"
)
call :colorEcho 09 "´3 :s  `"
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

回答1:

Win32 File Namespaces

For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

To create the file in the batch script path:

> "\\?\%~dp0´3 :s " type nul

To remove:

del "\\?\%~dp0´3 "

The :s is a NTFS stream suffix which can be omitted for the task of deleting the file.