使用非ASCII字符的PowerShell和文件名(例如
我试图索引我的电影集,并在此问题上遇到了一个问题,在这个问题中,由于特殊角色,至少在进口阶段跳过了一个标题。该代码由于从æ开始而跳过“æon通量”。有人会知道如何纠正这一点吗?
Clear-Host
# Variables:
$movie_dir = "K:\Movies"
# Because reasons...
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
# Connect to the library MySQL.Data.dll
Add-Type -Path 'C:\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.8\MySql.Data.dll'
# Create a MySQL Database connection variable that qualifies:
$Connection = [MySql.Data.MySqlClient.MySqlConnection]@{ConnectionString='server=127.0.0.1;uid=username;pwd=password;database=media'}
$Connection.Open()
# Drop the table to clear all entries.
$sql_drop_table = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql_drop_table.Connection = $Connection
$sql_drop_table.CommandText = 'DROP TABLE Movies'
$sql_drop_table.ExecuteNonQuery() | Out-Null
# (Re)create the table.
$sql_create_table = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql_create_table.Connection = $Connection
$sql_create_table.CommandText = 'create table Movies(movie_id INT NOT NULL AUTO_INCREMENT, movie_title VARCHAR(255) NOT NULL, movie_file_date INT, movie_IMDB_id INT, PRIMARY KEY (movie_id))'
$sql_create_table.ExecuteNonQuery() | Out-Null
$movies = Get-ChildItem $movie_dir -File -include *.mp4 -Recurse -Depth 1 |
Select-Object -ExpandProperty FullName |
Sort-Object |
Get-Unique |
where{$_ -ne ""}
foreach ($movie in $movies)
{
# .net function to get just the filename (movie title).
$title = [System.IO.Path]::GetFileNameWithoutExtension($movie)
# Get the creation date of the movie and reformat it to yearmonthday.
$add_date = (Get-ChildItem $movie).CreationTime.toString("yyyyMMdd")
$query = "INSERT INTO Movies(movie_id, movie_title, movie_file_date) VALUES(NULL, @title, $add_date)"
$command = $connection.CreateCommand()
$command.CommandText = $query
# Sanatize single quotes in filenames for input.
$command.Parameters.AddWithValue("@title", $title) | Out-Null
$command.ExecuteNonQuery() | Out-Null
}
# Close the MySQL connection.
$Connection.Close()
Write-Host
Write-Host("Added") $movies.Count ("movies.")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为是Get-ChildItem以那个特殊的角色跳过文件。更有可能,您需要告诉您的MySQL使用UTF-8。
为此,请查看如何使mySQL正确处理UTF-8
至于您的代码,我将更改此信息:
从那里开始使用
FileInfo对象并使用:
I don't think it is the Get-ChildItem that skips the file with that special character. More likely, you need to tell your MySql to use UTF-8.
For that, have a look at How to make MySQL handle UTF-8 properly
As for your code, I would change this:
into
and work with the FileInfo objects from there on:
尽管Æ不是ASCII角色,它不是“特殊”,因此我编辑了问题标题和标签以反映这一点。
$命令,这是插入的行数。但是,您正在丢弃此值,但是
……如果
insert
失败,它会掩盖问题。取而代之的是,测试结果并适当响应...这将表明问题是否在于与文件系统或数据库进行交互。
其他一些注释:
mysqlcommand
实现 idisposable 接口,因此每个实例都应 /a>完成它......以及
$ sql_drop_table
和$ sql_create_table
的相同。中的代码最后
块中的代码即使从尝试
block 。参见 with parameters.add and parameters.addwithvalue 及其链接及其为何
addwithvalue(addwithvalue()代码>可能是有问题的。
而不是...
...a more typical way to build this message would be with
Though Æ is not an ASCII character it is not otherwise "special", so I edited the question title and tags to reflect that.
ExecuteNonQuery()
returns the number of rows affected by the command; in the case of$command
, it's the number of rows inserted. You are discarding this value, however......which masks the problem in the event the
INSERT
fails. Instead, test the result and respond appropriately...This will make it clear if the issue lies in interacting with the filesystem or the database.
Some other notes:
MySqlCommand
implements theIDisposable
interface and so each instance should be disposed when you're done using it......and the same for
$sql_drop_table
and$sql_create_table
. The code in thefinally
block will run even if an error is thrown from within thetry
block.See Difference with Parameters.Add and Parameters.AddWithValue and its links for why
AddWithValue()
can be problematic.Instead of...
...a more typical way to build this message would be with string interpolation...
...or the format operator...
You can also incorporate numeric format strings, so if
$movies.Count
is1234
and$PSCulture
is'en-US'
then......and...
...will both write...