简体   繁体   English

根据两列中的CSV值移动文件

[英]Moving files based on CSV values from two columns

I have been working on a script which moves files in a directory to a child directory which is created if the column 'folder' contains a 'Y'. 我一直在研究将目录中的文件移动到子目录的脚本,如果“文件夹”列包含“ Y”,则会创建该子目录。 The files which have no 'Y' in thier respective folder column are moved in to the folder of the most previous file name with a Y. 在各自的文件夹列中没有“ Y”的文件将被移到带有Y的最新文件名的文件夹中。

I seem to be having some issue with the move-item cmdlet. 我似乎对move-item cmdlet有问题。 This has been affecting me with the rename-item in another script im attempting so i think it has something to do with how im attempting to access the file name of existing files in a folder. 这已经在另一个尝试中的脚本中影响了我的重命名项,因此我认为这与尝试访问文件夹中现有文件的文件名有关。

$sourceDir = read-host "Please enter source Dir:"

$csv = import-csv C:\scripts\files\files.csv
$csv | where {$_.folder -eq 'Y'} | % {

        $path = $sourceDir + "\" + $_.fileName
        if(-not $_.PSIsContainer)
        {
            md $path
        }#end if
    }#end for

$move = ".\" + $csv[0].fileName 
$csv | % {
            if ($_.folder -eq 'Y')
            {
                $move = ".\" + $_.fileName
                mi $_.fileName "$move"
            }
            mi $_.fileName "$move"
        } #end for

Again the mi seems to be throwing the error. mi似乎再次抛出错误。 The first for loop creates the folders correctly but the second falls down. 第一个for循环可正确创建文件夹,但第二个则失败。 I was fiddling with it and I was able to make it move the files which correspond. 我在摆弄它,并能够使其移动对应的文件。

Sorry that was an older version of the script after I was having problems with the moving. 抱歉,在我遇到移动问题之后,该脚本是一个较旧的版本。

Move-Item : Cannot find path 'C:\scripts\BB.026.001.001.0017' because it does not exist.
At C:\scripts\Y2.ps1:25 char:7
+                 mi  <<<< $_.fileName "$move"
Move-Item : Cannot find path 'C:\scripts\BB.026.001.001.0017' because it does not exist.
At C:\scripts\Y2.ps1:27 char:6
+             mi  <<<< $_.fileName "$move"
Move-Item : Cannot find path 'C:\scripts\BB.026.001.001.0018' because it does not exist.
At C:\scripts\Y2.ps1:27 char:6
+             mi  <<<< $_.fileName "$move

"

The extension of the file as well as the correct path need to be referenced Thanks, 该文件的扩展名以及正确的路径都需要引用,谢谢,

Craig. 克雷格

OK, I'm unsure as to where you have the file extension stored, but I've rewritten your script putting everything into one foreach loop with an IF statement to enumerate whether a folder should be created. 好的,我不确定文件扩展名的存储位置,但是我已经重写了脚本,并使用IF语句将所有内容放入一个foreach循环中,以枚举是否应创建文件夹。

$sourceDir = "C:\Temp\sotemp\Source\"                                #Configure source file directory
$destDir = "C:\Temp\sotemp\Dest\"                                    #Configure destination file directory
$csv = import-csv 'C:\Temp\sotemp\files.csv'                         #Import CSV file

$csv | % {                                                           #For each line in the CSV
            $sourceFile = $sourceDir + $_.fileName + ".txt"
            if($_.folder -eq 'Y'){                                   #If the csv entry has a Y in the folder column
                $destPath = $destDir + $_.fileName                   #Build the destination directory path
                if (!(Test-Path -path $destPath)){                   #If the destination directory doesn't exist...
                    New-Item $destPath -itemtype directory -force    #Force creation of the destination directory if it doesn't exist
                }
                mi $sourceFile $destPath                             #Move the file!
            }
            else {
                $destPath = $destPath                                #This isn't needed, but it explicitly shows we are reusing the $destPath variable with whatever was in it the last time we populated it
                mi $sourceFile $destPath                             #Move the file!
            }
        }

Let me know if I'm way off target here, if I'm on target, you would just need to update the paths and file extension. 让我知道我是否偏离目标,如果我符合目标,则只需要更新路径和文件扩展名即可。 In addition, there is no error checking and I haven't used the '-force' parameter on file move to force an overwrite. 另外,没有错误检查,并且在文件移动时我没有使用'-force'参数来强制覆盖。 The script is also reliant on the first row in the CSV having a 'Y' in the folder column to ensure the $destPath variable is populated. 该脚本还依赖于CSV的第一行,在文件夹列中带有“ Y”,以确保填充$ destPath变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM