繁体   English   中英

Powershell脚本:解压缩文件并在zip文件中执行bat文件

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

我需要编写一个脚本来将zip文件解压缩到唯一的文件夹中并在其中执行bat文件。

我可以使用以下代码来完成解压缩过程。 我在执行bat文件时遇到问题。 我需要以一种能够对当前所在文件夹的文件执行其操作的方式执行bat文件。

bat文件包含此代码

copy *.prn /b \\PC\Printer

我当前的Powershell脚本

$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

为什么不将批处理文件中的一行嵌入到powershell脚本中,那么您可以在powershell脚本中使用变量。

只需将cmd / c放在复制之前:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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