Я прочитал https://stackoverflow.com/questions/ask/advice? Переименовать элемент Powershell, если файл существует http://social.technet.microsoft.com/Forums/scriptcenter/en-US/c347ce7a-3e23-4476-90c2-37b79785ac2f/moving-files-based-on-modified-date Powershell: рекурсивное перемещение файлов http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/22/hey-scripting-guy-september-22-2009.aspx и еще около двух десятков статей.
Прошу прощения, я плохо разбираюсь в сценариях, хотя мне нравится изучать его и возможности, которые он предоставляет. Мои предыдущие должности не требовали от меня написания сценариев, кроме пакетных файлов с использованием Windows CMD. Недавно я принял должность сетевого администратора, и мой руководитель попросил меня написать сценарий, который будет перемещать файлы на основе нескольких критериев. Для этого я использую Powershell.
- Файлы старше одного дня перемещаются - не копируются - из $ path в $ destination
- Исключение относится к callpilot.dev, который должен оставаться нетронутым.
- Файлы, находящиеся в исходной папке, а также файлы, находящиеся во всех подпапках, должны быть перемещены
- Структура исходной папки должна оставаться на месте
- Верхняя папка назначения должна содержать фактическую дату минус 1 день.
- Вся структура подпапок должна быть создана с использованием тех же имен, что и исходная структура.
- Остальные исходные файлы следует удалить (кроме callpilot.dev).
Вот моя структура тестовых папок:
c:\temp\others\callpilot
c:\temp\others\ftps
c:\temp\others\mysql
c:\temp\others\rss-enterprise
c:\temp\others\rss-sql2k8
c:\temp\others\tree.txt
c:\temp\others\callpilot\example2.gz
c:\temp\others\callpilot\backup.gz
c:\temp\others\callpilot\barnak.bak
c:\temp\others\callpilot\callpilot.dev
c:\temp\others\callpilot\IPESystemBackup 131022 2000.bkp
c:\temp\others\ftps\www.123.com
c:\temp\others\ftps\www.123.com\backup-10.22.2013_00-00-08_123.tar.gz
c:\temp\others\ftps\www.123.com\backup-10.23.2013_00-00-08_123.tar.gz
c:\temp\others\ftps\www.123.com\backup-10.4.2013_00-00-07_123.tar.gz
c:\temp\others\mysql\sql.txt
c:\temp\others\rss-enterprise\enterprise.txt
c:\temp\others\rss-sql2k8\data
c:\temp\others\rss-sql2k8\data\New Bitmap Image.bmp
c:\temp\others\rss-sql2k8\data\New Journal Document.jnt
c:\temp\others\rss-sql2k8\data\New Microsoft PowerPoint Presentation.pptx
c:\temp\others\rss-sql2k8\data\New Microsoft Visio Drawing.vsd
Вот мой сценарий:
$date = get-date
$path = "C:\temp\others\*.*"
$destination = new-item "c:\temp\others\$($date.toshortdatestring().replace("/","-"))" -type directory
Foreach($file in (Get-ChildItem $path -file -recurse -exclude "callpilot.dev"))
{
If($file.LastWriteTime -gt (Get-Date).adddays(-1).date)
{
Move-Item -Path $file.fullname -Destination $destination
}
}
Мне не хватает №6 и №7. Не могли бы вы помочь мне их найти?
Я получил ваш ответ, спасибо за это. Что-то все еще не работает.
Вот мой сценарий:
$date = get-date
$path = "C:\temp\others\*.*"
$destination = new-item "c:\temp\others\$($date.toshortdatestring().replace("/","-"))" -type directory
Foreach($file in (Get-ChildItem $path -file -recurse -exclude "callpilot.dev"))
{
If($file.LastWriteTime -gt (Get-Date).adddays(-2).date)
{
$destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root.ToString())
New-Item $destination -Type Directory -ea SilentlyContinue
Move-Item $file $destination
} else {
Remove-Item $file
}
}
Файлы перемещены, но папка не создана. И ошибка:
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Move-Item : Cannot create a file when that file already exists.
At C:\temp\organize_final222.ps1:11 char:9
+ Move-Item $file $destination
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\temp\others\...sio Drawing.vsd:FileInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Я вставил ваши строки в нужное место?