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

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.

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.

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

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

WinSCP downloading to local directory named after today

I have written a WinSCP script which downloads files from a remote server to my local directory. My local directory changes everyday.
option batch abort
option confirm off
open sftp://sftpsite -hostkey="ssh-rsa ab:cd:....."
synchronize local ????? /Home/user/
exit
I am not sure what my local directory should be.
If you want to use today's date as a name of the target local directory, use %TIMESTAMP#yyyymmdd# syntax:
synchronize local C:\Data\%TIMESTAMP#yyyymmdd# /Home/user/
Note that if you are downloading the files to a new folder every day, it's not really synchronization. It is a normal full download. So get command might be more meaningful:
get /Home/user/* C:\Data\%TIMESTAMP#yyyymmdd#\
So.. I think i figured it out. My local directory should be something like this:
C:\Data\%datestamp%
where datestamp is something which changes everyday and is obtained by running a batch process.
option batch abort
option confirm off
open sftp://sftpsite -hostkey="ssh-rsa ab:cd:....."
synchronize local C:\Data\%datestamp% /Home/user/
exit

scheduled batch file fail moving file to google drive

i'm trying to move file from 1 server to an other with schedule batch file, for that i use google drive as a third part where i store my files. i have made some script
#echo "executed %date:~-10,2%%date:~-7,2%%date:~-4,4%" >> Logs.txt
copy /y "C:\backup\Portal2%date:~-10,2%%date:~-7,2%%date:~-4,4%.bak" "c:\users\administrator\google drive\"
this script move a file from a directory to an other, it works fine if i click it,but if i try to schedule it doesn't work.
I tried to change path to a random path in my computer and scheduled it, it worked perfectly.
its like my computer doesn't recognize google drive at all.
When copying over a network you need to use credentials in the scheduled task that have access to the network resource.
This is probably a bit late for you but might be helpful to others who follow in your footsteps. I had this problem and it came down to security. When you run something in batch it doesn't have the correct permissions. I can't remember the exact solution but you need to set the batch task to run with your username.

How can I use batch commands to set the permissions of a directory to allow domain users full access

Basically, I have a directory that domain users need to have full access to. They use a batch file to open the piece of software that they use for their work, and this piece of software needs full access to this directory on their C Drive.
I want to add commands to the batch file to make sure this is always the case.
You should be able to use cacls like this:
cacls directory /g users:F
You will probably need to run this as admin though for it to work though. Unless your domain users have admin rights then you will need to look at another way of going about it.

Resources