Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File powershell复制磁盘,不包括特定文件类型_File_Powershell_Copy - Fatal编程技术网

File powershell复制磁盘,不包括特定文件类型

File powershell复制磁盘,不包括特定文件类型,file,powershell,copy,File,Powershell,Copy,我正在编写一个PowerShell 2脚本,该脚本将读取CD-ROM或DVD光盘并复制其内容-需要注意的是,我需要检查某些文件类型,而不是复制该文件类型的任何内容。到目前为止,我的工作如下: $user = read-host "Enter owner's username:" $drv = read-host "Enter Optical Drive letter (no colons or slashes):" $list = Import-Csv badtypes.csv $badlis

我正在编写一个PowerShell 2脚本,该脚本将读取CD-ROM或DVD光盘并复制其内容-需要注意的是,我需要检查某些文件类型,而不是复制该文件类型的任何内容。到目前为止,我的工作如下:

$user = read-host "Enter owner's username:"
$drv = read-host "Enter Optical Drive letter (no colons or slashes):"

$list = Import-Csv badtypes.csv
$badlist = @()
$Extns = @()

ForEach($xt in $list)
{
    $Extns += "."_$xt.Extention
}

$filepath = $drv +":\"
$cnts = Get-ChildItem $filepath -r

ForEach($itm in $cnts)
{
   if($itm.PSis.Container)
   {
       #write new folder name in user's temporary folder
   }
   else
   {
       CheckFile $itm
   }
}

Function CheckFile($fl)
{
    $fildextension = [System.IO.Path]::GetExtention($fl)

    $badfound = 0

    ForEach($a in $Extns)
    {
        if($a -eq $fildextension)
        {
           $badfound = 1
        }
    }

    if(badfound -eq 0)
    {
       write-host "File Type Acceptable:" $fl
       # write file to proper place in the user's temporary folder 
    }
}
我在获取在正确位置创建的空文件夹并将(可接受的)文件复制到正确位置时遇到问题


任何帮助都将不胜感激

坚持使用powershell解决方案,请尝试以下尺寸:

$user = read-host "Enter owner's username:"
$drv = read-host "Enter Optical Drive letter (no colons or slashes):"

$list = Import-Csv badtypes.csv
$Exclusions = "*.$($list.Extension -join ",*.")"
$DestFolder = New-Item -path "\\BAAC\homedir\$user\transfer\$(get-date -f MMddyyyy.HH.mm)"  -ItemType Directory
$FilePath = $drv +":\"

Write-Host "Copying files from $FilePath to $DestFolder`:"

Copy-Item "$FilePath*" -Destination $DestFolder -Exclude $Exclusions -Recurse -PassThru
explorer $DestFolder
不需要用户名,但我把它放在那里了。以下是脚本将执行的操作:

  • 它得到驱动器号
  • 导入要排除的扩展名列表,从中生成字符串(如果CSV有三个条目分别为bat、exe和com,则生成的字符串类似于“
    *.bat、*.exe、*.com
  • 它在所需文件夹中创建日期/时间格式的文件夹
  • 然后它递归地将文件和文件夹复制到新文件夹,排除排除列表中的任何内容
  • 最后,它会打开一个Windows资源管理器窗口,指向包含最近复制的所有文件和文件夹的目标文件夹

编辑:更新为使用注释中指定的路径。如果此答案为您的问题提供了解决方案,请将其标记为所选答案,以便未来用户可以找到并使用它,而无需重复提问。

您是否考虑过使用ROBOCOPY解决此问题?它是高度可配置的,返回可用的退出代码和日志文件。谢谢!因此,用户临时文件夹的路径将是奇数,因为执行此操作的人不是介质的所有者。因此,我还必须获得一个服务帐户(用于此类脚本的管理员)来编写文件。该目录看起来像:\\BAAC\homedir\username\transfer\newFolderNameByDate.hour.minuteI已更新我的答案以匹配您的目标路径,如果该答案为您的问题提供了解决方案,请将其标记为解决方案。太棒了!我也能让它工作,但你的版本更圆滑!我把这个作为解决方案——希望我能投赞成票。再次感谢你!胡扯!需要排除的文件类型正在通过排除输入文件的确切外观是什么?我不需要整个东西,但包括任何标题的样本将是好的。我测试了这段代码,所以它不应该让事情通过,除非我假设你的CSV内容是错误的。