从CSV中的值创建文件夹 [英] Creating folders from values in a CSV

查看:197
本文介绍了从CSV中的值创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一小部分较大的脚本解决方案,其中我需要基于存储在CSV中的值创建文件夹,然后根据csv列中的值将适用的文件移动到新文件夹。



CSV的格式:

  fileName,文件夹
AC002 Y
AC034 Y
AC001
X2400 Y
AC006
AC007
AC009 Y

这是我为问题工作的代码:

  $ sourceDir = read-host请输入源目录:

$ csv = import-csv C:\scripts\files\files.csv
$ csv |其中{$ _。folder -eq'Y'} | %{
$ path = $ sourceDir +\+ $ _。fileName
if(-not(Test-Path $ path))
{
md $ path

} #end if
} #end for



感谢Shay Levy的帮助,
Craig

h2_lin>解决方案

尝试这样,它会创建文件夹,即使它存在。如果您只想创建不存在的文件夹,我们可以更改:

  $ csv = import-csv C:\ scripts\files\files.csv 
$ csv |其中{$ _。folder -eq'y'} | `
foreach {md -force(join-path $ sourceDir $ _。fileName)}


I am working on a small portion of a larger scripting solution in which i need to create folders based on values stored in a CSV and then move the applicable files in to the new folder according to values in a column of the csv.

The format of the CSV:

fileName, folder
AC002       Y
AC034       Y
AC001
X2400       Y
AC006
AC007
AC009       Y

This is the code I have working for the problem:

$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 (Test-Path $path))
            {
                md $path

            }#end if
        }#end for

The next step will probably be a bit more tricky.

Thanks to Shay Levy for the help, Craig

解决方案

Try this, it will create the folder even if it exists. We can change that if you want and create only folders that do not exist:

$csv = import-csv C:\scripts\files\files.csv
$csv | where {$_.folder -eq 'y'} | `
       foreach { md -force (join-path $sourceDir $_.fileName) }

这篇关于从CSV中的值创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