Share via


Import-csv file for robocopy

Question

Wednesday, January 28, 2015 2:50 PM

I am using a powershell script which imports a csv file to copy directories.
Since the folders which I need to copy contain spaces I use double quotes in the csv file.

Although I don't get the right syntax when I use import-csv

Either there are too many quotes in the source and/or destination folder or too few quotes.

the robocopy command should work fine if there are quotes at the beginning and the end of each folder.

So I started with the following syntax in my csv file:

source;destination
"source folder";"destination folder"

If I run the import-csv command with the content listed above it only shows double quotes at the destination folder.

so I started adding additonal double quotes around the source folder, it seems toe be an easy thing, but I didn't find the correct amount of double quotes to get the source and destination folder both starting and ending with double quotes.

any suggestion how to fix this ?

All replies (9)

Friday, January 30, 2015 7:26 AM ✅Answered

$csvFile = '.\test.csv'
$srchome = 'c:\source location'
$dsthome = 'c:\destination location'
$logfile = '.\copy.log'

Import-Csv $csvFile -Delimiter ';' | 
    ForEach-Object {
        $srcDoc = '"{0}\{1}"' -f $srcHome,$_.Source
        $dstDoc = '"{0}\{1}"' -f $dstHome,$_.Destination
        Write-Host $srcDoc,$dsrDoc -ForegroundColor green
        RoboCopy $srcDoc $dstDoc /z /e /np /MIR /COPY:DAT /r:10 /w:40 /log+:$logFile
    }

¯\(ツ)_/¯


Wednesday, January 28, 2015 3:03 PM

This code produces what is needed, you can modify it for your needs

Import-Csv F:\Testing\test.csv -Delimiter ";" | Foreach {
  """$($_.Source)""" + " " + """$($_.destination)"""
}

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet


Thursday, January 29, 2015 7:06 AM

Thanks,

But how does this fit into my script ?

I tried this, but it didn't work as it should:

$CsvFile = ".\test.csv"
$srchome = "c:\source location"
$dsthome = "c:\destination location"
$Logfile = ".\copy.log"

#$csv = Import-CSV $CsvFile -Delimiter ';'

$csv = Import-Csv $CsvFile -Delimiter ";" | Foreach {
  """$($_.Source)""" + " " + """$($_.destination)"""
}
    
    foreach ($folder in $csv)
    {
    $srcdoc = $srchome +$folder.source
    $dstdoc = $dsthome +$folder.destination

    robocopy $srcdoc $dstdoc /z /e /np /MIR /COPY:DAT /r:10 /w:30 /log+:$LogFile
    }

Thursday, January 29, 2015 8:18 AM

I recommend hiring a consultant ot a trined admin to help you sort this out. 

There are also good learning materials here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

With a little effor you will beable to understand these basic items that you have been shown.

¯\(ツ)_/¯


Thursday, January 29, 2015 9:13 AM

I think there is only a very simple thing that should be corrected in the above script.

I don't think hiring does the job, if that is all you can say, you'd better not reply


Thursday, January 29, 2015 9:23 AM

I think there is only a very simple thing that should be corrected in the above script.

I don't think hiring does the job, if that is all you can say, you'd better not reply

THe answer was given.  Now you want an  editor.  Please think about what you are doing.

If you cannot learn the technology you will need to hire someone to do it for you.

The answer was explicit. You only need to apply it to your script.

Start by reading the information in the link I posted.  A short bit of learning and you will beon your way.

This forum is not for free consulting ot free scripting.  It is for those who need to learn the technology.Almost anyone counl see the solution.  You cannot because you want others to do for you.  Please rethink you position and attemtp to learn the basicss of computers.

¯\(ツ)_/¯


Thursday, January 29, 2015 2:56 PM

I agree with jrv, do some reading to get a basic understanding of the concepts

The script I posted was just an example of how to enclose the data in quotes, since the quotes are removed when importing a csv, they are only there for the purpose of the data containing a comma (,). So now we need to put them back since, your paths contains spaces and breaks your robocopy.

Import-Csv F:\Testing\test.csv -Delimiter ";" | Foreach {
  """$($_.Source)""" + " " + """$($_.destination)"""
}

After I import the csv file, I send it over to the Foreach-Object cmdlet, this takes pipeline data and processes it. the $_ is the current object on the pipe line, hence me doing $_.Source

With your script, you are doing

$CsvFile = ".\test.csv"
$srchome = "c:\source location"
$dsthome = "c:\destination location"
$Logfile = ".\copy.log"

$csv = Import-CSV $CsvFile -Delimiter ';'

foreach ($folder in $csv)
{
  $srcdoc = $srchome +$folder.source
  $dstdoc = $dsthome +$folder.destination

  robocopy $srcdoc $dstdoc /z /e /np /MIR /COPY:DAT /r:10 /w:30 /log+:$LogFile
}

You are saving the results from Import-Csv to a variable $csv, then looping through the variable. So if you possible did the following it would work as my script showed

$srcdoc = """$($srchome$folder.source)"""
$dstdoc = """$($dsthome$folder.destination)"""

Now by looking at your script I am assuming that all the source and destination paths in your csv are \somepath\somepath\... since the srchome and dsthome do not have a ending \?

You can clean up the script to be

$csvFile = ".\test.csv"
$srchome = "c:\source location"
$dsthome = "c:\destination location"
$logfile = ".\copy.log"

Import-Csv $csvFile -Delimiter ";" | ForEach-Object {
  $srcDoc = """$($srcHome$_.Source)"""
  $dstDoc = """$($dstHome$_.Destination)"""

  RoboCopy $srcDoc $dstDoc /z /e /np /MIR /COPY:DAT /r:10 /w:40 /log+:$logFile
}

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet


Friday, January 30, 2015 6:59 AM

Hi,

Thank you for your clear explanation, but when running this script is says:

Unexpected token '$_' in expression or statement.

This error occurs in the following lines:

  $srcDoc = """$($srcHome$_.Source)"""
  $dstDoc = """$($dstHome$_.Destination)"""

What is happening here ?


Friday, January 30, 2015 8:30 AM

Thanks, that did the trick.

Thank you all for your help.