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
Related
I'm relatively new to programming so I need some help.
I have 2 directories
C:\test1
C:\test2
So in test1 will get constantly get files.
Which look like this:
testA000_00001.txt0..txt
test00A0_00102.txt1..txt
test00A0_00102_00123.txt45..txt
...
testG000_00999.txt999..txt
testH000_00013.txt0..txt
Since its essential that the files in test1 stay the way that they are I'm gonna need them in test2.
And since test2 needs to be the current version it is needed to be done the moment the files are in test1.
But without the .txt0. - .txt999. part.
testA000_00001.txt
test00A0_00102.txt
test00A0_00102_00123.txt
...
testG000_00999.txt
testH000_00013.txt
Its also essential that these files are only copied once since they aren't gonna stay in test2 for long.
I tried it with xcopy and some other versions of copy but each time it copies the files back into test2 and after I move the files from test2 the files are copied into it again.
(sry cant comment yet)
I dont have exact code (working on it)
What i think would work is to:
Check a text file for things it alr coppied
schedual a task to run every minute or so
move to the folder you want
Use the dir command to take all files in the current directory
Copy them
Write these to a text file
Change The names
loop
When i get the code done i will edit.
Also moving them assoon as they enter the directory is very resource intensive but might be possible if the code is very efficient.
#make a folder
md "C:\test\test3"
#move to test1
cd "C:\test\test1"
# Presets
$txt = ".txt"
$files = dir
#clean up the files list
$files = $files -split "`r`n"
#for each file it found
foreach ($line in $files) {
#move the file from test1 to test 3
Copy-Item "C:\test\test1\$line" -destination "C:\Test\test3"
#take the first 14 chars from the file name
$line_name = $line.Substring(0, 14)
#add a .txt extension
$line_name = $line_name + $txt
#rename the files in test3
Rename-Item -path "C:\test\test3\$line" -NewName "$line_name"
#move the files to test 2 and if they alr exist replace(update) them
Move-Item -Path "C:\test\test3\$line_name" -Destination "C:\Test\test2" -Force
}
Remove-Item –path "C:\test\test3" –recurse -force
This Code is super close to working but i might not be able to finish today.
It only failes to move the file: test00A0_00102_00123.txt45..txt
due to the name not working. I will update the script when it works fully.
I'm new to PowerShell and have a task to copy a folder and its contents to an archive folder. The copied folder must be renamed with the date as well.
I've been working on some test folders but am having trouble figuring out how to copy the folder + its contents, let alone rename the copied folder.
I'm able to move a folder with
Copy-Item -Path C:\Test -Destination C:\Archive
But the contents don't come with. I'm also not sure how to apply to best apply
$Date = (Get-Date -UFormat "%m%d%Y")
(Get-Date -UFormat "%m%d%Y")
In order to rename the copied folder with the date.
You can create the backup folder first with the date in the name, then copy all the contents from the source, recursively.
$source = 'C:\Temp'
$backupDestination = 'C:\Backups'
# Will Create: 2019-02-20T15.43.05
$backupDate = (get-date -Format s) -replace ':', '.'
# sourceName will be set to "Temp"
$sourceName = (Get-Item $source).BaseName
# Combine the destination with the source name and the backup date
# to create C:\Backup\Temp-2019-02-20T15.43.05
$backupDirectory = Join-Path $backupDestination "$sourceName-$backupDate"
# Create the backup destination directory
New-Item -ItemType Directory -Path $backupDirectory
# Copy everything recursively
Copy-Item $source\** $backupDirectory\ -Recurse
Here's my script in powershell
$today = (Get-Date).ToString('dd_MM_yyyy')
$LocalPath = "C:\Builds\$today"
New-Item -ItemType directory -Path $LocalPath
$RemotePath = "C:\Builds\zip\$today"
$Max_hours = "-5"
#Max_mins = "-5"
$Curr_date = get-date
#Checking date and then copying file from LocalPath to RemotePath
Foreach($file in (Get-ChildItem $RemotePath))
{
if($file.LastWriteTime -gt ($Curr_date).addhours($Max_hours))
{
Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {& "C:\Program Files\7-Zip\7z.exe" "x" "-aoa" $_.fullname "-oC:\Builds\$today"}
}
ELSE
{"not extracting $file"
}
}
I've got a few *.zip files which I want to extract into a specific folder. The problem is, that 7zip creates subfolders with *.zip name and extract files into this folders.
Eg. I've got a.zip, b.zip and c.zip files and I want them to be extracted exactly in Builds folder. Right now after my command they are extracted to:
Builds/a/(here a.zip files)
Builds/b/(here b.zip files)
Builds/c/(here c.zip files)
I want them all to be in Builds/(here a,b,c files) with full directory paths.
Are there any 7zip options in shell to do that?
'-e' option exctracts all files without folders paths and thats now what I'm looking for.
If you're using PowerShell v5 you can use Expand-Archive instead of 7zip:
Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {Expand-Archive $_ -DestinationPath "C:\Builds\$today"}
EDIT:
I do not get folders created when using the command.
Zip file containing a file:
Running Expand-Archive C:\aaaa\nuget350.zip -DestinationPath C:\bbbb
What is extracted:
I have 3 folders, /Incoming, /Processed and /Temp. The incoming folder is updated with new files hourly and currently has 120k+ individual .zip files in it. Every hour these files are copied to the processed folder. where they are unzipped and the records are inserted into a SQL table. The table is Dropped and Recreated every hour and all files are re-imported. This process is starting to take a long time.
All the file transfers are currently being done in a cmd batch file, using robocopy /MIR and a SQL .dtsx file for importing.
I am trying to find a method to compare the incoming folder with the processed folder before new files are copied every hour, and copy the differences to the temp folder so only they are added to SQL instead of dropping and recreating every hour.
Any help would be awesome as I have spent hours on this single issue with no luck.
This solution will compare two folders (e.g. Incoming and Processed) and copy the new files in the first folder (Incoming) to the third folder (Temp) for processing.
$Folder1 = (Get-ChildItem -Recurse -path "C:\Incoming")
$Folder2 = (Get-ChildItem -Recurse -path "C:\Processed")
(Diff $Folder1 $Folder2 | ? {$_.SideIndicator -eq "<="}).InputObject |
ForEach-Object {
$ItemName1 = $_;
$ItemName2 = "C:\Temp\" + $ItemName1;
Copy-Item $ItemName2 -Destination "C:\Temp" -Force
}
you can directly use fc command
ex : fc srcfile destfile >logfile
or use ROBOCOPY
I have about 11000 different files in hundreds different folders, sub folders and sub-sub folders in following location \\mastercorrespondence, and I need to rename some of the files and copy corresponding file from K:\CDS_TOOL_MANUAL_OVERRIDES daily in their own subfolder.
In short it should perform following steps
Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.
For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondence” any sub-directory.
If corresponding file found then rename file in \\mastercorrespondence sub-directory as <Original Filename>_<Overwritten>_<dd.mm.yy>
Move the file from K:\ CDS_TOOL_MANUAL_OVERRIDES folder to the same location as it counterpart in the \\10.5.13.10\mastercorrespondence sub-directory.
If any documents did not have a corresponding file in \\mastercorrespondence sub-directory then write a message to log file stating names of unmatched files.
Folder Structure is Like.
\\mastercorrespondence\SIPP\21\201201\01
\\mastercorrespondence\SIPP\21\2012022
\\mastercorrespondence\ISA\10201201\201202\02
\\mastercorrespondence\ISA\10201201\201203
\\mastercorrespondence\ISA\10201201\201204
\\mastercorrespondence\ISA\10201201\201205
Here's a starting point in PowerShell, if that works for you:
#Look for any PDF format documents in K:\ CDS_TOOL_MANUAL_OVERRIDES folder.
$pdfFiles = Get-ChildItem -Path K:\CDS_TOOL_MANUAL_OVERRIDES -filter "*.pdf"
#For each document in K:\ CDS_TOOL_MANUAL_OVERRIDES look for PDF document with identical file name held in the \\mastercorrespondence” any sub-directory.
$referenceFiles = Get-ChildItem -Path \\mastercorrespondence -recurse -filter "*.pdf"
$pdfFiles | %{
$_ = $pdfFile
$matched = ""
#I don't fully understand what you're asking for, but it seems to me that the move and rename are on the same file, and technically a rename is a move... so I've combined them.
$referenceFiles | %{if ($_.Name -eq $pdfFile.Name) {$matched = $_} }
if ($matched -ne "") {
$destinationName = ($pdfFile.Name + "_OverWritten_"+(Get-Date -format dd.MM.yy))
Move-Item -Path $_.FullName -Destination ($matched.DirectoryName + "\" + $destinationName)
}
else{
#If any documents did not have a corresponding file in \\mastercorrespondence sub-directory then write a message to log file stating names of unmatched files.
("Unable to locate matching file: " + $pdfFile.FullName) | Out-File -Append -FilePath "K:\mover.log"
}
}