Robocopy file to random named folder - powershell

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

Related

Powershell - Copy files and rename if same name found

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.

Script to clone a template folder retaing permissions and prompt for new folder name

May I please get your insight?
I have a user group in AD call Contract Admin. I like would this user group to be able to run a script that clones a template folder (retaining permissions) to a new folder and prompt for a new client name.
The folder structure would be as follows:
Clients
New Folder (template folder) (Permissions: Admins, Contract Admin, Financial Admin)
Statements Folder (Permissions: Financial Admin)
End result after script:
Clients
ABC Company (Permissions: Admins, Contract Admin, Financial Admin)
Statements Folder (Permissions: Financial Admin)
Above is an example of the folder structure where New Folder is the template folder which also has a folder inside of it as well. I'd like to have the folders cloned with permissions retained.
I hope this is clear, I apologize if it is not. Thank you so much for your help.
The code I currently have for copying the permissions are as folows:
robocopy /e /copy:DATS "X:\Template" "X:\New Folder Name"
attrib -h "Template"
This is what I have for cloning the folders but I am lost from here.
Thank you
Solution:
#echo off
set "src=C:\Users\XXX\Desktop\template folder"
set /p "dest=Name of Client?: "
robocopy /e /copy:DATS %src% %dest%
the source can simply be the folder/file name without the full path if both the script and the folder/file to be copied is in the same directory.
/copy: Specifies the file properties to be copied. The following are the valid values for this option:
D Data A Attributes
T Time stamps S NTFS access control list (ACL)
O Owner information
U Auditing information
The default value for
CopyFlags is DAT (data, attributes, and time stamps).
But this is not a script. It's a robocopy command.
There are many switches robocopy, see its help file for them.
Specifically the mirror (/MIR) a folder tree to a new location.
As for prompting users, there are a few ways to do this. The most direct is Read-Host cmdlet provides. You simply capture that in a variable and pass that as the folder.
$FolderPath = Read-Host 'Enter a folder path.'

7zip / winrar command to extract a folder with path intact to specific folder but excluding parent source path

