Powershell - Copy files and rename if same name found - powershell

I am a bit confused on how to achieve this. Any input would be great.
I have a source dir and a target directory. During copy, if the file exists with same last modified stamp, then don't bother doing anything to save CPU resources.
If the source dir has a file that exists in target but the source version has a newer modified date, then the existing file in target directory should be renamed to file_yyyymmddhhmmss (the old modified date)
This way the new file can get in.
I got the basic copy here but struggling how can i amend this?
#Set source directory
$sourcedir="D:\Temp\2023\Source"
#Set target directory
$destinationdir="D:\Temp\2023\Destination"
#Set logfile - Add YYMMDDHHMMSS to log
$logfile="D:\Temp\2023\Log.txt"
robocopy $sourcedir $destinationdir /e /xo /log:$Logfile
If you think this can be achieved in Batch script, then fine. Otherwise Powershell is first preference.

Related

Robocopy file to random named folder

I'm creating a script to automatically copy and paste bookmarks to x amount of computers.
Robocopy the Mozilla Bookmarks which are stored in a file called places.sqlite on a drive. To the destination
Appdata\Roaming\Mozilla\Firefox\Profiles\\*RANDOM PROFILE NAME\places.sqlite
I can't find a solution to copy this file to the destination due to every windows profile and computer gets a random profile name created. I tried with a star * as you can see in the code because I thought a star choices the newest folder (last changed).
The code is:
$env:homeshare
$env:username
Robocopy "$env:homeshare\ComputerChange\" "C:\Users\$env:username\AppData\Roaming\Mozilla\Firefox\Profiles\*\" "places.sqlite" /COPY:DAT
How do I edit the code so it chooses the random named folder? Is it possible to copy the places.sqlite to ALL folder in the Profiles\ folder?
Appreciate the help

Robocopy not excluding changed files

I am copying a full directory over and am trying to exclude changed files. Note that I am doing this in Powershell.
robocopy "D:\Projects\Publishes\css" "D:\Projects\robocopy test\css" /xc
However my changed files at destination are being over-written. I can't use exclude older or newer files by the nature of my situation so these are out.
NOTE:
On the MSDN page it says /xct to exclude changed files but if I do this I get the following error:
ERROR : Invalid Parameter #3 : "/xct"
From other examples I've seen /xc so that is what I'm using here.
I translate your requirement as:
Some files on the destination directory have been changed. Don't replace those.
I think what you are looking for is the /XO flag
robocopy "D:\Projects\Publishes\css" "D:\Projects\robocopy test\css" /xo /e
This translates to
Do not overwrite newer files on the destination directory
Your /xn flag actually means
Do not copy newer files from the source directory
Sightly different.

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e

How to exclude subdirectories in the destination while using /mir /xd switch in robocopy

There is a script running which mirrors a bunch of folders from one volume to another. The problem is that now there are going to be subdirectories within those folders at the destination which are not part of the original mirroring script. They are standalone subdirectories and I don't want them purged once the mirroring kicks in. Is there a way for me to use the /xd switch in robocopy wherein I'd be able to exclude the destination subdirectories.
Example:
robocopy "\\hq04t2fis202\archive\dr" "\\hq04t3fis202\archive\dr" /mir /xd "\\hq04t3fis202\archive\dr\*\hq04s2dba301"
In the above example, there are several directories under \\hq04t2fis202\archive\dr that are being mirrored. And at the destination, once these directories are mirrored from the source, there is another script which dumps separate subdirectories within each of those directories. So what I want to do is somehow use the /xd switch to avoid purging those subdirectories at the destination.
Also, in the above example, in the /xd switch, I'm using the "*" wildcard to mean that I want to include all the directories that fall under the "\\hq04t3fis202\archive\dr" root folder at the destination.
Need help and suggestions as to whether this is possible, and if it is then how can I do it. So far I've tried and tested many other switches like /xo, /xx, but none of them solve my purpose.
Also, /xx would work except that now it won't delete any folders at the destination at all and it wouldn't be mirroring.
Hopefully I'm not overly confusing everyone here. Let me know if you have any questions.
When i tried the solution with /XD i found, that the path to exclude should be the source path - not the destination.
e.g. this Works
robocopy c:\test\a c:\test\b /MIR /XD c:\test\a\leavethisdiralone\
Rather than creating empty directories in source to exclude, you can supply the full destination path to the /XD switch to have the destination directories untouched
robocopy "%SOURCE_PATH%" "%DEST_PATH%" /MIR /XD "%DEST_PATH%"\hq04s2dba301
The issue is that even though we add a folder to skip list it will be deleted if it does not exist.
The solution is to add both the destination and the source folder with full path.
I will try to explain the different scenarios and what happens below, based on my experience.
Starting folder structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR
This will copy over all the files and folders that are missing and deletes all the files and folders that cannot be found in the source
Let's add a new folder and then add it to the command to skip it.
New structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\source\3"
If I add /XD with the source folder and run the command it all seems good the command it wont copy it over.
Now add a folder to the destination to get this setup:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
d:\Temp\dest\1.txt
d:\Temp\dest\2\2.txt
d:\Temp\dest\3\4.txt
If I run the command it is still fine, 4.txt stays there 3.txt is not copied over. All is fine.
But, if I delete the source folder "d:\Temp\source\3" then the destination folder and the file are deleted even though it is on the skip list
1 D:\Temp\source\
*EXTRA Dir -1 D:\Temp\dest\3\
*EXTRA File 4 4.txt
1 D:\Temp\source\2\
If I change the command to skip the destination folder instead then the folder is not deleted, when the folder is missing from the source.
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\dest\3"
On the other hand if the folder exists and there are files it will copy them over and delete them:
1 D:\Temp\source\3\
*EXTRA File 4 4.txt
100% New File 4 3.txt
To make sure the folder is always skipped and no files are copied over even if the source or destination folder is missing we have to add both to the skip list:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "d:\Temp\source\3" "D:\Temp\dest\3"
After this no matters if the source folder is missing or the destination folder is missing, robocopy will leave it as it is.
The way you can exclude a destination directory while using the /mir is by making sure the destination directory also exists on the source. I went into my source drive and created blank directories with the same name as on the destination, and then added that directory name to the /xd. It successfully mirrored everything while excluding the directory on the source, thereby leaving the directory on the destination intact.
The argument order seems to matter... to exclude subdirectories, I used
robocopy \\source\folder C:\destinationfolder /XD * /MIR
...and that works for me (Windows 10 copy from Windows Server 2016)
Try my way :
robocopy.exe "Desktop\Test folder 1" "Desktop\Test folder 2" /XD "C:\Users\Steve\Desktop\Test folder 2\XXX dont touch" /MIR
Had to put /XD before /MIR while including the full Destination Source directly after /XD.

xcopy file, rename, suppress "Does xxx specify a file name..." message

