Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Escaping "\" when naming a file as "ParentDirectory\FileName"

$
0
0

Hello!

I have this script:

Function StripATF{

if(test-path -Path "$FolderWhereATFare\Extracted ATF Blocks")
{
remove-item -Path "$FolderWhereATFare\Extracted ATF Blocks" -recurse
}
$source_folder = "$FolderWhereATFare"
$dest_folder = "$FolderWhereATFare\Extracted ATF Blocks"
cd $source_folder
New-Item -Path "$FolderWhereATFare\Extracted ATF Blocks" -ItemType directory -Value $_ -Force
$files = get-childItem "$FolderWhereATFare" -include *.atf  -recurse
foreach($file in $files){
$foldername = get-childItem "$file" -include *.atf  -recurse |select -expand fullname | foreach {"{0}-{1}" -f ($_ -split '\\')[-2,-1] }
get-content($file.fullname)|Foreach-Object {

  $_ -replace 'CMD', '' `
     -replace '0x', '' `
           -replace 'DI', '' `
           -replace 'SW', '' `
           -replace 'LO 0', '' `
           -replace 'LI', '' `
           -replace 'LE 0', '' `
           -replace '\$ ', ''`
           -replace '\$', ''
     } | Set-Content "$dest_folder\$($foldername)"} 
    

     }
StripATF

and it names the file I am altering as "ParentDirectory-FileName.atf".

However I would like to know if it is possible to name the file as "ParentDirectory\FileName.atf" without causing an exception error saying it cannot find the path. Is the "\" excapeable?

The part where it grabs the name of the file and alters the name is here:

$foldername = get-childItem "$file" -include *.atf  -recurse |select -expand fullname | foreach {"{0}-{1}" -f ($_ -split '\\')[-2,-1] }

When I set the content here:

Set-Content "$dest_folder\$($foldername)"

the set-content cmd-let will throw an exception error if I change the "-" to "\".

Any suggestions?

Thanks! 


Viewing all articles
Browse latest Browse all 10624

Trending Articles