简体   繁体   中英

Powershell Script : Unzip files and execute bat file within the zip files

I need to write a script to unzip zip files into unique folders and execute a bat file within them.

I am able to get through the unzipping process with the below code. I having a problem with executing the bat file. I need to execute the bat file in a way that it is able to perform its operation on the files of the folder it is presently in.

The bat file contain this code

copy *.prn /b \\PC\Printer

My Current Powershell Script

$shell=new-object -com shell.application

$CurrentLocation=get-location
$CurrentPath=$CurrentLocation.path
$Location=$shell.namespace($CurrentPath)

# Find all the Zip files and Count them
$ZipFiles = get-childitem *.zip
$ZipFiles.count | out-default

# Set the Index for unique folders
$Index = 1

# For every zip file in the folder
foreach ($ZipFile in $ZipFiles)
{

    # Get the full path to the zip file
    $ZipFile.fullname | out-default

    # Set the location and create a new folder to unzip the files into - Edit the line below to change the location to save files to
    $NewLocation = "E:\BCode\$Index"
    $A = "E:\BCode\$Index"
    New-Item $NewLocation -type Directory

    # Move the zip file to the new folder so that you know which was the original file (can be changed to Copy-Item if needed)
    Move-Item $ZipFile.fullname $NewLocation

    # List up all of the zip files in the new folder 
    $NewZipFile = get-childitem $NewLocation *.zip

    # Get the COMObjects required for the unzip process
    $NewLocation = $shell.namespace($NewLocation)
    $ZipFolder = $shell.namespace($NewZipFile.fullname)

    # Copy the files to the new Folder
    $NewLocation.copyhere($ZipFolder.items())

#NOT WORKING
#ITS NOT SENDING FILES TO THE PRINTER
#PRINTER NAME & PC NAME ARE PREFECT
    $PC = "$A\*.prn" + ' /b //Samsung-2012/Zebra'

    cmd \c COPY $PC

    # Increase the Index to ensure that unique folders are made
    $Index = $Index + 1

} 


$destination = 'E:\BCode'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse

Why not embed the one line from the batch file in the powershell script, then you can use the variables in your powershell script.

Simply put cmd /c before the copy:

cmd /c copy "$NewLocation\*.prn" /b \\PC\Printer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM