Batch file to copy from all user profiles - batch-file

I want to generate a batch file that will copy all *.doc and *.xls file types from a user's profile. I will then set the batch file to run automatically via Scheduled Tasks after-hours when all users are logged out. Here is what I have:
for %%x in (doc xls) do xcopy c:\Users\user1\*.%%x "\\server\i$\User Backups\user1\%computername%\" /c /i /y /s /d
This works fine, however, I need to generate a line item in the batch file for each and every user in our organization (user1, user2, etc.) so that I hit all profiles. When new users are hired, the file needs to be updated with their profile info. Ideally, I'd like something a bit more automated that is similar to this:
for %%x in (doc xls) do xcopy %userprofile%\*.%%x "\\server\i$\User Backups\%username%\%computername%" /c /i /y /s /d
The downfall is that by using %userprofile% in place of the 'user1' input, it only runs against the currently logged in user. Is there another option I could incorporate that wouldn't care about the currently logged in user, and instead just run against all user profiles on a machine?

You could use reg query to get the list of user profiles from the registry, but you only care about users who have a folder under C:\Users, so just loop over those:
for /d %%u in (C:\Users\*) do for %%x in (doc xls) do xcopy C:\Users\%%~nu\*.%%x "\\server\i$\User Backups\%%~nu\%computername%\" /c /i /y /s /d

Related

Batch File to move subfolder contents from mapped location to Server

I want to write a batch file to move all the content from mapped location to the server
ODM folder is already there on the server.
I tried both the code:
robocopy "\\eng1\d$\ODM" "\\200.200.200.9\c$\ODM" /s /z /move /mt:96 /log:C:\Temp\log.txt
and
xcopy /-y \\eng1\d$\ODM\*.* \\200.200.200.9\C$\ODM\ /d /c /y
pause
I am getting errors as
Invalid username or password for first code and
Invalid Drive specification for second
What is wrong with the code?
You can just run net use as also mentioned by Stephan in the comments above, to ensure it has a connection to both UNC paths, each time using username and password. (Remove domain\ if you do not use domain.
net use \\eng1\IPC$ /user:domain\user password
net use \\200.200.200.9\IPC$ /user:domain\user password
robocopy "\\eng1\d$\ODM" "\\200.200.200.9\c$\ODM" /s /z /move /mt:96 /log:C:\Temp\log.txt

Batch Script to copy folder from Server to user desktop in a network

My goal is to create a batch script to copy a folder with subfolders to user desktop with overwrite option and minimized of command prompt.
I am pushing the script through Group policy in user start up screen.
However I am getting an error when running the script locally. Not sure what I am missing in the script..
#echo off
#cls
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
echo Your Source Path:
set INPUT1=\\X.X.X.X\Test\TMS\
echo Your Destination Path:
set INPUT2=C:\Users\%Username%\Desktop\
xcopy %INPUT1% %INPUT2% /y /s
:minimized
You mentioned folder, so I am writing this assuming you want to create the TMS folder with content on the desktop.
I would try something like this inside of you minimized label. This is untested as I have no Network drives to test with.
for /f "tokens=2" %i in ('net use * "\\X.X.X.X\Test\TMS\" ^| findstr /i Drive') do set "tmpDr=%%i"
mkdir "%USERPROFILE%\Desktop\TMS" >nul
xcopy "%tmpDir%\*" "%USERPROFILE%\Desktop\TMS" /s /y
net use /d %tmpDir% >nul
Some things to note in your code. You have 2 minimized labels, you need to enclose path variables with double quotes to eliminate possible whitespace, you can drop the echo's seeing as you plan on running the script minimized. Last but not least, you do not need to specify full path to the user's desktop as %USERPROFILE% variable already exists as Drive:\Users\Username

Batch - Loop through folders and run command

I am trying to remove Dropbox from around 500+ Windows 7 computers using a batch script. Dropbox is installed on a per-user basis under "%APPDATA%\Dropbox".
To uninstall silently you have to run "%APPDATA%\Dropbox\bin\DropboxUninstaller.exe /S". I have tested this command and it works fine, but the problem is it only uninstalls it from the user directory you run it from. I have tried taking the DropboxUninstaller.exe file and running it from the root of C:, but it fails.
I will be pushing this script out via SCCM, so it will run under the SYSTEM account. I need to find a way to loop between all of the user direcories, find which profiles have the Dropbox\bin\DropboxUninstaller.exe path/file and run it in that location.
I have tested logging in as another user (local admin) and running the Dropbox uninstaller from another users directory, and it uninstalls fine for the user whos directory I run it in, so I know this will work.
If anyone could help me out with the correct command, that would be great! I have googled around for an answer, but it doesn't seem to be out there.
Thanks in advance,
Search user profiles, and for each if unistaller exists, execute it (asumming an "standard" installation, maybe this needs to be adjusted)
#echo off
setlocal enableextensions
for /F "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath /s ^| find "REG_EXPAND_SZ" ^| findstr /v /i "\\windows\\ \\system32\\"') do (
call :doUninstall "%%b"
)
endlocal
exit /b
:doUninstall
set "_uninstaller=%~1\AppData\Roaming\Dropbox\bin\DropboxUninstaller.exe"
if not exist "%_uninstaller%" goto :EOF
start "" /wait "%_uninstaller%" /S
goto :EOF
for /r c:\windows %A in (notepad.exe) do "%A" %windir%\win.ini
Remember %%A in a batch and %A at command prompt.

BAT Command to Delete specific sub-folder

I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"

executing all the .reg files from batch file

I have following script that executes all the .reg files in the current directory. the problem is i also have sub directories in that folder and i want to execute those reg files as well. I am not sure what switch i am supposed to use. i tries using /s which returns all the .reg files of the drive.
this is how my batch file looks like:
rem #echo off
cls
SET folder=%~dp0
for %%i in ("%folder%*.reg") do (regedit /s "%%i")
echo done
pause
EXIT
This is how file/directory structure looks like:
Folder1
batchfile.bat -> user double clicks or executes from command prompt
1.reg
2.reg
3.reg
Folder2
4.reg
5.reg
what am i missing here?
I think that you need a recursive for loop using the /R switch:
for /R "%folder%" %%i in (*.reg) do (regedit /s "%%i")
You didn't actually say what your problem was, so instead of fixing your script here's what I would have done:
#echo off
set folder=%~dp0
for /f "usebackq" %%i in (`dir /s /b *.reg`) do (
regedit /s "%%i"
)
And the /s script on regedit is for silent mode. It tells regedit not to ask the user for confirmation with a dialog box. The for loop is what causes the batch file to loop through all subdirectories, not the /s switch on regedit.
From Microsoft's Knowledge Base:
[/s|-s]
When a filename is specified on the command line, this switch is used to
suppress any informational dialog boxes that would normally be displayed.
This is useful when the Setup program for an application wants to execute
REGEDIT.EXE with a .REG file, but does not want the user to be confused by
any dialog boxes that are displayed.

Resources