How can I make a batch script copy files from the user folder with support for different users? - batch-file

I would like to use a short batch script to copy files from OneDrive to a different folder, the issue is the OneDrive folder is in the users folder under C:/.
What can I do to have the batch script go into the user folder of the user currently logged in followed by the OneDrive folder to copy a specific file?
Currently this is what I have:
xcopy "C:\jmills\OneDrive\TestFolder\Test_2018.accde" "C:\Test\Test_EXE\" /d
As you can see the user "jmills" is hard coded which makes the batch work with only this specific user.

It is hardcoded because you selected to hardcode it. Note that there is an environment variable called username which holds the username of the user currently logged in. Variables in batch file can be accessed by % or ! when delayed expansion is enabled. I find no reason for using delayed expansion here, so use just percent-signs:
xcopy "C:\%username%\OneDrive\TestFolder\Test_2018.accde" "C:\Test\Test_EXE\" /d

Related

Using command to find DIR name within USER folder

Im creating a .bat file to copy over a folder from the current directory into the USERS/???/AppData/Local/CourseWork/ folder
However there is no way of knowing what the name of the ??? folder will be as it will be a random students computer.
Is there any way Using Command (and my BAT file) To either retrieve said Folder name , Or copy the files into the Coursework Folder without knowing the ??? folder name.
i was hoping it would be as simple as
copy "Test.exe" "C:\USERS\ . \APPDATA\LOCAL\test.exe"
but unfortunatly it is not.
Iv also looked at Xcopy and robocopy but found no solution , not that there is no solution, i just have not yet found it...
Does anybody have any idea how they would work around this
My .bat file works fine for 1 set of folders & files but once it comes time for this particular folder (C:/USERS/???/APPDATA/LOCAL/COURSEWORK/), im very stuck on what to do for not knowing the name of the ??? folder or how to use Command to copy into the Coursework folder whilst bypassing the name of ???
Thanks for any help.
If the .bat file script is run when the user is logged in, then it is simple.
COPY ".\Test.exe" "%USERPROFILE%\AppData\Local"
If the .bat file script is run while logged in as a different user (ie. "teacher"), then the username must be known AND the "teacher" account must have permission to write into the user's directories.

Ideas for cmd to delete content of documents folder WIN 10?

I want to create a .bat file with a command line inside that deletes all content of the C:\Users\Documents folder. I only want to delete the entire content of the folder, not the folder itself.
Why do I need this? Our company helps unemployed people find jobs and they use our laptops to create CVs and application lettres. They forget to delete their data (most of them don't really know how to use a computer) so I am trying to automate this process with a .bat file and a scheduled task (run script when user logs in).
If the laptops were WIN10 Pro, I would have used group policies in AD, but these computers are WIN10 home.
Any Ideas? Thank you for your help.
One other way to do it is to copy an empty directory using robocopy to the documents folder. So as an example, Create a directory called none.
In this example, I created the empty directory as D:\none
robocopy "D:\none" "%userprofile%\Documents" /MIR
This will work from cmd and in a batch-file.

Delete file from C:\Windows folder

I'm working with an application that was poorly designed. When installed, it creates a .INI file (remember those?) in the C:\Windows folder. In order to get things working correctly in our environment, I don't use that .INI file. Instead, I have separate .INI files in a different location where I can properly management them within the framework of the application. The problem I'm running in to is users not running the application correctly so it picks up this default .INI file that doesn't work in our environment (remember I said the application is poorly designed).
To make sure that my users are running the correct configuration, I need to delete this pesky .INI from the C:\Windows folder. I have .BAT files that manage application launch and updates so I tried to just issue a delete in the .BAT file but it isn't working. When I try to run just the delete command in a command window, I get "Access Denied". I can't rename it or move it either. I would just go touch each computer but don't have the time to get to them all.
I have just spent the last two hours googling to find a solution and the only solutions are to use Windows Explorer which isn't an option. I know it has to be something simple that I haven't been able to find. Ideas?
for /f "usebackq delims=" %A in ("%userprofile%\Desktop\ComputerName.txt") do echo Del \\%A\C$\Windows\file.ini
Computername.txt is one computer per line, names or addresses.
127.0.0.1

CleanUp Desktop Windows 7

I am searching for a Windows setting (GPO?), scheduled task or a tiny script that enables me either to prevent users to save anything to the Windows desktop or to clean up the entire desktop (shortcuts, files, folders) in a predefined time interval.
The only "hack" I found so far is to restrict user permissions on the desktop folder.
Isn't there a more general solution for clean-up tasks like this?
First, I agree with Ansgar Wiechers: Why delete files and folders of users desktop?
I know, there are some use cases where this makes sense. But such an intervention in the user management should be really explained when asking here on Stack Overflow such a question.
Second, the delete command posted by npocmaka just deletes all files in users desktop folder, but not the subfolders in desktop folder.
Therefore a batch file would need one more line:
#echo off
del /F /Q "%USERPROFILE%\Desktop\*" 2>nul
for /D %%D in ("%USERPROFILE%\Desktop\*") do rd /Q /S "%%~D" 2>nul
After this operation just the items from all users desktop folder are still displayed on desktop of the user.
To prevent users to put files and folders on their own desktop, the string value Desktop in
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
and in
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
could be modified to all users desktop folder which is by default write-protected via appropriate NTFS permissions for standard users.
Of course also the NTFS permissions of folder %USERPROFILE%\Desktop could be changed after the cleanup to prevent a user to put files and folders on own desktop.

How to create a setting that changes the batch copy command based on what your username is?

I have a very simple batch command that goes like this:
xcopy "T:\UserPreferences.xml" "C:\Users\USERNAME\AppData\UserPreferences.xml" /y
So it is pulling a file from the mapped network T drive to the local Users folder. However, because the username for each person is different, it means people on my network cannot run the command without adjusting the command to fit their local machine.
Is there a way to create a variable so that the local folder location is adjusted based on the user who is running it?
You shouldn't be using C:\Users at all.
Copy the file to "%APPDATA%\UserPreferences.xml" instead. The environmental variable APPDATA points to the logged in user's AppData folder, which may not always be located on drive C:. For instance, when roaming profiles are enabled, the APPDATA folder is located on an administrator-specified network drive where it's available from other systems on the network/domain.

Resources