0

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

1
  • all files and folders in the csv file are located under $sourceDir? Commented Sep 12, 2011 at 12:14

1 Answer 1

1

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) }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much I had never seen the join-path cmdlet before. This works a treat, I wasnt sure how to filter the fields before a for loop and its blindingly obvious now. I have edited my original to show how i know how to create folders only if they dont exist, if you know a way of improving this code I would love to see it, just wanted to show im thinking of how ot do it myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.