Hello not sure where I should place this question but;
I want to run a batch script on a windows machine every night around midnight.
All I want it to do is back up all files and folders on a network drive and copy this to a hard drive on the computer running the batch script. The only thing unsual I want it do is exclude copyying a folder called trashbox
Local Computer
C:\BACKUP\
Network Drive
Z:\FILES\*
exclude Z:\FILES\trashbox
So it needs to;
Remove previous days backup
Start at midnight
Backup all files and folders on Z:\FILES*
Exclude Z:\FILES\trashbox* from copying
Any ideas would be most appreciated!!
Test this: it will create a mirror backup and delete files that aren't needed but keep files that already exist, and only copy the different files.
robocopy "Z:\FILES" "C:\BACKUP" /mir /xd "trashbox"
Create new batch file using below code. It will first remove the previous directory and create new one after that copy all content to new dir from network drive.
rmdir /s /q "C:\Backup"
TIMEOUT 5
mkdir "C:\Backup"
copy /y "Z:\Networkfolder" "C:\Backup"
TIMEOUT 5
rmdir /s /q "C:\Backup\trashbox"
exit
Related
I have some files in a USB drive which need to be copied to multiple computers. These files contain an executable which will use the other config files.
My issue is, for Windows 10 PCs, while the temp_folder gets created, none of the files get copied.
For windows 7 I was able to create a batch file which copied the files to the local drive and ran the executable using the config files.
The batch file contents were as below :
mkdir C:\temp_installer
copy ".\file_name" "C:\temp_installer"
<rest of the code>
I have tried using xcopy and robocopy, but still see the batch file run and just stop at creating the folder. The same issue isn't observed in Windows 7.
Has someone tried this or can someone tell me what I might be doing wrong?
This would be a better option, we do not need to be concerened about permission issues on the root of C:
#echo off
cd /d "%~dp0"
set "inst_dir=%temp%\temp_installer"
mkdir "%inst_dir%">nul 2>&1
for %%i in (*) do if not "%%i"=="%~nx0" copy /Y "%%i "%inst_dir%"
:# When completed, we can call execute the files from "%inst_dir%"
The for loop is not needed to be honest, I am only doing it to not copy the .bat/.cmd file itself to the folder as there would be no need for it there.
Or even simpler, without having to do all the above, you could just use robocopy
#echo off
cd /d "%~dp0"
robocopy /MIR .\ "%temp%\temp_installer"
Powershell is your friend here, try this:
Copy-Item E:\Document\ C:\Temp\Document\ -R
Works great for me and it even creates destination directory, also Copy-Item has alias cp and copy.
If you running some sort of script, you might have issues with Execution-Policy: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6
I'm running a TeamCity server and agent on a Windows machine. My last step in the build process is to upload the bin/release filed over to a shared Windows folder on another server through SMB.
I need to delete all filed on the remote server before uploading the new build but can't figure out a way to do it.
I don't see any such option in SMB upload runner.
Yes, you are correct that should be added as a step under build steps, I would prefer a powershell command something like this
robocopy \\%WebServer1%\%SourceFolder% \\%WebServer1%\%DestinationFolder% /E /PURGE /IS /COPY:DT /R:1 /W:2
RMDir /S "%WebServer1%\%SourceFolder%
Where,
/E - Copies sub directories
/PURGE - Deletes destination files and directories that no longer exist in the source
/COPY:DT - Specifies the file properties to be copied, in this case it copies Data and Timestamps
/R:1 - Specifies the number of retries on failed copies, in this case it is 1
/W:2 - Specifies the wait time between retries, in seconds, in this case it is 2 seconds
/s - Includes subdirectories
RmDir will remove the source directory once the robocopy is successful.
If you need to directly remove the files instead of copying and then removing, you could use Move
Reference for Move - https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/move
I would personally prefer copy and delete
call :deleteSelf&exit /b
:deleteSelf
start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b
This is the code I use. Batch file running it sits within C:\Windows\test
The file is successfully deleted, along with any other files in the directory, but not the directory itself. Does anyone know some way to solve this issue? I'm rather stumped.
You will need, at least, to
leave the current batch file so it is not open
ensure your current active directory is not the one you want to remove
so, if you follow the already pointed dbenham's approach for leaving the current batch file you could use something like
((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0")
That is,
the (goto) will generate an error that will leave current batch file execution
we change the current active directory to the parent of the folder where the batch file is stored
it the active directory has been changed, we try to remove the folder that holds the batch file
Of course, if there is another process/file locking the folder you will not be able to remove it.
surely it's not as easy as adding the following line to your batch file:
cd c:\
rd c:\windows\test
There is a shared folder in my D drive as works (D:\works). I need to delete all the files in that folder and sub folders except word and excel files in there. how can i do this ?
You could do something similar to what this guy's done: http://www.codesingh.com/2009/08/using-robocopy-to-delete-old-files-from.html
Something like this should work:
mkdir D:\_tempDelete
robocopy D:\works D:\_tempDelete /e /MOVE /XF *.xls* *.doc*
rmdir D:\_tempDelete /s /q
Provided you have permissions to create and delete folders on D:. Otherwise you could just move the files somewhere on your local drive and delete them from there.
I am trying to write a Windows Batch file that will allow me to move all directories within a given source directory into a target directory that exists within that source directory.
Obviously my move command with need to only apply to directories and also exclude the target directory from being processed.
Is this possible with a Windows batch command?
Robocopy (present in recent versions of windows or downloadable from the WRK) can do this, just use the /xd switch to exclude the target directory from the copy;
robocopy c:\source\ c:\source\target\ *.* /E /XD c:\source\target\ /move
FOR /d %%i IN (*) DO IF NOT "%%i"=="target" move "%%i" target
That won't work - you'll get an error telling you the target directory is inside the source directory or so, even if you explicitly exclude the target directory. What you can do is move the directories to a temporary location which is not under the source, and then move them into the target.
BTW, using the move command won't let you specify folders to exclude. For that you can use xcopy, but note that it will copy the folders, as opposed to move them. If that matters, you can delete whatever you want afterwards, just make sure you don't delete the target dir, which is in the source dir...
Using robocopy included with Windows 7, I found the /XD option did not prevent the source folder from also being moved.
Solution:
SET MoveDirSource=\\Server\Folder
SET MoveDirDestination=Z:\Folder
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"
This loops through the top level folders and runs robocopy for each.
NB: Robocopy mentioned above using the /move flag will copy the files and then delete them from the source folder rather than moving the files. This may be critical if moving large numbers of files from one location to another on the same disk (because move is virtually instantaneous, while copying is a much slower operation)
On windows batch:
FOR /d %%i IN (MySourceDirectory\*) DO move "%%i" MyTargetDirectory\%%~ni
The above command moves all directories found in MySourceDirectory (/d) to MyTargetDirectory using the original directory name
(~ni) Robocopy's move first does a copy, then delete, so it is slower.
This works for me:
move c:\fromDir\*.* c:\toDir\