Make a file copy, Rename the file, Copy the file to an ftp directory

rated by 0 users
This post has 4 Replies | 2 Followers

Not Ranked
Posts 3
ddander54 Posted: 01-19-2012 1:31 PM

I'm totally new to PowerShell, so bear with me....I have 3 books to read.

I have a daily task that I would like to automate with a script and then use Windows Task Scheduler to run it.  I copy and rename 6 files on a network drive, then move the files with the new names to an ftp directory. 

1) On my local PC I go to a network drive, and make a copy of 6 files (file name changes daily). 

2) Then I rename the files to a standard naming convention for each of the 6 files.  The original filename will be something like ABC_FILE_2012-C1630_01-18-12.xls and the new file name will be filename_x_c#.xls where '#' is replaced by the number following 'C' in the original file.  ie,  ABC_FILE_2012-C1630_01-18-12.xls becomes filename_x_c1630.xls.

3) Copy the 6 renamed files to an ftp server (with specific userid/pw) directory.

4) Need to Create a Windows Sheduled Task to run this script daily.

Can someone provide an outline of the PS script that I would need to do this?

TIA,

Don

Top 10 Contributor
Posts 323
Microsoft MVP
Top Contributor

I can help with some of this.

 

1. IN terms of which files to use - how many will there be in the file - will there only ever be those 6 files?

2. To rename them, you could resort to a regex, or some simple text cracking, or something like this:

$files = ls c:\whereeverhtefilescam3from =incl *.xls

foreach ($file in $files) {

   $fid = $file.split("_")[2].split("-")[1].substring(1)

   $nfn = "drive:\folder\" + "filename_x_" + $Fid + ".xls"

   copy $file.fullname $nfn

}

3. For some details on how to do the uploading, I wrote on script that may help. See: http://pshscripts.blogspot.com/2010/10/copy-filetoftpps1.html

 

4. Re task scheduler - See http://pshscripts.blogspot.com/2010/09/new-taskps1.html for a related script.

 

Hope this helps

 

 

 

Not Ranked
Posts 3

Thomas, Thank you for your feedback.

There are actually 3 .csv files and 3 .xls files.  The naming convention is slightly different, but very similar, so I should be able to figure out what to do based on your example.

Thanks for the other 2 links.  I will review shortly......

 

Don

Not Ranked
Posts 3

Ok, Im working my way thru this and have run into this error. 

 

foreach 

 

 

($filename in $files}

{

 

 

 

$fid = $filename.split("_")[2].split("-")[1].substring(1)

Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'split'.
At X:\Download\PowerShell Scripts\My Test.ps1:35 char:26
+    $fid = $filename.split <<<< ("_")[2].split("-")[1].substring(1)
    + CategoryInfo          : InvalidOperation: (split:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Anyone got any ideas?

Top 10 Contributor
Posts 323
Microsoft MVP
Top Contributor

This should look more like this:

 

Foreach ($file in $files) {

$fid = $file.name.split(...)

}

In your case, you were trying to call the split method on the file object, not the file name (which is a string and DOES have a split method)

Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.