Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
使用PowerShell移动文件并保留文本文件_Powershell - Fatal编程技术网

使用PowerShell移动文件并保留文本文件

使用PowerShell移动文件并保留文本文件,powershell,Powershell,如果我正在使用PowerShell搜索驱动器中的文件夹,以将它们从CSV移动到另一个文件夹。关于如何为移动到替换文件夹中的文件夹保留文本文件,我现在还处于空白状态 用于定位文件夹和移动的当前PowerShell代码: $File = Import-Csv C:\share\test\files.txt foreach ($fileName in $File.FileName) { Move-Item -Path "C:\share\test\OldLocation\$fileName"

如果我正在使用PowerShell搜索驱动器中的文件夹,以将它们从CSV移动到另一个文件夹。关于如何为移动到替换文件夹中的文件夹保留文本文件,我现在还处于空白状态

用于定位文件夹和移动的当前PowerShell代码:

$File = Import-Csv C:\share\test\files.txt
foreach ($fileName in $File.FileName) {
    Move-Item -Path "C:\share\test\OldLocation\$fileName" -Destination "C:\share\test\NewLocation\$fileName"
}

如果我看一下这个问题的标题,假设您想将文件移动到一个新位置,
您的CSV如下所示:

FileName
file1.docx
file2.docx
file3.docx
image1.jpg
这应该做到:

$oldLocation = 'C:\share\test\OldLocation'
$newLocation = 'C:\share\test\NewLocation'
# this is the path and filename for the text to leave behind
$movedFiles  = Join-Path -Path $oldLocation -ChildPath 'Files Moved.txt'

$messages    = @()
$filesToMove = Import-Csv 'C:\share\test\files.txt'
foreach ($file in $filesToMove.FileName) {
    $oldFile = Join-Path -Path $oldLocation -ChildPath $file
    $newFile = Join-Path -Path $newLocation -ChildPath $file

    if (Test-Path -Path $oldFile -PathType Leaf) {
        ################################################################################################
        # WARNING: Using parameter '-Force' will overwrite any file in the new location with that name. 
        # If that is not what you want, what will be your strategy ?
        ################################################################################################

        Move-Item -Path $oldFile -Destination $newFile    # -Force
        # add a new line for the text file
        $messages += "File '$file' has been moved to '$newLocation'"
    }
}
if ($messages.Count) {
    # write the textfile with all the files that have been moved in the old location
    Add-Content -Path $movedFiles -Value ($messages -join [Environment]::NewLine)
}
else {
    Write-Warning "No files have been moved."
}
移动文件后,旧位置应该有一个包含

File 'file1.docx' has been moved to 'C:\share\test\NewLocation'
File 'file2.docx' has been moved to 'C:\share\test\NewLocation'
File 'file3.docx' has been moved to 'C:\share\test\NewLocation'
File 'image1.jpg' has been moved to 'C:\share\test\NewLocation'

我不清楚你到底想把这些文本文件放在哪里。你是想把这些文件从原来的位置移走,而是把一个这样的文本文件留在原来的位置,还是想移动整个文件夹?标题是“移动文件”,但在问题中,您指的是“在驱动器中搜索文件夹以移动它们”。请具体一点我正在移动网络驱动器中的文件夹,我想在一个名为相同内容的文件夹中留下一个文本文件,但其中有一个文本文件,上面写着“联系它此文件夹已存档”。这基本上就是我要做的,只是我没有给出新的位置,只是让他们知道与我联系