This seems pretty simple and maybe I'm just overlooking the proper flag, but how would I, in one command, copy a file from one directory to another and rename it in the destination directory? Here's my command:
if exist "bin\development\whee.config.example"
if not exist "TestConnectionExternal\bin\Debug\whee.config"
xcopy "bin\development\whee.config.example"
"TestConnectionExternal\bin\Debug\whee.config"
It prompts me with the following every time:
Does TestConnectionExternal\bin\Debug\whee.config specify a file name
or directory name on the target (F = file, D = directory)?
I want to suppress this prompt; the answer is always F.
I use
echo f | xcopy /f /y srcfile destfile
to get around it.
Don't use the xcopy, use copy instead, it doesn't have this issue.
xcopy is generally used when performing recursive copies of multiple files/folders, or when you need the verification/prompting features it offers. For single file copies, the copy command works just fine.
Another option is to use a destination wildcard. Note that this only works if the source and destination filenames will be the same, so while this doesn't solve the OP's specific example, I thought it was worth sharing.
For example:
xcopy /y "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\*"
will create a copy of the file "whee.config.example" in the destination directory without prompting for file or directory.
Update: As mentioned by #chapluck:
You can change "* " to "[newFileName].*". It persists file extension but allows to rename. Or more hacky: "[newFileName].[newExt]*" to change extension
There is some sort of undocumented feature in XCOPY. you can use:
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config*"
i tested it just today. :-)
Just go to http://technet.microsoft.com/en-us/library/bb491035.aspx
Here's what the MAIN ISSUE is "... If Destination does not contain an existing directory and does not end with a backslash (), the following message appears: ...
Does destination specify a file name
or directory name on the target
(F = file, D = directory)?
You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.
Took me a while, but all it takes is RTFM.
So, there is a simple fix for this. It is admittedly awkward, but it works.
xcopy will not prompt to find out if the destination is a directory or file IF the new file(filename) already exists. If you precede your xcopy command with a simple echo to the new filename, it will overwrite the empty file. Example
echo.>newfile.txt
xcopy oldfile.txt newfile.txt /Y
I met same issue when try to copy file with new name only if file does not exist in destination or exist (with new name), but is older. The solution is to add * char at end of destination file name. Example:
xcopy "C:\src\whee.config.txt" "C:\dest\bee.config.txt*" /D /Y
This is from Bills answer.
Just to be really clear for others.
If you are copying ONE file from one place to another AND you want the full directory structure to be created, use the following command:
xcopy "C:\Data\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg" "C:\Target Data\\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg\"
Yes, put a backslash at the end of the file name and it will NOT ask you if it's a file or directory. Because there is only ONE file in the source, it will assume it's a file.
xcopy src dest /I
REM This assumes dest is a folder and will create it, if it doesnt exists
XCOPY with * at the end of the target to copy files whether they exist or not in destination
XCOPY with \ at the end of the target to copy folders and contents whether exist or not in destination
Alternatively
RoboForm SOURCE DEST FILE for files
RoboForm SOURCE DEST for folders
I had a similar issue and both robocopy and xcopy did not help, as I wanted to suppress the comments and use a different destination filename. I found
type filename.txt > destfolder\destfilename.txt
working as per my requirements.
Back to the original question:
xcopy "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\whee.config"
could be done with two commands eg:
mkdir "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"
By simply appending "\.." to the path of the destination file the destination directory is created if it not already exists. In this case
"c:\mybackup\TestConnectionExternal\bin\Debug\"
which is the parent directory of
the non-existing directory
"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
At least for WIN7 mkdir does not care if the directory
"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"
really exists.
The right thing to do if you wanna copy just file and change it's name at destination is :
xcopy /f /y "bin\development\example.exe"
"TestConnectionExternal\bin\Debug\NewName.exe*"
And it's Gonna work fine
I suggest robocopy instead of copy or xcopy. Used as command or in GUI on clients or servers. Tolerant of network pauses and you can choose to ignore file attributes when copying of copy by file attributes. Oh, and it supports multi-core machines so files are copied much faster in "parallel" with each other instead of sequentially. robocopy can be found on MS TechNet.
For duplicating large files, xopy with /J switch is a good choice. In this case, simply pipe an F for file or a D for directory. Also, you can save jobs in an array for future references. For example:
$MyScriptBlock = {
Param ($SOURCE, $DESTINATION)
'F' | XCOPY $SOURCE $DESTINATION /J/Y
#DESTINATION IS FILE, COPY WITHOUT PROMPT IN DIRECT BUFFER MODE
}
JOBS +=START-JOB -SCRIPTBLOCK $MyScriptBlock -ARGUMENTLIST $SOURCE,$DESTIBNATION
$JOBS | WAIT-JOB | REMOVE-JOB
Thanks to Chand with a bit modifications:
https://stackoverflow.com/users/3705330/chand
Place an asterisk(*) at the end of the destination path to skip the dispute of D and F.
Example:
xcopy "compressedOutput.xml" "../../Execute
Scripts/APIAutomation/Libraries/rerunlastfailedbuild.xml*"
Use copy instead of xcopy when copying files.
e.g.
copy "bin\development\whee.config.example"
"TestConnectionExternal\bin\Debug\whee.config"
Work Around, use ReName... and Name it some Cryptic Name, then ReName it to its Proper Name
C:
CD "C:\Users\Public\Documents\My Web Sites\AngelFire~Zoe\"
XCopy /D /I /V /Y "C:\Users\Public\Documents\My Web Sites\HostGator ~ ZoeBeans\cop.htm"
Ren "cop.htm" "christ-our-passover.htm"
xcopy will allow you to copy a single file into a specifed folder it just wont allow you to define a destination name. If you require the destination name just rename it before you copy it.
ren "bin\development\whee.config.example" whee.config
xcopy /R/Y "bin\development\whee.config"
"TestConnectionExternal\bin\Debug\"
When working with single files , I use both commands.
To copy a file to another existing directory, use copy
copy srcPath\srcFile existingDir\newFile
To copy an existing file to and create new directories, use xcopy
xcopy srcPath\srcFile newDirectoryPath\newFile
To suppress the xcopy 'file or directory' prompt, echo in the response. So for a file copy echo in f.
echo f | xcopy srcPath\srcFile newDirectoryPath\newFile
Note flag /y works in both commands to suppress the confirmation to overwrite the existing destination file.
MS Docs: copy, xcopy
Since you're not actually changing the filename, you can take out the filename from the destination and there will be no questions.
xcopy bin\development\whee.config.example TestConnectionExternal\bin\Debug\ /Y
This approach works well when the destination directory is guaranteed to exist, and when the source may equally be a file or directory.
You cannot specify that it's always a file. If you don't need xcopy's other features, why not just use regular copy?
Does xxxxxxxxxxxx specify a file name
or directory name on the target
(F = file, D = directory)? D
if a File : (echo F)
if a Directory (echo D)