FIXNames

May 30, 2022
31
0
I’ve got a strange one here. Let’s see if I can adequately describe what I have and what I want to do in just one pass. I have downloaded a number of files, in subdirectories which have strange non-ascii characters at the end of the file name. I want to eliminate those non-ascii characters at the end of the directory names. I have attached a rar file with some examples. Scratch that, rar not allowed, make that a 7zip file.



If you do a “dir” with powershell, and redirect the output into a file (thus getting Unicode), and then examine the output listing using a hex editor, you will see that the directory names end with



20 E2 AD 90 EF B8 8F 0D 0A



Which begins with a space (20) and ends with CR LF (0D 0A). How would I rename those directories, removing the “E2 AD 90 EF B8 8F”?
 

Attachments

  • test1.7z
    503 bytes · Views: 12
May 30, 2022
31
0
For what it's worth, the cygwin "ls -l" command will return this:

drwxrwx---+ 1 Henry None 0 Jun 2 15:48 'Test1 '$'\342\255\220\357\270\217'
drwxrwx---+ 1 Henry None 0 May 8 16:32 'Test2 '$'\342\255\220\357\270\217'
drwxrwx---+ 1 Henry None 0 May 8 16:10 'Test3 '$'\342\255\220\357\270\217'
 
May 20, 2008
11,867
121
Syracuse, NY, USA
I wonder how they were created in he first place. Neither the Win32 function CreateDirectory nor the C functions _mkdir (ascii) or _wmkdir (Unicode) will do it. If I could make one I'd experiment with getting rid of it.

You might be seeing the CRLF that follows the line's output when you use Powershell's "dir" (so the next entry will be on a new line); it would show up in the redirected file. Did you try TCC's RD command with just a quoted " dirname" (beginning with a space)?
 
May 30, 2022
31
0
That's why I attached the 7zip file. Just unzip that somewhere in an empty directory, and you will have example directory names to work with.
 
May 20, 2008
11,867
121
Syracuse, NY, USA
They're not CRLFs. They're Unicode characters (11088 and 65039) which don't translate in my code page and which look peculiar here in the forum. You can rename one with explorer.
After a little experimenting, I renamed one with TCC. Doing it for a lot of them would require a little work.

Code:
c:\users\vefatica\desktop\test1> do x in /a:d * ( echo %@ascii[%x] )
84 101 115 116 50 32 11088 65039
84 101 115 116 51 32 11088 65039
119 97 115 116 101 115 116 49

c:\users\vefatica\desktop\test1> ren "Test2 %@char[11088]%@char[65039]" Test2
C:\Users\vefatica\Desktop\test1\Test2 ⭐️ -> C:\Users\vefatica\Desktop\test1\Test2
     1 dir renamed

c:\users\vefatica\desktop\test1> d
2022-05-08  17:32         <DIR>    Test2
2022-05-08  17:10         <DIR>    Test3 ⭐️
2022-06-02  16:48         <DIR>    wastest1
2022-06-02  18:46           1,244  file.txt
2022-06-02  16:34              33  list.txt
2022-06-02  16:51             522  uni2.txt
2022-06-02  16:35             457  unicode.txt
 
May 30, 2022
31
0
The "%@ascii[]" and "%@char[]" were the parts I hadn't thought of. So can this be expressed in terms that the fixnames plugin can deal with?

Answer: Yes, it can!

fixnames /D /P /Q:%@char[11088]%@char[65039] /R:_ *
"E:\testarea3\Test1 ⭐️" ─► "E:\testarea3\Test1 __" (Y/N/A)? Yes
"E:\testarea3\Test2 ⭐️" ─► "E:\testarea3\Test2 __" (Y/N/A)? Yes
"E:\testarea3\Test3 ⭐️" ─► "E:\testarea3\Test3 __" (Y/N/A)? Yes

Found 3, fixed 3, failed 0, skipped 0 in "E:\testarea3\"

Thanks for the help.
 
May 30, 2022
31
0
Okay, here is my (semi-) final solution for this, seems to handle all of the cases that I have on hand.

I wanted the directory names truncated, extraneous junk on the end removed, but I couldn't seem to make fixnames do that, he wanted to substitute, not trim. I asked for blanks, he gave me underscores, so the directory names ended with " __" (one blank, two underscores).

If you want perfection, you will need to allow for file name collisions, but this is good enough for my purposes.

=======begin btm script================
@echo off
fixnames /D /Q:%@char[11088]%@char[65039] /R:%@char[32] *

for /d %%i in ("*") do (set fname=%%i & call :rename)
goto :eof
:rename
set str=%fname%
set str=%str: __=%
if %str% EQU %fname% exit /b
ren "%fname%" "%str%"
exit /b
=================end script======================
 
May 20, 2008
11,867
121
Syracuse, NY, USA
If it's always three bad characters on the right you could try (I haven't) a loop involving

Code:
...
ren "%x" "%@left[-3,%x]"
...
 
May 30, 2022
31
0
These are a bunch of download directories, there might be 20 or 30 directories, and only a few have the non-ascii characters on the end of the name. The script above handles those cases, and leaves the rest alone.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,596
97
Albuquerque, NM
prospero.unm.edu
I wanted the directory names truncated, extraneous junk on the end removed, but I couldn't seem to make fixnames do that, he wanted to substitute, not trim. I asked for blanks, he gave me underscores, so the directory names ended with " __" (one blank, two underscores).

Yeah, that whole /Q /R thing is pretty simpleminded. It always just replaces one character with one characters. I could probably work up some kind of syntax to delete characters. But that command is already overly ornate for its original purpose: fixing filenames containing percent signs (and a couple other characters troublesome for TCC).

Here's a different approach. A user-defined function to strip the offending characters out of a filename:
Code:
function fix=`%@replace[%@char[0x20 0x2b50 0xfe0f],,%@name[%1]]`

And using that function in REN via delayed expansion:
Code:
ren /a:d /s /n * `%@fix[*]`
 
May 30, 2022
31
0
Yeah, that whole /Q /R thing is pretty simpleminded. It always just replaces one character with one characters. I could probably work up some kind of syntax to delete characters. But that command is already overly ornate for its original purpose: fixing filenames containing percent signs (and a couple other characters troublesome for TCC).

Here's a different approach. A user-defined function to strip the offending characters out of a filename:
Code:
function fix=`%@replace[%@char[0x20 0x2b50 0xfe0f],,%@name[%1]]`

And using that function in REN via delayed expansion:
Code:
ren /a:d /s /n * `%@fix[*]`
I'm getting unexpected results from your code. It truncates the directory name at the first occurrence of a blank. It works for the test case I gave (above), but if you have any directory names with embedded blanks, it goes south.

Given a directory "xx yy zz", it would truncate to "xx".
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,596
97
Albuquerque, NM
prospero.unm.edu
I'm getting unexpected results from your code. It truncates the directory name at the first occurrence of a blank. It works for the test case I gave (above), but if you have any directory names with embedded blanks, it goes south.

Given a directory "xx yy zz", it would truncate to "xx".

Quite right. The function should have used %$ instead of %1 :
Code:
function fix=`%@replace[%@char[0x20 0x2b50 0xfe0f],,%@name[%$]]`

And it would probably be prudent to quote the replacement name:
Code:
ren /a:d /s /n * "%%@fix[*]"

And if that seems to work, you can remove the /N.