大家好,我正在尝试将每个月的文件移动到一个文件结构中,结构是这样的:
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"
}}
发布于 2014-11-14 06:18:38
下面是一个函数,它将一些参数设置为mandatory,因此如果您在运行它时没有提供它,它应该会提示您输入。根据文件类型将目标目录设置为预定义位置,但如果在运行时指定,则可以重写目标目录。月份输入假定为数字。如果要将其应用到生产环境中,我预计会出现一些输入验证、确认提示和可能的结果验证。这是一个wiki答案,看看是否有人想要添加任何东西。
用法: Move-Files -sourceDir c:\temp -type xls -year 2014 -month 11
Override: Move-Files -sourceDir c:\temp -type xls -year 2014 -month 11 -destinationDir "Override Location“
Function Move-Files{
param(
[Parameter(Mandatory=$True)][string]$sourceDir,
[Parameter(Mandatory=$True)][string]$type,
[string]$destinationDir,
[Parameter(Mandatory=$True)][string]$year,
[Parameter(Mandatory=$True)][string]$month
)
# Add wildcard and period to search for all files of the given type by extension
[string]$searchtype = '*.' + $type
# Determine quarter of the year
if ($month -like "1" -or $month -like "2" -or $month -like "3"){[string]$quarter = "Quarter 1"}
elseif ($month -like "4" -or $month -like "5" -or $month -like "6"){[string]$quarter = "Quarter 2"}
elseif ($month -like "7" -or $month -like "8" -or $month -like "9"){[string]$quarter = "Quarter 3"}
elseif ($month -like "10" -or $month -like "11" -or $month -like "12"){[string]$quarter = "Quarter 4"}
# Read month number to pull month name
[string]$monthname = (get-date -Month $month).tostring("MMMM")
# Set destination based on file type.
if($type -like "xls" -and $destinationDir -like $null){[string]$destinationDir = "Excel Files"}
elseif($type -like "jpg" -and $destinationDir -like $null){[string]$destinationDir = "Image Files"}
elseif ($destinationDir -like $null){[string]$destinationDir = Read-Host 'Please enter destination directory.'}
# Build complete destination path
[string]$completeDir = $sourceDir + '\' + $year + '\' + $quarter + '\' + $monthname + '\' + $destinationDir
# Check for directory and create if not found
if (!(test-path $completeDir)) {[void](New-Item -ItemType Directory -Path $completeDir)}
# Move the files
Get-ChildItem -Path $($sourceDir + "\" + $searchtype) | Move-Item -Destination $completeDir -Force
}https://stackoverflow.com/questions/26884179
复制相似问题