Copy file to Firefox user profile with batch? - batch-file

I am trying to create an active setup to copy files to all users profile after application launch.
Using this command, I am unable to copy file(s) to user profile
The problem is that although no one uses profiles, the "Default" Profile have a random prefix name (24xwe234.default \ 45qw324w.default).
xcopy "C:\Temp\123.cfg" C:\users\%username%\AppData\Roaming\Mozilla\Firefox\Profiles\*.default" /d /y
How can I copy the file to the ****.default/ Folder via batch script?

If you have Administrative privileges (in order to read/write other users %HOMEPATH%), then everything below could be applied to other users, else it's limited to your own user only.
Although xcopy is a pretty advanced command, I don't think it can handle wildcards at destination in this scenario (also didn't you miss an "*" after ".default"?).
One alternative would be to use a .bat script that iterates (using [SS64]: FOR /F (or [MS.Docs]: for)) over the .default* like dirs (corresponding to Firefox profiles - not an expert on this area, could there be more than 1?) and copies the file (using good old [MS.Docs]: copy) in each of them (if any).
code.bat:
#echo off
setlocal enableextensions
set _SOURCE_FILE="C:\Temp\123.cfg"
set _TARGET_USER=%USERNAME%
set _FIREFOX_PROFILES_PATH="C:\Users\%_TARGET_USER%\AppData\Roaming\Mozilla\Firefox\Profiles"
for /f %%f in ('dir /b "%_FIREFOX_PROFILES_PATH:"=%\*.default*"') do (
echo Copying %_SOURCE_FILE% to "%_FIREFOX_PROFILES_PATH:"=%\%%f"
copy /y %_SOURCE_FILE% "%_FIREFOX_PROFILES_PATH:"=%\%%f"
)
As it is now, the script copies the file in the default Firefox dir(s) for current user only.
Again, with Administrative privileges, it can be extended to iterate all existing users, and do the same thing for each of them (well, it could be extended anyway, but it will still do only what's doing now, when run by a regular user).
But anyway, the question (or, at least the step that posed problems) is about the current user's .default profile.

Related

using registry key for jumplabel in batch errorlevel problem

I'm facing the following problem: In Windows AD every user logging in at a machine for the first time, shall get copied some program links on his desktop. Therefore desktop-pc and notebook are member of the AD and the desktops use serverbased-profiles while notebook use local profiles, it is necessary to differ the runonce-script.
My plan/script was the following one:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders" /v "Desktop" /f "R:\Profile\Desktop"
if %ERRORLEVEL% EQU 0 goto Domain
if %ERRORLEVEL% EQU 1 goto Notebook
:Domain
ECHO off
xcopy "c:\UserApps\Desktop-Verknuepfungen\*.lnk" "R:\Profile\Desktop\*.lnk" /Y > nul
exit
:Notebook
ECHO off
xcopy "c:\UserApps\Desktop-Verknuepfungen\*.lnk" "%USERPROFILE%\Desktop\*.lnk" /Y > nul
exit
The shell-folder desktop string should be examined if it's the ad-path or the local path to decide: desktop or notebook.
Unfortunately in every case, although it is a notebook, the errorlevel 0 returns and the script uses the wrong jumplabel. Normally it should return for a local path errorlevel 1.
Maybe someone can help out?
Thank you so much in advice,
Chris
here you have a solution that should solve your problem.
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\explorer\Shell Folders" /v "Desktop" | find /i "R:\Profile\Desktop" && goto Domain
:Notebook
ECHO off
xcopy "c:\UserApps\Desktop-Verknuepfungen\*.lnk" "%USERPROFILE%\Desktop\*.lnk" /Y > nul
exit
:Domain
ECHO off
xcopy "c:\UserApps\Desktop-Verknuepfungen\*.lnk" "R:\Profile\Desktop\*.lnk" /Y > nul
exit
If the registry key is existent its content will be sent to find. Then find /i will search within the output without case sensitivity. if the string R:\Profile\Desktop is found the success can be used with && to trigger the goto Domain. Just had to switch :Notebook and :Domain so that in error (searchstring not found) he will choose :Notebook.
Andi
With the following builtin alternative you might solve this problem as well, but it doesn't need batch files. Microsoft tries to remove the need for that kind of skripts
Use GPP (Group Policy Preferences) with WMI filtering and let that do the copy job. You can define paths where files are and where they should be copied to. If you just need a fixed source / target definition or can use environmental variables that should be a good alternative.
Microsofts details about that:
Files extension:
Group Policy includes the Files preference extension.
For computers or users, this extension allows you to:
Copy a file (or multiple files in one folder) to a new location and then configure the attributes of those files. New subfolders are created as necessary.
Delete a file (or multiple files in one folder) and replace it with a copy of a file from a source folder.
Modify the attributes of a file (or multiple files in one folder).
Delete a file (or multiple files in one folder).
Modify the attributes of, replace, or delete all files with a
particular extension in one folder.
Modify the attributes of, replace, or delete all files in a particular folder.
Note:
To configure folders rather than individual files, use the Folder
extension.
You can create and configure File preference items for any
domain-based Group Policy object (GPO). You configure the settings by editing a GPO using the Group Policy Management Console. When editing a GPO, you can find this preference extension at the following location:
Computer Configuration or User Configuration
** └ Preferences
****  └ Windows Settings
******  └ Files
Source: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/dn789188(v=ws.11)#files-extension
You have a lot more options there including some good filtering options.

Is there a way to have a .bat script copy files and folders from one drive to another?

I am trying to copy a file and folder from on my flash drive to the desktop of any computer I plug it into. I am somewhat familiar with the command line and batch scripts but have not used it for doing file manipulation in this way.
I originally wrote a script to test copy it from my flash drive to my desktop, however, the issue is on a new computer I won't always have the same drive letter assigned.
The code I used to test copy everything with known drive letters where the script starts in the folder on my flash drive where what I want to copy is:
copy aFile.file C:\Users\*user*\Desktop
where user is my user folder.
The main issue I am running into right now is copying things from one drive to another where it needs to go into a user profile
cd has the /D switch which specifically specifies to switch drives.
so
cd /D %userprofile%\Desktop
goes to the active user's Desktop... right where I want it.
whereas
cd %userprofile%\Desktop
does nothing because users are on a separate drive.
Is there something similar to /D for copy? The main issue is that drives letters change and user names change.
User name changes are easy to deal with because I can use %userprofile\Desktop.
If we assumed that copy made use of the same switches as cd (it doesn't) then what I basically want is:
copy /D aFile.file %userprofile%\Desktop
I am sure I am missing something relatively simple but I didn't see any switches that seemed to be what I need.
I am also open to using robocopy if that would be easier, but attributes (timestamp, owner, etc) don't matter to me so I didn't think it would benefit me immediately.
batch scripts may not be compatible with cd
try to make a script that asks two inputs.
#echo off
set /p var1="this is the file that will be moved"
set /p var2="this is the destination"
xcopy %var1% %var2%
echo copied!
pause >nul
this will ask for two inputs,first input asks for the file,second one is the file copy to

Batch file to remove copied shortcuts from public desktop

I currently use this code to copy shortcuts from a folder on a server to C:\Users\Desktop:
if not exist "%1" md "%1"
copy /y "%~dp0PlaceShortcutsHere\*.*" "%1"
This copies any shortcuts I place in the folder to the desktop.
I now need a way to remove these, baring in mind that the shortcuts in the source folder can and will change over time.
Is there a way to compare the shortcuts in the desktop and on the server and only delete the ones that are present in both folders, and only from the desktop of the computer?
These shortcuts are not all of the shortcuts on the desktop of the machines, there are others as well, hence wanting to only delete the ones present in both locations. I will also need this to be adaptive as the shortcuts present on the server will be added to or removed as needed.
This is to be deployed out through SCCM 2007/12 but I want to test it locally first.
And yes, using a GP would be easier but the GP we use has stopped working so I need a backup way of deploying shortcuts.
Took some time but I've found an answer myself. Posting this if it is useful to anyone in the future.
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Breaking it down:
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
The above created a text file with the names of the shortcuts in it for referencing by the next line.
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
The above maps the server/share that the files are in temporarily, then uses the "For /F" loop to get the file names and then delete them.
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Finally, this line deleted the text file that was created.
This is useful for when the contents of the folder is always changing, it will create an up to date list of the files and then delete the list afterwards, preventing confusion.
%date:~-4,4%%date:~-7,2%%date:~-10,2%
Lastly, this little line will input the current date into the filename of the text documet, so should the final delete line not work you can see the date on it. At a guess I would also think that putting it in paths will make the script only choose the files from the current day, not any others, but I have not tested this.
Please correct me if I'm wrong on anything.

Copy file and delete duplicates using Windows batch file

I'm trying to make a batch file through Notepad to essentially automate a process of moving files from drive to drive.
My aim is to move files from my H drive to my A drive, H:\Arco\examplefile.csv to \A:\DSE\Open_Access_Data\ARCo.
I also want to automate this job to run every 30 minutes if possible. But if I need to do it by clicking it then so be it. So far, all I've managed to do is copy files over to my desktop. I can't seem to get it to go between my directories.
COPY H:\dehpc14_Disk_Quota_Report.csv %userprofile%\Desktop
This should work:
:LOOP
copy H:\Arco\examplefile.csv A:\DSE\Open_Access_Data\ARCo /y
timeout /t 1800
goto :LOOP
That will copy your files every 30 seconds and overwrite any existing files.
Note: The timeout command is only availble in Vista and above, if you need to use this on XP let me know.
To launch the batch file on system startup you can either put it in the startup folder of the user or use the registry.
The startup folder for the current user is
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
For all users
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
Or you can use the registry which I personally prefer. Create a string value with the path to your batch file in
Current user
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
Computer users
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Both registry options will require admin rights though.
To do either startup or registry in batch respectively
copy %0 "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" /y
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchFile" /d "%0" /f
Which will either copy itself to startup or add itself to the registry each time it runs. So you can either do it manually or have it do this itself (above commands) from the first time you run it.
The %0 is the batch files own path, if you want to use the commands from somewhere else, or just from cmd then type in the full path of the batch file instead.
Use a command like cron in Unix to setup a timed interval to run programs like your automated copy. See Stack Overflow question What is the Windows version of cron? for a cron-like version for Windows.
To copy from one drive to another use
copy filepath1 filepath2
where filepath1 is your H:\path-to-file and filepath2 is your A:\path-to-file.

batch file to copy files to another location?

Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.

Resources