Overwrite file contents. Batch File - batch-file

I have a batch file, and i need to move a folder from one directory to another.
I can do that no problem. But, im hitting a problem after that.
The folder i just moved is created at run time, then moved to the final destination folder where it needs to remain, unless the user decides to delete it.
So, if the user runs the batch file a second time, when it tries to move the run time folder, there is already the old version of the folder at the destination and it gives a "permission denied" error.
So my question is, what is the best way to overwrite the contents of a folder from a batch file?
Right now i am just using the move command. Which works fine for overwriting regular files, but it cannot overwrite folders apparently.
Thanks.

Would suggest to use either xcopy or robocopy for Overwriting folders
Xcopy /E /Y would Overwrite folder ,
please find More options Xcopy /?
Please note that i have mentioned this options only to overwrite and not to move folder.

Related

A robocopy batch script made files disappear (not hidden)

I tried robocopy to test copy a single file from a folder to the desktop, and then many files from the desktop disappeared (not hidden).
Now I am desperate to retrieve those files.
I used the script
ROBOCOPY "C:\Users\frank\Desktop\New" "C:\Users\frank\Desktop" /mir
in a txt file and renamed it to .bat.
Then i ran it as admin.
A prompt then opened with many file names rushing past until i closed it.
Now many files are missing from the desktop.
I have tried to research what happened and only came up with cases where robocopy set the copied files to "hidden". But i have set the desktop folder attributes to "show" (it was set to hidden) and they are still missing.
The single file in the "new" folder i was trying to copy over, did not move.

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.

Find a specific folder and copy folder and contents with a batch file

I am trying to write a batch file that will be deployed with SCCM. The script should do the following:
1) Create a scheduled task (confirmed this works)
2) Creates required directory that will store data. (Confirmed this works)
3) Since this will be pushed out through SCCM, we don't know the path that it'll have so the script will need to search the entire C drive for this folder, it'll then need to grab that folder and copy it over to the directory created in step 2. (This is the problematic step)
4) Run an installer (tested this works).
Question is, how can I accomplish step 3? Below is some code I have tried but it fails
dir *TestFolderName*.* /s copy C:\NewlyCreatedFolder
and
xcopy / "%~dp0 TestFolderName" C:\NewlyCreatedFolder
Any suggestions or tips on how to search for the TestFolderName path, capture and move would be great.
Thanks!
You seem to have missed the step where "TestFolderName" is created, although from your xcopy example it seems to imply its where the script itself is deployed to by SCCM.
If that's the case, just use:
copy "%~dp0\TestFolderName\*.*" "C:\NewlyCreatedFolder\"
If this isn't the case, please provide additional info and I'll update my answer.

Unable to copy file to system32 using batch file

I tried to copy a file to System32 with this batch and it said "Access denied":
xcopy "BACKGROUNDDEFAUL.jpg" "C:\Windows\System32\oobe\info\backgrounds\"
pause
Is there a way to do it? If not, why?
C:\Windows\ contains the operating system. You can't directly modify the files in it and its subfolders (such as System32) because an improper modification can break your computer.
To set a picture as background, right-click it and click "Set as desktop background".

Batch File Copy With Undefined Destination

So I have been searching around for the answer here and am not able to find it. I'm not by any means well-versed with coding these sorts of things so excuse my noobishness.
So I am trying to create a simple batch file for deployment with a backup device for the end user to double-click and copy their profile folder to the backup media.
I am not certain if xcopy or robocopy is the best solution for this, but coming from server administration I am more familiar with robocopy. The copy utility I'm using isn't necessarily the problem, however.
What I have:
robocopy D:\%USERPROFILE% <destination> /e /copyall
While the source resolves to the user profile without issue, I have not been able to derive a variable for the destination that could account for the possibility of different from letters, which I don't think is possible. Is there a variable that sets the destination as the location from where the batch file is launched?
Thank you for the help!
Is there a variable that sets the destination as the location from
where the batch file is launched?
yes, there is:
echo %~dp0
this gives you Drive and Path of your batchfile. For more ~.modifiers see call /?

Resources