example
There is a file "sample.rar".
Folder structure is: "rising\dawn\ and here there are many (folders1, folders2 and file1, file2)" in this archive.
i have used following command
7z.exe x "sample.rar" "rising\dawn\*" -oi:\delete
The result is:
all files and folders in "rising\dawn\" are extracted to "i:\delete" folder but the empty parent folders "rising\dawn\" are also created in destination folder.
e.g. destination looks:
i:\delete\rising\dawn\folder1\file1.bmp
i:\delete\rising\dawn\folder2\subfolder
i:\delete\rising\dawn\file1.txt
i:\delete\rising\dawn\file2.txt
i don't want "rising\dawn\" empty folders to be created but the folder structure there onwards must be as is in the archive.
i want the result:
i:\delete\folder1\file1.bmp
i:\delete\folder2\subfolder
i:\delete\file1.txt
i:\delete\file2.txt
at last i found a way out solution. thanks to the winrar support. i have accepted it as an answer below.
if you find the question useful don't forget to click the up-vote button.
Finally this gave me the result.
Thanks to winrar support.
rar x -ep1 sample.rar rising\dawn\* d:\e\delete\
i have tried other answers given here, this is the only correct answer.
don't forget to upvote.
You can extract the archive normally and
1) move the lower level folder/files to where you would like it, then
2) remove the extra top level archive folders.
Code to do so will depend on the exact task.
Using e command instead of x and add -r option works well.
Like this:
7z.exe e -r "sample.rar" "rising\dawn\*" -oi:\delete
My executable version is "7-Zip [64] 9.20 2010-11-18",
And the platform is Windows 8.1.
This command line eliminates unnecessary parent folders and preserves the hierarchy of folders.
You need to use the e command rather than the x command:
7z.exe e "sample.rar" "scholar\update\*" -oi:\delete
Using e instead of x means 7zip will extract all matching files into the same folder (as specified via the -so switch, or the current directory if this isn't specified) rather than preserving the folder structure from inside the archive.

Batch File to Automate backup folders

So, I currently have a Drobo server that houses a Backup folder for customers that need to have their Hard drive files backed up. I create a folder for each customer in this backup folder. It's our policy to keep these files for our customers for 30 days, after which, need to be deleted. I'm wondering if its possible to make a batch file that can scan the entire Backup folder, each folder for each customer ONLY, not all the files, just the folder by modified date and if it is older than 30 days, move the entire folder that I'll label Delete for further review before deleting. I'm going to make a second batch file to delete all the folders and files once inside that delete folder, but I need the first batch file that scans just the folders' modified date in order to determine if it needs moved first. Any help would be appreciated. Thanks.
Forfiles /P C:\test\ /D -30 /c "cmd /c move #file c:/delete"
just an example of script you might use

After Robocopy, the copied Directory and Files are not visible on the destination Drive

I've been happily using robocopy for backing up my computers to an external usb drive. It's great since it only copies the files that were changed/updated/new. I can take my external drive to any machine and look at it just as if it's another drive on the computer.
I've recently purchased a 750g and another 1tb external hard drives. I ran a robocopy over the weekend that copied about 500g to my external drive. After the copy My Computer shows that ~500g has been used on the external drive. The strange thing is that when I click on the drive in Windows Explorer, nothing shows up in the right pane of Windows Explorer (and the + goes away in the left pane). I copied a single file (drag-and-drop) to this drive and it shows up in Windows Explorer. Command Prompt show the same thing. 1 file.
I know the files are on the drive as it shows up as the Free Space has been reduced.
I read that I should make sure simple file sharing is off, which it is. I also took ownership of the files as Administrator. Still nothing. It works the same on my WIndows XP machine and my Windows 7 Ultimate.
Has anyone else seen this? Or even better, does anyone know what I am doing wrong or how to solve this problem?
thanks!
Bill44077
In my case, the above didn't work.
This worked instead: attrib -h -s -a [ Drive : ][ Path ].
For example: attrib -h -s -a "C:\My hidden folder".
When copying from the root directory of a drive to a folder (non-root directory on a different drive), this can happen.
RoboCopy may set the new directory to hidden, as it copies the system attribute of the root folder of the drive over to the new folder.
You can prevent the new directory from becoming hidden by adding the /A-:SH option/flag/switch to your robocopy command.
See this Server Fault Answer to "Why does RoboCopy create a hidden system folder?
" for more information.
However, this may or may not prevent copying system attributes in other folders, according to this discussion on the Microsoft forum "ROBOCOPY hides destination Directory".
Here is an example taken from my longer, more thorough, Answer on Super User to the Question "How to preserve file attributes when one copies files in Windows?":
Robocopy D:\ C:\D_backup /A-:SH /DCOPY:T /COPYALL /E /R:0 /ZB /ETA /TEE /V /FP /XD D:\$RECYCLE.BIN /XD "D:\System Volume Information" /LOG:C:\D_backup_robocopy.LOG /MIR
However, if you already copied the directory without the /A-:SH option, running the command mentioned by Ricky above (attrib -h -s -a [ Drive : ][ Path ]) will fix the issue by unhiding the directory. Though, I found that -a was not needed.
So in my case, for the example above, attrib -h -s C:\D_backup (without the -a option) made D_backup visible.
Just ran into this issue myself, so it may be a late response and you may have worked it out already, but for those stumbling on this page here's my solution...
The problem is that for whatever reason, Robocopy has marked the directory with the System Attribute of hidden, making it invisible in the directory structure, unless you enable the viewing of system files.
The easiest way to resolve this is through the command line.
Open a command prompt and change the focus to the drive in question (e.g. x:)
Then use the command dir /A:S to display all directories with the System attribute set.
Locate your directory name and then enter the command ATTRIB -R -S x:\MyBackup /S /D where x:\ is the drive letter and MyBackup is your directory name.
The /S re-curses subfolders and /D processes folders as well.
This should clear the Read Only and System attributes on all directories and files, allowing you to view the directory normally.
In addition to the great answers SherylHohman and Ricky left I wanted to add that merely adding the /A-:SH switch for robocopy did not work and the copy created a hidden, system folder on the destination drive.
However, using the /A-:SHA parameter did work and my top level destination directory was not given the system or hidden attributes. Weirdly, my drive does not have the "a" (archived) attribute set so I am dumbfounded as to why this works at all. I do prefer simply removing these attributes to only the root destination folder after completion of the robocopy command per Ricky's suggestion so that these attributes are respected for any sub-directories. Though the /A- switch is easier to manage and (for my backup purposes) are not relevant to any directories I am backing up. You may want to consider not removing the system or hidden attributes if you're backing up your C:\ drive though.
You could try this, I say could, because the whole Windows 10 has annoying flaws everywhere, I have lost trust to Windows 10 and Microsoft.
Well I found that after I robocopied the whole Documents-folder to a root of external drive, I got a folder that is not named Documents but the Documents-folder is renamed&translated to my native language, so it could be some Language issue. (the /XD option tells robocopy to skip a folder)
C:\users\asdf\documents >robocopy . f:\ManuBackup /XD c:\Users\Asdf\Documents\OneDrive /s
File Explorer shows Tiedostot-name (=Documents in finnish) and Command Prompt shows ManuBackup-name. Also I have tried all attrib.exe commands to the ManuBackup-folder, don't trust me 100%