How to simply list all files of a folder using dir to text with UTF-8 encoding?
Asked Answered
A

2

6

Simple question:

On Windows 10, I found a quick and dirty solution to save the names of all the files in a folder by just simply:

  • Creating a text file
  • Type and save dir > 1.txt within it
  • Rename the .txt file to .bat
  • Run it. Done.

But, the text file this creates is in ANSI encoding, and thus won't show certain characters... Is there a simple script I can add to the bat file before running it to make the generated texts encoded as UTF-8?

Aulea answered 9/1, 2021 at 9:6 Comment(5)
Add this command chcp 65001 to change code page to UTF-8Suggestible
What do you think you need to use UTF-8 for? If you think it is required in order to print certain characters, can you tell us what those characters are? The Windows character set uses 8 bits to represent each character; therefore, the maximum number of characters that can be expressed using 8 bits is 256 which is usually sufficient for western languages, including the diacritical marks used in French, German, Spanish, and other languages. However, if your characters are from eastern languages you may need to use Unicode to employ those characters, but that would not necessarily be UTF-8.Malignity
BTW, your provided command does not save the names of all the files, it includes much more information for that. If you want only the names, then open a Command Prompt window, type dir /?, press the [ENTER] key, and take a look at its options. As a minimum, for just the file names, you should use the /B option, and the /A option with -D, (which will exclude directories). There is also no need to perform all of your bulletted items, just open a Command Prompt window, and type, for example Dir /B /A:-D "C:\Users\ShoulderMonster\Desktop" 1> "C:\Users\ShoulderMonster\Documents\1.txt"Malignity
Oh, I didn't know that about UTF-8! Maybe that's why one of my Japanese text files is in UTF-16 LE? Thank you for that tidbit! :) I try to avoid Command Prompt solutions because admittedly I don't understand it too well. And most of all, the bat way is easy to remember! I tried your code, but it outputted Japanese as ??? despite being UTF-8 and few enough characters.. Not sure why. I really like that it's JUST the list, but I'm afraid one of these days I'll type the command incorrectly. Maybe if I do it enough I'll get it, though! Is there a way to do the same thing and output UTF-16?Aulea
Okay, nevermind about needing UTF-16. Even getting its CHCP code it wouldn't work (and a bit complicated explanation why), but either way I didn't need it after all. The lists I needed to create are only at max 100 files long, and not all of them have Japanese characters, so Hackoo's solution worked for everything. :) For context, I used to use my 3DS as an MP3 player, but most of the songs got corrupted for some reason. So, I've been meaning to note all of them and recollect what I can! Thank you both for the prompt help!Aulea
S
15

You should add this command chcp 65001 before dir command to change code page to UTF-8

@echo off
CHCP 65001>nul
dir>1.txt

Further reading about CHCP command

Suggestible answered 9/1, 2021 at 9:55 Comment(2)
Oh thank you so much, that did the trick!! May I ask, what is the CHCP code for UTF-16 LE? :)Aulea
Nevermind, all is good and your guide as is was enough for my purposes! Thank you for the help!Aulea
G
1

This works for me:

PowerShell -Command "TREE /F | Out-File output.txt -Encoding utf8"
Goldy answered 2/11, 2021 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.