locked
Zipping file throws error RRS feed

  • Question

  • Hi There,

    I am facing some issues while zipping files in particular directory through power shell.

    I am getting the error message:-

    ==========================================================
    Exception calling "CreateFromDirectory" with "4" argument(s): "Could not find a part of the path 'C:\WINDOWS\system32\2018-02-13'."
    At line:32 char:1
    + [System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder,$ZipTo, $C ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DirectoryNotFoundException
     ===================================================

    Below is my code-

    #get the list of files 
    $here = (Get-Item -Path ".\" -Verbose).FullName
    $tempVariable = $here
    $files_to_zip = Get-ChildItem -Path $here 
    
    #create a temporary folder using today's date
    $tempFolder = $here
    $tempFinalFolder = "$tempFolder_$Today"
    New-Item -ItemType directory -Path $tempFinalFolder -Force
    
    #decide how long back to go
    $timespan = new-timespan -days 1
    
    #move the files to a temporary location
    foreach($file in $files_to_zip)
    {
    	$fileLastModifieddate = $file.LastWriteTime
    	if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
    	{
    		Move-Item "$here\$file" -destination $tempFinalFolder
    	}
    }
    
    $CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal
    $IncludeBaseFolder = $false
    $zipTo = "{0}\Archive_{1}.zip" -f $here,$Today
    
    #add the files in the temporary location to a zip folder
    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    [System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder,$ZipTo, $CompressionToUse, $IncludeBaseFolder)
    
    #Remove-Item $tempFinalFolder -RECURSE

    I am not sure why its looking into to C:\WINDOWS\system32 .I am using  current directory in 

    $here 
    Tuesday, February 13, 2018 4:57 PM

All replies

  • You have to use the full path to all files.

    \_(ツ)_/

    Wednesday, February 14, 2018 1:56 AM
  • Hi,

    Based on my research, I'll give you the following recommendations, hope it is helpful to you:
    1. To get the current folder, you can use Get-Location cmdlet.
    2. $Today variable is not defined in the script. Maybe like this:
    $Today = (Get-Date).ToString('yyyy-MM-dd')
    3. You can use the following command to get files that has been modified for more that 1 day:
    $file.LastWriteTime -lt (Get-Date).AddDays(-1)
    4. You can use Join-Path cmdlet to combine a path and a child path into a single path.

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, February 14, 2018 9:50 AM
  • Hi,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, February 16, 2018 9:19 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 20, 2018 8:03 AM
  • You can also script zipping using the 7zip command line version.

    Tuesday, February 20, 2018 1:29 PM