How can I use robocopy to exclude files with multiple periods using wildcards? - powershell

I have multiple config files that I need to copy to a destination directory. Some of these files have multiple extensions like the following:
web.config.env1
web.config.env2
web.config.something
I also have files that just simply end in ".config". I'd like to use Robocopy to copy all files in a directory plus the ".config" files while excluding any ".config.*" files.
So I tried the following in powershell:
robocopy $SourceDir $DestinationDir "*.*" /L /XF "*.config.*"
However, if I try to exclude anything matching "*.config.*" it also excludes all configuration files that end with only with "*.config". How should I go about excluding the config files with additional extensions while copying all others?

Related

Robocopy deletes existing files in destination folder

I am new to Powershell and Robocopy usage.
I am trying to send some files to destination path. But destination path contains some files that is not contained in source. I want that files remain intact after copy operation.
When i try to run Robocopy from a powershell script like this:
Robocopy sourcePath destinationPath /MIR
it syncs two paths. So that destinationPath lonely files are deleted.
Is there any way to prevent this behavior?
Don't use the /MIR flag. That is exactly to keep the source and destination in sync, including deletion of files missing in source.
Use robocopy /E to copy a folder structure including empty subfolders. By default it only copies new and newer files.

How to copy and overwrite exiting folder and files inside it if exist using power script [duplicate]

I have a set of projects that involve a mix of project-specific files plus common files. I'm trying to copy contents from two different folders -- a project-specific folder, and a common folder -- into a single folder named for the project. I also want to retain any folder hierarchies from the original folders.
For example, some paths to the common files:
src\Common\PackageAssets\logo1.jpg
src\Common\PackageAssets\logo2.jpg
And example paths to project-specific files:
src\Projects\ProjectA\PackageFiles\readme.txt
src\Projects\ProjectA\PackageFiles\scale-100\projA.png
The desired result after copying would be:
bld\ProjectA\pkgFiles\logo1.png
bld\ProjectA\pkgFiles\logo2.png
bld\ProjectA\pkgFiles\readme.txt
bld\ProjectA\pkgFiles\scale-100\projA.png
What I'm using is this:
[string]$pkgContentPath = "bld\$project\pkgFiles"
# copy common files
Copy-Item -Path .\src\Common\PackageAssets -Recurse -Destination $pkgContentPath
# copy project-specific files
Copy-Item -Path .\src\Projects\ProjectA\PackageFiles\ -Recurse -Destination $pkgContentPath
But instead of the expected results, all the files are ending up in an extra level of subfolder:
bld\ProjectA\pkgFiles\PackageAssets\logo1.png
bld\ProjectA\pkgFiles\PackageAssets\logo2.png
bld\ProjectA\pkgFiles\PackageFiles\readme.txt
bld\ProjectA\pkgFiles\PackageFiles\scale-100\projA.png
I'm stumped. I can't figure out how to get rid of the extra subfolder layer. I tried using Get-ChildItem piping to Copy-Item, but then the subfolder hierarchies were lost.
In a .bat file, this works:
xcopy src\Common\PackageAssets\* bld\%project\pkg_contents /s
xcopy src\Projects\%project\PackageFiles bld\%project\pkg_contents /s
I guess I could use xcopy, but surely there must be a way to do this using cmdlets.
The behavior you describe is a known problem as of Windows PowerShell v5.1 / PowerShell Core v6.2.0-preview.2, unfortunately:
In short, the behavior of Copy-Item regrettably depends on whether the target directory happens to exist already or not:
If the target dir. exists, it is not the source directory's content that is copied, but the source dir. as a whole, to a subdirectory of the target directory named for the source dir.
The workaround is already mostly spelled out in PetSerAl's helpful comment on the question:
Before copying, make sure that the target directory exists.
Append \* to the source path to explicitly target the contents of the source dir.
Also specify -Force, so as to ensure that hidden files and directories are also copied.
$pkgContentPath = "bld\$project\pkg_contents"
# Make sure the target dir. exists.
# (-Force leaves an existing dir alone and otherwise creates it.)
New-Item -Force -Type Directory $pkgContentPath
# IF desired, clear out the pre-existing directory content.
# !! Beware of Remove-Item's intermittent failures - see below.
# Remove-Item -Force $pkgContentPath\* -Recurse.
# Copy with \* appended to the source paths and -Force added:
#
# copy common files
Copy-Item -Recurse -Force -Path .\src\Common\PackageAssets\* $pkgContentPath
# copy project-specific files
Copy-Item -Recurse -Force -Path .\src\Projects\ProjectA\PackageFiles\* $pkgContentPath
A note re use of Remove-Item -Recurse to clear out preexisting destination-directory content, if needed: Regrettably, Remove-Item can fail on occasion and you cannot predict when that happens - see this answer for a robust alternative.

Delete file if it exists anywhere on the windows Device

I Am trying to delete a file which exists in like many folders which are dynamically created and I have no clue how and where to start. What would be the best approach using scripting to delete this file ?
del has a /s switch to process all subfolders:
del /s c:\googledrivesync.exe
it may take a while to scan the folder tree. Get yourself a cup of coffee...
Assuming you want to delete specific files in a directory structure (as opposed to just deleting everything), from PowerShell, you could do
dir -recurse -file -filter nameOfFileToRemove rootDirectory | remove-item
This will recurse through the file tree, looking for files matching nameOfFileToRemove and then removing them. Also nameOfFileToRemove may contain wildcard characters like "nameOfFile*.txt"

Robocopy: not deleting folders containing excluded files

I want to mirror two folders, but to exclude certain file types (e.g. js files). I can do that with /XF option.
The problem I run into is that if there is a folder Foo with js file in Destination folder, but there is no Foo folder in Source folder - robocopy command bellow deletes the Foo folder on the destination side together with the js files in it.
Robocopy.exe d:\Source d:\Destination /E /PURGE /XF *.js /COPYALL /FFT
What I would like is to exclude from deliting folders that contain excluded files.
You can't. If you apply /PURGE it will delete, it will delete any file/folder that doesn't exist in your source folder.
If you need to maintain that folder, you need to create that folder in the source and remove the /PURGE switch.
Robocopy.exe d:\Source d:\Destination /E /XF *.js /COPYALL /FFT

How to copy files from folder tree dropping all the folders with Robocopy?

I have the following folder structure:
FolderA
--Folder1
--Folder2
--Folder3
...
--Folder99
Folders 1 through 99 have files in them.
All I want to do is to copy ALL THE FILES into ONE FOLDER, basically do a FolderA copy, and wipe out Folders 1-99 keeping all the files.
I'd like to do it with Robocopy from cmd.exe if possible (Windows Server 2008)
Why use robocopy? It's a good tool for a specific task but this is not the one.
You can simply use what cmd already gives you:
for /r %f in (*) do #copy "%f" target
This will essentially "flatten" your directory hierarchy. for /r will walk a directory tree recursively, looking for file names matching the given pattern. You can also specify the directory to start in:
for /r FolderA %f in (*) do #copy "%f" target
Within the loop it's just a simply copy of the file into a specified folder.
Robocopy is a great tool... when you have a job it can handle. Why not use xcopy?
If you have two drives you can just use xcopy:
XCOPY C:\*.* D:\NewFolder\ /S
Or use XXCOPY for one drive:
XXCOPY C:\*.* C:\NewFolder\ /S /CCY
XXCOPY
Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest