i am trying to write down a script which can help me to archive multiple files in multiple subdirs. Also i need to exclude specific files.
So far i got that few lines script
$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
C:\Program Files\7-zip\7z.exe" a -t7z -mx=9 -ms=on $file
}
Basically its searches recursively all subfolders for .jpeg files and gives me the list of them excluding the ones that ends with 'i'.jpeg lets say 'photoi.jpeg'.
This is working, but i cannot make it to the next step as i need to run 7zip for all listed files.
Can someone help me out here.
Thanks in advance :)
Not sure if you are trying save one big zip lots of individual ones, however I do something like this:
Set-Alias sz "C:\Program Files\7-zip\7z.exe"
$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
$output = sz a -t7z -mx=9 -ms=on "$File" 2>&1
}
You might have to modify the zipping line has I have tested it using your command line options. Another nice touch is that I have captured the output of the command for reporting purposes.
You need to pass filenames, not objects, to 7z command.
#listfile syntax can help:
Get-ChildItem ...| Where-Object ...| select -expand fullname| out-file alist.txt -encoding utf8
'C:\Program Files\7-zip\7z.exe' a archive.7z -mx=9 -ms=on `#alist.txt
Remove-item .\alist.txt
note the backtick before #
Related
I have customized one powershell code to zip files older than 7 days from a source folder to a subfolder and then delete the original files from source after zipping is complete. The code is working fine with inbuilt Compress-Archive and Remove-Item cmdlets with less volume of files, but takes more time and system memory for a large volume of files. So, I'm working on a solution using 7zip instead as it's faster.
Below script does zipping correctly but not following the condition of only files older than 7 days and deletes all the files from source folder. It should zip and delete only files older than 7 days.
I have tried all possible ways to troubleshoot but no luck. Can anybody suggest possible solution?
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$Date = Get-Date -format yyyy-MM-dd_HH-mm
$Source = "C:\Users\529817\New folder1\New folder_2\"
$Target = "C:\Users\529817\New folder1\New folder_2\ARCHIVE\"
Get-ChildItem -path $Source | sz a -mx=9 -sdel $Target\$Date.7z $Source
There are several problems here. The first is that 7-Zip doesn't accept a list of files as a pipe, furthermore even if it did your GCI is selecting every file and not selecting by date. The reason that it works at all is that you are passing the source folder as a parameter to 7-Zip.
7-Zip accepts the list of files to zip as a command line argument:
Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [#listfile]
And you can select the files you want by filter the output from GCI by LastWriteTime.
Try changing your last line to this
sz a -mx=9 -sdel $Target\$Date.7z (gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7) | select -expandproperty FullName)
If you have hundreds of files and long paths then you may run into problems with the length of the command line in which case you might do this instead:
gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7) |% { sz a -mx=9 -sdel $Target\$Date.7z $_.FullName }
Consider a temporary file with a list of those files which need to be compressed:-
$tmp = "$($(New-Guid).guid).tmp"
set-content $tmp (gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7)).FullName
sz a -mmt=8 out.7z #$tmp
Remove-Item $tmp
Also looking at the parameters to 7-Zip: -mx=9 will be slowest for potentially a small size gain. Perhaps leave that parameter out and take the default and consider adding -mmt=8 to use multiple threads.
In bash, you can
cp filea fileb filec Folder
which will copy filea fileb and filec into Folder
For some reason, i couldn't find a way to do the same thing in Powershell.
Everywhere i looked, all i could find is just the possibility to use wildcards in order to move
a bunch of files from the same type like:
cp *.txt Folder
But that's not what i'm looking for. I'm looking for a way to copy several files
by their names in one command.
Does anybody knows a way to do that?
"I'm looking for a way to copy several files
by their names in one command."
Here are three examples that can do what you want. The first one is closest to what you're looking for.
param(
$files = #('filea', 'fileb', 'filec'),
$dest = 'Folder'
)
Copy-Item -LiteralPath $files -Destination $dest -Verbose
$files | ForEach-Object { Copy-Item -Path $_ -Destination $dest -Verbose }
foreach ($file in $files) { Copy-Item -Path $file -Destination $dest -Verbose }
A one liner to do:
#("file1","file2","filen") | % {Copy -Path $_ -Destination "destination"}
I have a folder that contains several thousand files. I would like to write a Powershell script that loops through the files and copies each file whose filename contains a specific keyword. In pseudocode:
For each file in C:\[Directory]
If filename contains "Presentation" Then
copy file in C:\[Directory 2]
Simply like this ?
copy-item "C:\SourceDir\*Presentation*" "C:\DestinationDir"
or like this :
copy-item "C:\SourceDir\*" "C:\DestinationDir" -Filter "*rrrr*"
But a risk exist if you have a directory with "presentation" in his name into the source directory. Then take all method proposed here and add -file in get-childitem command.
Like in this short version of Robdy code :
gci "C:\SourceDir" -file | ? Name -like "*Presentation*" | cpi -d "C:\DestinationDir"
That code should do the trick:
$files = Get-ChildItem -Path "C:\path\to\source\folder"
$files | Where-Object Name -Like "*Presentation*" | Copy-Item -Destination "C:\path\to\destination\folder"
Of course can be written in one line but I put in two for visibility.
Edit: as Esperento57 pointed out, you might want to add -ItemType File to Get-ChildItem cmdlet to not include folders with 'Presentation' in their name. Also, depending on your needs you might also want to use -Recurse param to include files in subfolders.
If you have files in subfolders and you want to keep the path in destination folder you'll have to change the script a bit to something like:
Copy-Item -Destination $_.FullName.Replace('C:\path\to\source\folder','C:\path\to\destination\folder')
And for the above you'll have to make sure that folders are actually created (e.g. by using -Force for Copy-Item.
This seems to work:
$src = "Dir1"
$dst = "Dir2"
Get-ChildItem $src -Filter "*Presentation*" -Recurse | % {
New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
Copy-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force
}
Try something like this:
Get-ChildItem "C:\Your\Directory" -File -Filter *YourKeyWordToIsolate* |
Foreach-Object { Copy-Item $_.FullName -Destination "C:\Your\New\Directory" }
... but, of course, you'll need to fill in some of the blanks left open by your pseudocode example.
Also, that's a one-liner, but I inserted a return carriage for easier readability.
I am using a script to build VB6 files. Until now every folder was hard coded which is not the best solution, so I try to change the script to find and build .vbp file from the folder. Unfortunately it isn't even executed. The code is below. $outputfile is the logs file.
Get-ChildItem $folder -Include *.vbp -Recurse | foreach ($_) {
& vb6.exe /out $outputFile $_.FullName /make
}
My guess would be issues with the parsing the $_.fullname without enclosing in $(). Try this:
Get-ChildItem $folder -Include *.vbp -Recurse | foreach {
& vb6.exe /out $outputFile $($_.FullName) /make
}
I am looking to Recurse a Folder Structure (ex.F:\Directory\layer1\layer2\layer3) and find all of the files with the specified file extension (ex. ".dll, .txt"). Then I need to copy the listed/found files to a destination folder.
If anyone could send me a good PowerScript that will do this I would much appreciate it.
I have a drive with about 1,000 folders and 5,000 files so this will probably save me a good day or two of manual work. :)
Thank You!
Try the following, modify as needed:
$movetopath = "C:\new folder\"
New-Item -Path $movetopath -ItemType Directory -Force
$files = Get-ChildItem -Path F:\ -Recurse
$files | Where-Object {$($_.Extension -eq ".dll") -or $($_.Extension -eq ".txt")} | ForEach-Object {Copy-Item $_ -Destination $movetopath}
I suggest you use find command first
then pipe the output of find command to tar.
Untar the archive to the destination folder.
Try something like
Get-ChildItem -Path $path -Recurse | where { !$_.PSIsContainer } | where { $_.Extension -eq ".txt" -or $_.Extension -eq ".log" } | Copy-Item -Destination $dest
This is propably slower then robocopy. Also, next time please show some effort and ask a question rather then a full solution. This is not a webshop.