1

I hope some of you can help me. I got stuck modifying a powershell script.

The script checks for a zip file (file name is a fix value) in a specific folder (origin) and moves it to another folder (destination), however I need a script which checks for the .zip extension, not a fix value and moves it to another folder as well. I'm using this script at the moment:

powershell.exe -nologo -noprofile -command "& { $shell = New-Object -COM Shell.Application; $target = $shell.NameSpace('D:\Anlagen'); $zip = $shell.NameSpace('C:\Temp\Rechnungen\Outlook'); $target.CopyHere($zip.Items(), 16); }"

As you can see I need this script as a batch.file.

Mofi
  • 46,139
  • 17
  • 80
  • 143
eXquiisiTe
  • 13
  • 1
  • 4
  • Are you sure about this statement, "The script checks for a zip file (file name is a fix value) in a specific folder (origin) and moves it to another folder (destination)"? The code, to me, does not appear to be moving a zip file from a source to a destination! Have you tried running these four lines in the powershell console or ISE, `$shell = New-Object -COM Shell.Application`, `$target = $shell.NameSpace('D:\Anlagen')`, `$zip = $shell.NameSpace('C:\Temp\Rechnungen\Outlook')`, and `$target.CopyHere($zip.Items(), 16)`? If so, what happens? and does it match your description? If not, why not? – Compo Jan 15 '20 at 18:22
  • 2
    Why wouldn't you just unzip the archive to the target directory? Unzipping then moving seems wasteful. – jwdonahue Jan 15 '20 at 22:50
  • Sorry if I described my problem confusingly. The script need to do the following: The script detects one OR multiple zip-file(s) and unzips the content directly to the destination folder. The zip-file(s) have to be deleted (after unziping obv.) since they won't be used afterwards. – eXquiisiTe Jan 16 '20 at 08:43

2 Answers2

3

Use Expand-Archive to unzip the file to a directory, and then from your batch script, copy the files somewhere else. If you need to do this from a batch script:

powershell.exe -c "Expand-Archive -Path 'C:\path\to\archive.zip' -DestinationPath 'C:\unzip\directory'"
xcopy /s /e /t "C:\unzip\directory" "C:\final\destination\directory"

Note that UNC paths should also work with either command, not just local paths.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • "but you will need to first mount a drive to a UNC path with net use before you can interact with a network location using xcopy". I don't believe that is true. – jwdonahue Jan 15 '20 at 22:54
  • It's been a while since I've had to use `xcopy`, and blindly believed a colleague who told me that. I'll remove it since I can't find any information corroborating that claim. – codewario Jan 15 '20 at 23:09
  • You could use [`robocopy`](https://ss64.com/nt/robocopy.html) which surely supports UNC paths... – aschipfl Jan 15 '20 at 23:58
  • 1
    Yes, xcopy and robocopy are built on the same underlying code base. The only real differences between them are the supported command line options. Robocopy is a far more useful tool however. I would add that neither are necessary in this case. Most unzip tools allow you to specify the output path. Why unzip and then copy it? – jwdonahue Jan 16 '20 at 04:17
  • OP could want two copies of the files in the archive. Maybe they will answer on the main question. – codewario Jan 16 '20 at 04:40
  • @BendertheGreatest The script only unzips one .zip-file. As soon as there are more than one in the root folder the script aborts. – eXquiisiTe Jan 16 '20 at 08:38
1

If you have 7-Zip:

set 7zip="C:\Program Files\7-Zip\7z.exe"

IF EXIST "path\to\file.zip" (

    %7zip% x -o"path\to\new\folder" "path\to\file.zip"
)

The following line will search recursively through subfolders for any zip file. In the example below, the script will begin the recursive search at the root of C:. The path to the file will be saved as a variable, in order to be called later.

for /f "tokens=*" %%x in ('forfiles /p C:\ /m *.zip /s /c "cmd /c echo @path"') do if exist %%x (

    %7zip% x -o"path\to\new\folder" %%x
)

Another way to search recursively through multiple drive letters is to set the drive letters as variables in a FOR loop. The following example will check to see if the drive letter exists, then search the entire directory for a zip file. Example:

for %%i in (c:\, d:\, e:\, f:\, <enter as many as needed>) do if exist %%i (

    for /f "tokens=*" %%x in ('forfiles /p %%i /m *.zip /s /c "cmd /c echo @path"') do if exist %%x (

        %7zip% x -o"path\to\new\folder" %%x
    )
)
TrahanL
  • 19
  • 3
  • 7zip isn't needed to solve this problem. All Windows systems have the necessary components installed. – jwdonahue Jan 15 '20 at 22:57
  • Got it. Just to make sure I understand, it was necessary when you posted a 7-Zip solution, but it's not necessary when I post a 7-Zip solution? https://stackoverflow.com/a/53246000/10123413 – TrahanL Jan 16 '20 at 03:37
  • absolutely. Read the OP for that thread you referenced. They were asking for help with existing script that already used 7z. Yes I could have confused matters more by proposing an entirely different approach, but it wasn't necessary. – jwdonahue Jan 16 '20 at 04:15