Hello all, I'm new to writing scripts in batch, I have encountered a problem, hope you can help me guys. I want to know how can I get user account name and then replace it in another command in fully automated script, let me explain the issue by some part of my real script. Code: COPY C:\Users\nima\AppData\Local\Temp\file.txt C:\file.txt This script will only work if run under 'nima' user account, how can I get name of user account which run the script and then replace it in that address? P.s.: Do I need Administrator privilege to access files in the \AppData\Local\Temp folder or not? If yes, I would be really really appreciated I you share 'auto administrator elevation' script to put it at start. Best Regards, Nima
Try, Code: copy "%temp%\file.txt" "%systemdrive%\file.txt" It does not need admin privileges. (edit - copying to root of c drive will require admin rights) Edit- Better, Code: if exist "%temp%\file.txt" ( copy "%temp%\file.txt" "%systemdrive%\file.txt" )
Try @BAU 's this code to auto elevate batch script (for rights to copy in root of c drive) , https://pastebin.com/XTPt0JSC Code: ::AveYo: self-elevate passing args and preventing loop set "args="%~f0" %*" & call set "args=%%args:"=\""%%" reg query HKU\S-1-5-19>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit) It is good enough, but it's not perfect. A perfect code requires some more lines, but that should be used when other codes in the script also have the same level of treatment.
Thanks! Very good point you mentioned. In my script, I have to call an EXE that needs access to modify some data in registry, I think I need to run that as Admin, too.
Adding to the good information here, user name is simply %username% C:\Users\%username%\... it's one of the system variables like %systemdrive% etc.
C:\Users\nima\AppData\Local\Temp could be written: %systemdrive%\Users\nima\AppData\Local\Temp C:\Users\%username%\AppData\Local\Temp %userprofile%\AppData\Local\Temp %localappdata%\Temp %Temp% or %TMP% ps: Personally when writing "public scripts", I now prefer to define the temp path I will use as a variable in the beginning of script, because some people will mess with their variables and/or TEMP folder. Then using %tmpfolder% variable in script makes it more "bulletproof" like this: Code: set "tmpfolder=C:\Users\%username%\AppData\Local\Temp" COPY "%tmpfolder%\file.txt" "C:\file.txt" But obviously everyone does have different style and preferences. ps2: Using "" is recommended in case there is space in your path (for example my username has a space...) ps3: I prefer using robocopy when possible (especially if multiple files), copy is good way to change file name though. ps4 As an additional note, switches are your friends: for example copy /y : suppresses prompting to confirm that you want to overwrite an existing destination file. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy (I most often do write copy /b /v /y )