大家好,我正在尝试将每个月的文件移动到一个文件结构中,结构是这样的:
Year >> Quarter >> Month >> FileType日志文件会自动放到一个日志文件夹中--我想把这些文件中的任何一个放入上面的结构中。
我尝试了以下几种方法:
function moveFiles{
# The three parameters.
param([string]$sourceDir, [string]$type, [string]$destinationDir)
# Move the files
Get-ChildItem -Path $sourceDir -Include $type | Move-Item -Destination $destinationDir - Force
}然而,每个月它都会将每个文件移动到新的月份中,我知道这与递归有关,但我删除了它,但没有文件被移动。
任何帮助都将不胜感激。
我如何调用函数:
$sourceDir = "C:\Logs"
$destinationExcelDir = ($monthFolder + "\Excel Files");
#Moving all Excel files from logs folder to Archive
moveFiles $sourceDir "*.xls" $destinationExcelDir使用write-host,我可以确认两个路径都是正确的,而且也是不同的,但是我尝试将文件复制到同一主目录下的子文件夹中。
例如,$destinationExcelDir为"C:\Logs\2014\quarter 4\11.11\Excel“
发布于 2014-11-13 13:30:21
假设您希望按照文件的上次写入时间对C:\logs中的文件进行排序,下面的函数修订版应该会有所帮助。您只需为路径提供一个通配符,其中包含扩展名和路径名中的最后一个文件夹名称作为函数的参数。
示例用法: moveFiles C:\Logs*.xls "Excel文件“
function moveFiles {
param(
[string]$source,
[string]$destinationDir
)
Get-ChildItem $source -file | foreach {
$year = $_.LastWriteTime.Year
# Switch statement to get the quarter name
switch -regex ($_.LastWriteTime.Month) {
"[1-3]" {$quarter = 'Quarter 1'}
"[4-6]" {$quarter = 'Quarter 2'}
"[7-9]" {$quarter = 'Quarter 3'}
"[10-12]" {$quarter = 'Quarter 4'}
}
# Switch statement to get the month name
switch ($_.LastWriteTime.Month) {
'1' {$month = 'January'}
'2' {$month = 'Feburary'}
'3' {$month = 'March'}
'4' {$month = 'April'}
'5' {$month = 'May'}
'6' {$month = 'June'}
'7' {$month = 'July'}
'8' {$month = 'August'}
'9' {$month = 'September'}
'10' {$month = 'October'}
'11' {$month = 'November'}
'12' {$month = 'December'}
}
# Create the destination folder if it doesn't exist
if (!(test-path "C:\Logs\$year\$quarter\$month\$destinationDir")) {
New-Item -ItemType Directory -Path "C:\Logs\$year\$quarter\$month\$destinationDir"
}
# Move the files into the correct folder
move-item $_ "C:\Logs\$year\$quarter\$month\$destinationDir"
}}
https://stackoverflow.com/questions/26884179
复制相似问题