Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Capture a string of characters and use as filename.

Reply
 
Old 06-06-2008   #1 (permalink)
MCSE_Sec


 
 

Capture a string of characters and use as filename.

I have hundreds of Excel files created using Powershell with generic filenames.

The first Cell (A,1) contains infomation I would like to use as the filename
for the Excel file.

The first 4 characters (in the Cell A,1) are the same in each file. I want
to capture the 5th through x character, with x being the character preceding
the ',' comma and save the Excel file as this string.

How can I do this?



My System SpecsSystem Spec
Old 06-06-2008   #2 (permalink)
Shay Levi


 
 

Re: Capture a string of characters and use as filename.



$xl = new-object -com excel.application

foreach($xls in (dir d:\scripts\temp *.xls)){
#$xl.Visible=$true
$workbook = $xl.workbooks.open($xls.fullname)
$sheet = $workbook.worksheets | where {$_.name -eq "sheet1"}
$name = $sheet.range("A1").value()
$newName = $name.substring(4,($name.indexOf(",")-4))
$workbook.close()
move-item $xls.fullName ($xls.directoryName + "\$newName.xls")
}

$xl.quit()



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I have hundreds of Excel files created using Powershell with generic
> filenames.
>
> The first Cell (A,1) contains infomation I would like to use as the
> filename for the Excel file.
>
> The first 4 characters (in the Cell A,1) are the same in each file. I
> want to capture the 5th through x character, with x being the
> character preceding the ',' comma and save the Excel file as this
> string.
>
> How can I do this?
>

My System SpecsSystem Spec
Old 06-06-2008   #3 (permalink)
MCSE_Sec


 
 

Re: Capture a string of characters and use as filename.

Shay,

Thanks so much! I had to modify just a little since my worksheets were
labeled as the filename as well (exluding the extension). Your help was
appreciated greatly!


"Shay Levi" wrote:
Quote:

>
>
> $xl = new-object -com excel.application
>
> foreach($xls in (dir d:\scripts\temp *.xls)){
> #$xl.Visible=$true
> $workbook = $xl.workbooks.open($xls.fullname)
> $sheet = $workbook.worksheets | where {$_.name -eq "sheet1"}
> $name = $sheet.range("A1").value()
> $newName = $name.substring(4,($name.indexOf(",")-4))
> $workbook.close()
> move-item $xls.fullName ($xls.directoryName + "\$newName.xls")
> }
>
> $xl.quit()
>
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > I have hundreds of Excel files created using Powershell with generic
> > filenames.
> >
> > The first Cell (A,1) contains infomation I would like to use as the
> > filename for the Excel file.
> >
> > The first 4 characters (in the Cell A,1) are the same in each file. I
> > want to capture the 5th through x character, with x being the
> > character preceding the ',' comma and save the Excel file as this
> > string.
> >
> > How can I do this?
> >
>
>
>
My System SpecsSystem Spec
Old 06-06-2008   #4 (permalink)
Kryten


 
 

Re: Capture a string of characters and use as filename.

Quote:

> $xl = new-object -com excel.application
>
> foreach($xls in (dir d:\scripts\temp *.xls)){
> #$xl.Visible=$true
> $workbook = $xl.workbooks.open($xls.fullname)
> $sheet = $workbook.worksheets | where {$_.name -eq "sheet1"}
> $name = $sheet.range("A1").value()
> $newName = $name.substring(4,($name.indexOf(",")-4))
> $workbook.close()
> move-item $xls.fullName ($xls.directoryName + "\$newName.xls")
>
> }
>
> $xl.quit()

That solution from Shay is going right into my "Scriptbits" container
in ASE.

One question though..shouldn't the $newName = $name.substring(4,
($name.indexOf(",")-4))
be $newName = $name.substring(4,($name.indexOf(",")-1)) to crank the
index back 1 place before the ","?

Just wanted clarification because I'll definitely use this one.

Thanks Shay!

Stuart



My System SpecsSystem Spec
Old 06-06-2008   #5 (permalink)
Shay Levi


 
 

Re: Capture a string of characters and use as filename.


Hi Stuart,



I tested it against a string that looks like:

PS > $name = "sameSELECTME,bla bla"


Using -1 returns:

PS > $name.substring(4,($name.indexOf(",")-1))
SELECTME,bl


While -4 returns:

PS > $name.substring(4,($name.indexOf(",")-4))
SELECTME



I used -4 because you have to take into count the first 4 fixed characters
(remove them). Here's another example:

# get all characters until the comma sign
PS > $name.substring(0,$name.indexOf(","))
sameSELECTME

#get all characters until the comma sign and then remove the first 4 characters
PS > $name.substring(0,$name.indexOf(",")).remove(0,4)
SELECTME



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:
Quote:

>> $xl = new-object -com excel.application
>>
>> foreach($xls in (dir d:\scripts\temp *.xls)){
>> #$xl.Visible=$true
>> $workbook = $xl.workbooks.open($xls.fullname)
>> $sheet = $workbook.worksheets | where {$_.name -eq "sheet1"}
>> $name = $sheet.range("A1").value()
>> $newName = $name.substring(4,($name.indexOf(",")-4))
>> $workbook.close()
>> move-item $xls.fullName ($xls.directoryName + "\$newName.xls")
>> }
>>
>> $xl.quit()
>>
> That solution from Shay is going right into my "Scriptbits" container
> in ASE.
>
> One question though..shouldn't the $newName = $name.substring(4,
> ($name.indexOf(",")-4))
> be $newName = $name.substring(4,($name.indexOf(",")-1)) to crank the
> index back 1 place before the ","?
> Just wanted clarification because I'll definitely use this one.
>
> Thanks Shay!
>
> Stuart
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to prepend a filename with a pattern string? VB Script
Remove characters in a string VB Script
Move file to folder depending on last two characters of filename VB Script
How to Randomize characters in a string VB Script
Removing characters from a string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46