Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need to recursivley get folder name and append to files

$
0
0

 As I process folders I want to capture the folder name and append to each file I process in that perticular folder.

 THis is a script I have that appends a file name to the end. I want the folder that it's currently processing added to the end of
each file.

 The folder structure example I'm using is

C:\sistemi  -- Need to recursivley get next to folders and pull Folder name in example.

$path='C:\Sistemi\'
get-childitem -path $path *.spx | % {" Starting File Process....."  >> $path\sistem.log
	   $PDate = get-date
       $date = get-date -uformat "_%d_%m_%Y_%H%M%S.bak"
       #"Set backup date to $date" >> $path\sistem.log
       $newname=($_.fullname -replace ".spx",$date)
	$file = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)"File backup name is $newname" >> $path\sistem.log 
       Rename-Item $_.fullname $newname"Renamed file to $newname" >> $path\sistem.log
	   $lines = get-content $newname
	   foreach ($line in $lines) 
	    {
			$line + " " + [System.IO.Path]::GetFileNameWithoutExtension($_.fullname) | Out-File "$path\sistem.csv"   -append
	    }		
       Write-Host $_.fullname "Files Processed on $PDate are $newname " >> $path\sistem.log
}


C:\Sistemi\REW1___1
C:\Sistemi\REW2___1 

I need to pull up to the underscore as the folder name (REW1,REW2 examples) and append that to the end of each processed file in my script.

 


 


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>