Question

I have a script that I've been working on, which reads a specified directory, locates multiple .CSV files, and executes some logic for each of the .CSV files and ultimately renames them .csv.archived . I'm trying to handle this as cleanly as possible, but I am making a mess.

The issue at hand, is that I cannot seem to figure out how to pass the individual file names through to strings for purposes of renaming the existing file. The process loops through fine, and the files ultimately get renamed but I get the following error:

enter image description here

#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "\\SERVER\Audit Test\" -recurse | where {$_.extension -eq ".csv"} | % {
$_.Name
}

In the code above, my thoughts are that $_.Name is where I (believe I) am pulling the file name of each file. At the end of this next block, the file is renamed with the file name.

#for each file found in the directory
ForEach ($item in $Filecsv) {

 #count the times we've looped through
 "Iterations : " + $iterations


# get the date and time from the system
$datetime = get-date -f MMddyy-hhmmtt
# rename the file 
rename-item -path ("\\SERVER\Audit Test\" + $_.Name ) -newname ($filename + $datetime + ".csv.archived")
$iterations ++

}

I think the process is fubar'd here:

rename-item -path ("\\SERVER\Audit Test\" + $_.Name )

I've gutted the irrelevant code for testing purposes, and would be happy if someone could tell me that I am doing something wrong, and that I am not crazy.

I am not sure that I properly understand the way that the ForEach loop works, TechNet helps: http://social.technet.microsoft.com/Forums/en-US/e8da8249-ea91-4772-ae85-582a4b37425b/powershell-foreachobject-vs-foreach?forum=smallbusinessserver

But doesn't answer my particular question.

Anyone care to shed some light?

Thanks! Here's the full script:

$iterations = 1

#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "\\SERVER\Audit Test\" -recurse | where {$_.extension -eq ".csv"} | % {
$_.Name
}

#check to see if files exist, if not exit cleanly 


#for each file found in the directory
ForEach ($item in $Filecsv) {



 #count the times we've looped through
 "Iterations : " + $iterations


# get the date and time from the system
$datetime = get-date -f MMddyy-hhmmtt
# rename the file 
rename-item -path ("\\SERVER\Audit Test\" + $_.Name ) -newname ($_.Name + $datetime + ".csv.archived")
$iterations ++

}
Was it helpful?

Solution

If it were me I'd work with the files as objects and not just a string for their name. So I'd do something like this:

#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "\\SERVER\Audit Test\" -recurse | where {$_.extension -eq ".csv"} 

#for each file found in the directory
ForEach ($item in $Filecsv) {

 #count the times we've looped through
 "Iterations : $iterations"


# get the date and time from the system
$datetime = get-date -f MMddyy-hhmmtt
# rename the file 
$NewName = $item.fullname -replace ".csv$","$datetime.csv.archived"
$Item.MoveTo($NewName)
$iterations ++

}

That takes each file, sets up a new name by replacing the .csv at the end with the $datetime.csv.archived that you want, and then moves the file to the new name effectively renaming it.

Also, if you want the Name property for each item instead of doing a ForEach{$_.Name} you are probably better off doing Select -Expand Name

OTHER TIPS

This works for me, I changed $_.Name to $item (which is a string at this point so doesn't have the name property) and it works

$iterations = 1

#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "c:\AuditTest\" -recurse | where {$_.extension -eq ".csv"} | % {
$_.Name
}

#check to see if files exist, if not exit cleanly 


#for each file found in the directory
ForEach ($item in $Filecsv) 
{

     #count the times we've looped through
     "Iterations : " + $iterations


    # get the date and time from the system
    $datetime = get-date -f MMddyy-hhmmtt
    # rename the file 
    rename-item -path ("c:\AuditTest\" + $item ) -newname ("c:\AuditTest\" + $item + $datetime + ".csv.archived")
    $iterations ++

}

Depending on exactly what you're doing though I'd change it like this

$Filecsv = get-childitem "c:\AuditTest\" -recurse | where {$_.extension -eq ".csv"}

This will give you the actual file object rather than a string which gives you more options in terms of parsing the names etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top