robocopy to update contents of folder from local to production - powershell

I want to copy some folders from my local to production. The thing is I want to update the production only when the folder structure exists. I cannot use copy paste as well since the name of the files are different, when I do copy paste I end up with 4 files: file 1, file2, file a, file b.
Local:
Directory1: file1, file2
Directory2: abc.txt, hqyh.txt
Production:
Directory1: xyz.txt, jht.txt
Directory2: abc.txt, cde.txt
Directory3: p.txt, q.txt
Directory4: t.txt, u.txt
My result:
Production:
Directory1: file1, file2
Directory2: abc.txt, hqyh.txt
Desired result:
Production:
Directory1: file1, file2
Directory2: abc.txt, hqyh.txt
Directory3: p.txt, q.txt
Directory4: t.txt, u.txt
Code:
robocopy.exe C:\Local E:\Production /MIR

robocopy can't do what you're asking (skip folders that don't exist in the source) when you run it on the parent folder(s). Run it on the child folders instead.
$src = 'C:\Local'
$dst = 'E:\Production'
Get-ChildItem $src | ForEach-Object {
robocopy $_.FullName (Join-Path $dst $_.Name) /mir
}

Related

Recursively list through directory and exclude certain files and subdirs

I am trying to recursively traverse through a directory, then ignore certain files and sub-folders before zipping the contents of that directory (preserving the directory structure).
Here is the directory structure:
Here is powershell script:
I have updated my code:
[zipParams()]
param(
[String]$dir,
[String]$Zipfile
)
function create-zip
{
[zipParams()]
param(
[String]$dir,
[String]$Zipfile
)
"$dir, $Zipfile"
echo $dir;
[String]$pathZipExe = "C:\Program Files\7-zip\7z.exe";
[Array]$arguments = "a", "-tzip", "$Zipfile", "$dir", "-xr!obj", "-xr!Forms\*.aspx.cs", "-xr!Forms\*.aspx.designer.cs", "-xr!Forms\*.aspx.resx", "-xr!UserControls\*.ascx.cs", "-xr!UserControls\*.ascx.designer.cs", "-xr!UserControls\*.ascx.resx", "-xr!Reports\*.aspx.cs", "-xr!Reports\*.aspx.resx";
& $pathZipExe $arguments;
}
create-zip -dir $dir -Zipfile $Zipfile
I am executing the powershell script with the below command.
powershell -Command "& {"C:\MyCave\iso\SDG\zipps.ps1" -dir """C:\MyCave\iso\SDG\powershellcmp""" -Zipfile """C:\MyCave\iso\SDG\powershellcmp\cmp-$(get-date -f yyyy-MM-dd)
.zip"""}"
It is zipping but it is not excluding files and certain folders (extensions highlighted in the code).
Where am I making a mistake? Please guide. This is my first try at PS scripting...
I can't find the source of your problem, but the following script should work
function my-zip
{
param(
[string]$archive,
[string]$myBase
)
$zipExe = Join-Path $env:ProgramFiles '7-zip\7z.exe'
if(-not(Test-Path $zipExe))
{
$zipExe = Join-Path ${env:ProgramFiles(x86)} '7-zip\7z.exe'
if(-not(Test-Path $zipExe))
{
Write-Host "7-Zip.exe not found" -ForegroundColor Red
throw "7-Zip.exe not found"
return 1
}
}
$zipOutput = &$zipExe a -tzip $archive $myBase\* -r '-xr!*.prod' '-xr!*.test' '-xr!*.exe'
# &$zipExe a -tzip $archive $myBase\* -r '-xr!*.prod' '-xr!*.test' '-xr!*.exe'
if ($zipOutput -contains 'Everything is OK')
{
Write-Host "Files in directory $myBase added" -ForegroundColor Green
}
else
{
Write-Host "Problem during archiving files in $myBase" -ForegroundColor Red
}
}
my-zip -archive c:\temp\test\test2019-1.zip -myBase C:\temp\test
Update:
-r (Recurse subdirectories) switch
Specifies the method of treating wildcards and filenames on the command line.
Syntax
-r[- | 0]
Switch Description
-r Enable recurse subdirectories.
-r- Disable recurse subdirectories. This option is default for all commands.
-r0 Enable recurse subdirectories only for wildcard names.
Examples
7z l archive.zip *.doc -r-
lists all *.doc files that belong to the archived root directory in the archive.zip archive.
7z a -tzip archive.zip -r src*.cpp src*.h
adds all *.cpp and *.h files from directory src and all it's subdirectories to the archive.zip archive.
Found here:
https://sevenzip.osdn.jp/chm/cmdline/switches/recurse.htm

foreach (#file in $files){ nul find "2019" $file save(D:\Export2019\$file) }

.ps1 copy files to a specific locations if specific text in file
i`m trying a code in Powershell ...
I have a lot of .xml files in S:\Export\ and i want to copy the files that contains the text "2019" in the folder S:\Export2019\
This is my code:
Start-Transcript -Path S:\Export2019\info.txt
$files = Get-ChildItem "S:\Export\"
mkdir S:\Export2019
foreach ($file in $files){
>nul find "<APPDATE>2019" $file (
echo $file was found.
Save("S:\Export2019\$file")
)
}
ii S:\Export2019 #
I have a lot of .xml files in S:\Export\ and i want to copy the files that contains the text "2019" in the folder S:\Export2019\
this is not working:
>nul find "<APPDATE>2019" $file (
echo $file was found.
Save("S:\Export2019\$file")
I'm not exactly sure if I understood your question correctly. The following script will loop through all XML-files in a specific directory and searches for the text 2019. If that text is in the file, it will be copied into another directory
Please be aware that this script is a very rough and "brute force" approach but it should give you a basis to work with
$source_dir = ".\S_Export2019" # Directory where the XML files are
$target_dir = ".\Target_Directory" # Directory where "2019" files will be copied to
# Loop through the directory $source_dir and get the fullpath of all XML-files
foreach ($file in (Get-ChildItem "$source_dir\*.xml")) {
# Save the content of the XML file
$file_content = Get-Content $file -raw
# Check if the XML file contains "2019"
if ($file_content -match "2019") {
Write-Host "$file contains '2019'"
Copy-Item $file $target_dir # Copy file to $target_dir
}
}
Edit
Thanks #LotPings for the correction - I've added the -raw Parameter to the Get-Content and also changed the if-comparison to use -match rather than the former -contains

Combine root path with several child paths

How can I combine several paths, for ex.: file1, file2, file3 with root C:?
I know that possible this:
Join-Path C:\, D:\, E:\ file
Output:
C:\file
D:\file
E:\file
I need conversely, something like:
Join-Path C:\ file1, file2, file3
And output:
C:\file1
C:\file2
C:\file3
But this doesn't work.
"file1","file2","file3" | %{ Join-Path c:\ $_ }
c:\file1
c:\file2
c:\file3

Creating a folder on a basis of filename and move the file to that folder

I have a folder with MANY files like this
Character 1 - object one.txt
Character 4 - Object two.txt
and so on.
I would like to create subfolders based on the part before " -" and move the files into that folder.
I haven't touched PS for quite a while and I am sorry to say that I am unsure where to start except for GCI..
Any help would be greatly appreciated!
#Change Current Directory to the Folder with txt files
cd C:\path\to\folder
#List all the file contents and pipe to a foreach loop
Get-ChildItem -file | % {
#Split the file name at the dash, take the first half, and trim the spaces
#($_ is the pipeline variable so in this case it represents each file)
$folder = ($_ -split "-")[0].trim()
#if the folder doesn't exist, create it
if(-not (Test-path $folder)) {
New-item $folder -ItemType Directory
}
#Move file to the folder
Move-item $_ $folder
}

Using Windows Command Line To Rename Folders

I have a group of folders two levels deep, like:
Folder A
Sub Folder A – 1
<Files>
Sub Folder A – 2
<Files>
Folder B
Sub Folder B – 1
<Files>
Folder C
Sub Folder C – 1
<Files>
Sub Folder C – 2
<Files>
Sub Folder C – 3
<Files>
I need to use Windows 7 CommandLine or Powershell to rename the sub folders so that it includes the parent folder's name in it, like
Sub Folder A - 1
would be renamed to
Folder A - Sub Folder A - 1
Possible?
Thanks
This code should do it for you.
$RootDirectory = 'c:\test';
# 1. Get list of parent folders
$ParentFolderList = Get-ChildItem -Path $RootDirectory -Directory;
# 2. Iterate over top-level folders
foreach ($ParentFolder in $ParentFolderList) {
$ChildFolderList = Get-ChildItem -Path $ParentFolder.FullName
# 3. Iterate over subfolders of top-level folders, and rename them
foreach ($ChildFolder in $ChildFolderList) {
$NewFolderName = '{0}\{1} - {2}' -f $ParentFolder.FullName, $ParentFolder.Name, $ChildFolder.Name;
Rename-Item -Path $ChildFolder.FullName -NewName $NewFolderName -WhatIf;
}
}
Remove the -WhatIf parameter from the Rename-Item cmdlet to have it make the changes.
You can run this as a bat on the command prompt
for /d %%i in (*) do (
for /d %%x in ("%%i/*") do ren "%%i/%%x" "%%i - %%x"
)