Powershell move/rename file and retain extension


hi

trying script in powershell , pretty sure close. need on finish line. not great @ scripting able tell here goes.

have loads of .zip files in deep folder structure. zip files contain folders , within them 1 pdf or 1 doc or 1 xls etc.

example: \folder1\04.zip contains \users\gil\desktop\accounts.pdf
example: \folder29\15.zip contains \users\bob\mydocs\etc.docx

want grab single file form zip , extract (ignoring irrelevant folders in zip) same location zip file is, renaming extracted file same name zip retaining extension of file. once done, zip can deleted. 

thoughts find zip file, extract temp folder, move or copy file location of zip file , rename zip file name. delete contents of temp folder before moving onto next zip etc etc.

here script. except can’t figure out how include extension in moving/renaming phase. if change ($_.basename) ($_.name) name , extension of zip in previous step.i think need capture name of file in get-childitem step?

have no doubt poorly written. 

great appreciated.

  function expand-zipfile($file, $destination)  {  $shell = new-object -com shell.application  $zip = $shell.namespace($file)  foreach($item in $zip.items())  {  $shell.namespace($destination).copyhere($item)  }  }      $folderpath="c:\users\test.user\test\1";      get-childitem $folderpath -recurse | %{         if($_.name -match "^*.`.zip$")      {          $parent="$(split-path $_.fullname -parent)";              write-host "extracting $($_.fullname) $parent"            expand-zipfile -file $_.fullname -destination c:\temp\                      remove-item $_.fullname            get-childitem -file -path c:\temp\ -recurse | move-item -destination $parent\$($_.basename)            remove-item c:\temp\* -force -recurse      }  }


thanks on rhys

i decided go drawing board , ended below batch script.

@echo off y: cd "\path\to\folders\"  /r %%f in (*.zip) (  	"c:\program files\7-zip\7z.exe" e -aoa -oc:\dme\temp "%%f"  >> \path\to\logs.txt 	ren c:\dme\temp\*.* %%~nf.* 	move c:\dme\temp\%%~nf.* "%%~pf"	 	del "%%~f" )
i'm sure still not perfect think better had , works well. appreciate however.


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607