如果我注释掉下面的重命名行,则代码可以处理文件,但是我想要实现的是重命名文件,然后移动文件。该文件只是保留在源文件中,因此在此行中根本不会移动或重命名。
function RenameFile($location, $filename, $extension){
$d = Get-Date -UFormat "%Y%M%D"
$old = $location + $filename + $extension
$new = $filename + "_" + $extension
}
$folder = 'C:\Users\GeorgeH\Desktop\FileSystemWatcher\Folder to Monitor'
$filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubDirectories = $false; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action{
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
RenameFile -location "C:\Users\GeorgeH\Desktop\FileSystemWatcher\Folder to Monitor\" -filename "TEST" -extension .txt
Move-Item "C:\Users\GeorgeH\Desktop\FileSystemWatcher\Folder to Monitor\*.txt" -Destination "C:\Users\GeorgeH\Desktop\FileSystemWatcher\New
Folder 1"
}
# Unregister-Event FileCreated
答案 0 :(得分:0)
您的所有RenameFile
函数所做的都是设置似乎从未使用过的变量。
要真正重命名文件,您应该调用Rename-Item
:
function RenameFile($location, $filename, $extension){
Rename-Item -Path "$($location)/$($filename)$($extension)" -NewName "$($location)/$($filename)_$($extension)"
}
($()
可以避免与/
和_
的扩展冲突)
请参阅:Rename-Item