Im trying to copy files from one drive to another using a batch file, Which works! but we keep Changing file names on our main file which creates addition copys with diffrent names everytime its run. I dont want to delete the Copy file entirely bacause of the length of time the copy takes just to copy. I would like to Compare the 2 files and delete the files that are no longer on the main drive here is the test that im working on. Thanks for any help you can give me.
#echo
cls
If not exist "C:\Users\Jeremy\Desktop\Test Main\*.*" "Del "C:\Users\Jeremy\Desktop\Test Clone\*.*"
xcopy "C:\Users\Jeremy\Desktop\Test Main\*.*" "C:\Users\Jeremy\Desktop\Test Clone\*.*" /D /C /E /S /I /Y /V /H /R /F /d:01-01-1998
pause
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
you might want to look into robocopy, specifically with the /mir switch, which mirrors (copy all new files and delete all no longer existing files) the source folder to the target.
Thanks
This does work
#echo
cls
robocopy /MIR "C:\Users\Jeremy\Desktop\Test Main" "C:\Users\Jeremy\Desktop\Test Clone"
pause
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
But I would still like to understand if anyone can or wants to take the time to correct my original question
Try:
#echo off
cls
If not exist "C:\Users\Jeremy\Desktop\Test Main\*.*" "Del "C:\Users\Jeremy\Desktop\Test Clone\*.*"
xcopy "C:\Users\Jeremy\Desktop\Test Main\*.*" "C:\Users\Jeremy\Desktop\Test Clone\*.*" /D /C /E /S /I /Y /V /H /R /F /d:01-01-1998
If %errorlevel% EQU 2 (
echo You pressed CTRL+C to end the copy operation.
)
pause
Related
I have a batch script that deletes a folder but if the folder cannot be found, then the script stops.
I am using the follow command: rd Folder
Anything underneath that command is not executed if the folder doesn't exist. How can I fix this?
My script:
net share Users /delete
taskkill /F /IM status.exe
cd C:\Users\Normal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
del status.exe
cd C:\Users\Normal\AppData\Roaming
rd Others /s /q
REM ANYTHING BELOW WILL NOT CONTINUE:
start /b "" cmd /c del "%~f0"&exit /b
exit
pause > nul
Add a conditional statement to check if the folder does indeed exist before deleting. Doing so, your batch-file will not cease to run.
if exists c:\my_folder_to_delete\
REM Delete my folder now that I know it exists!!
replace the line rd Others /s /q with if exist Others (rd Others /s /q).
See http://ss64.com/nt/if.html for more complex conditional commands.
Here's my problem. Playing a PC game (Arkham City) that can sometimes corrupt the save out of nowhere. My idea is to copy the save data to a new directory (as opposed to overwriting it) every time I start the game. In fact, this batch file will do everything. Copy files to new directory, and execute the game. I want it to copy all of the files to a variable directory every time I execute the batch into a new folder like Backup1, Backup2, etc.
There are two directories this games uses for save data - need them both:
C:\Users\Daddy\Documents\WB Games
C:\Users\Daddy\AppData\Local\Microsoft\XLive
Those directories and subdirectories when then copy to a location like this:
C:\Users\Daddy\Documents\Batman\GameSaveData\Backup1\ where the value after 1 is the variable. So literally every time I execute this batch, it dumps a new set of save data into a new Backup% folder at that directory.
The example below is static, but just overwrites to same directory. Unfortunately I wouldn't know if my game was corrupted until I go to play, but by then, the batch will have overwritten the good save with the corrupted save. This is why I want to always dump into a new backup folder.
xcopy "C:\Users\Daddy\Documents\WB Games" "C:\Users\Daddy\Documents\Batman\GameSaveData\WB Games" /D /E /C /R /I /K /Y /S
xcopy "C:\Users\Daddy\AppData\Local\Microsoft\XLive" "C:\Users\Daddy\Documents\Batman\GameSaveData\XLive" /D /E /C /R /I /K /Y /S
"C:\Program Files (x86)\WB Games\Batman Arkham City GOTY\Binaries\Win32\BmLauncher.exe"
I'd suggest you substring %date% and %time% to a yyyymmddhhmmss variable and use that in place of 1,2 etc.
Quite how you'd do that would depend on your date and time formats, but it's documented extensively on SO.
Using the yyyymmddhhmmss format will mean that the last directory in a dir list would be the last chronologically...
This should do the trick:
(will create a subfolder in "%Userprofile%\Documents\Batman\GameSaveData" named YYYY-MM-DD_HourMinuteSecond and copy the contents to it)
#ECHO OFF
FOR /F "SKIP=1 TOKENS=1-7" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF NOT "%%~F" == "" (
SET YYYY=%%F
SET MM=00%%D
SET DD=00%%A
SET HH=00%%B
SET Min=00%%C
SET Sec=00%%E
)
)
SET friendlyTime=%YYYY%-%MM:~-2%-%DD:~-2%_%HH:~-2%%Min:~-2%%Sec:~-2%
SETLOCAL ENABLEDELAYEDEXPANSION
IF NOT EXIST "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%" MD "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%"
ENDLOCAL
XCOPY "%Userprofile%\Documents\WB Games" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\WB Games" /D /E /C /R /I /K /Y /S
XCOPY "%LocalAppData%\Microsoft\XLive" "%Userprofile%\Documents\Batman\GameSaveData\%friendlyTime%\XLive" /D /E /C /R /I /K /Y /S
Edit:
i haven't checked your XCOPY-Commandline, just copy and pasted it. :)
I am trying to write a bat file to backup a folder on my work server (sometimes the server and backup server do not sync correctly and files go missing).
I have tried many different solutions and read a few different forums to try to resolve this, but I cannot seem to find anything.
#echo This will now create a new backup of S:\Internal Auditor\9 - September 14
#echo off
:: variables
set SRCFOLDER="S:\Internal Auditor\9 - September 14"
set DESTFOLDER="S:\Internal Auditor\2014\9 - Sept Backup"
set folder=%date:~5,2%-%date:~8,2%-%date:~0,4%
set backupcmd=xcopy /W /E /H /V /C /Z /I /F /J /R /Y
echo ######## PLEASE WAIT SYSTEM BACKINGUP SOME DATA########
xcopy %SRCFOLDER% %DESTFOLDER% %backupcmd%
echo !!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
#pause
Please help - I'm tired of losing files, and I don't want to have to manually backup files every day.
(The goal is the create a new folder with date & time every time it runs under the sub-folder "9 - September 14"{historical backup}).
EDIT
Ok - So I have another thread open for something that was different, but now my 2 questions have kinda merged together, so please look # New folder for every backup CMD and see if you could help...
use set backupcmd=/W /E /H /V /C /Z /I /F /J /R /Y
instead of set backupcmd=xcopy /W /E /H /V /C /Z /I /F /J /R /Y . You have redundant xcopy in parameters.
EDIT. As far as I understood your comments you need a new folder like this "S:\Internal Auditor\%date:~5,2%-%date:~8,2%-%date:~0,4%"
so you can do this:
set SRCFOLDER="S:\Internal Auditor"
set "DESTFOLDER="S:\Internal Auditor\2014"
set "folder=%date:~5,2%-%date:~8,2%-%date:~0,4%"
md "%DESTFOLDER%\%folder%" >nul 2>&1
set "backupcmd=/W /E /H /V /C /Z /I /F /J /R /Y"
echo ######## PLEASE WAIT SYSTEM BACKINGUP SOME DATA########
xcopy "%SRCFOLDER%\%folder%" "%DESTFOLDER%\%folder%" %backupcmd%
echo !!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
After you enter the required source and destination path try this code..
set xcopy=xcopy //switches as per your requirement
set Folder=%Date:~-7,2%-%Date:~-10,2%-%Date:~-4,4%
mkdir %DESTPATH%\%Folder%
pause
%xcopy% %SOURCEPATH% %DESTPATH%\%Folder%
pause
I need to delete all .jpg and .txt files (for example) in dir1 and dir2.
What I tried was:
#echo off
FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t
In some directories it worked; in others it didn't.
For example, this didn't do anything:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
What I'm I missing in the second snippet? Why didn't it work?
You can use wildcards with the del command, and /S to do it recursively.
del /S *.jpg
Addendum
#BmyGuest asked why a downvoted answer (del /s c:\*.blaawbg) was any different than my answer.
There's a huge difference between running del /S *.jpg and del /S C:\*.jpg. The first command is executed from the current location, whereas the second is executed on the whole drive.
In the scenario where you delete jpg files using the second command, some applications might stop working, and you'll end up losing all your family pictures. This is utterly annoying, but your computer will still be able to run.
However, if you are working on some project, and want to delete all your dll files in myProject\dll, and run the following batch file:
#echo off
REM This short script will only remove dlls from my project... or will it?
cd \myProject\dll
del /S /Q C:\*.dll
Then you end up removing all dll files form your C:\ drive. All of your applications stop working, your computer becomes useless, and at the next reboot you are teleported in the fourth dimension where you will be stuck for eternity.
The lesson here is not to run such command directly at the root of a drive (or in any other location that might be dangerous, such as %windir%) if you can avoid it. Always run them as locally as possible.
Addendum 2
The wildcard method will try to match all file names, in their 8.3 format, and their "long name" format. For example, *.dll will match project.dll and project.dllold, which can be surprising. See this answer on SU for more detailed information.
You can use this to delete ALL Files Inside a Folder and Subfolders:
DEL "C:\Folder\*.*" /S /Q
Or use this to Delete Certain File Types Only:
DEL "C:\Folder\*.mp4" /S /Q
DEL "C:\Folder\*.dat" /S /Q
I wrote a batch script a while ago that allows you to pick a file extension to delete. The script will look in the folder it is in and all subfolders for any file with that extension and delete it.
#ECHO OFF
CLS
SET found=0
ECHO Enter the file extension you want to delete...
SET /p ext="> "
IF EXIST *.%ext% ( rem Check if there are any in the current folder :)
DEL *.%ext%
SET found=1
)
FOR /D /R %%G IN ("*") DO ( rem Iterate through all subfolders
IF EXIST %%G CD %%G
IF EXIST *.%ext% (
DEL *.%ext%
SET found=1
)
)
IF %found%==1 (
ECHO.
ECHO Deleted all .%ext% files.
ECHO.
) ELSE (
ECHO.
ECHO There were no .%ext% files.
ECHO Nothing has been deleted.
ECHO.
)
PAUSE
EXIT
Hope this comes in useful to anyone who wants it :)
I don't have enough reputation to add comment, so I posted this as an answer.
But for original issue with this command:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
The first For is lacking recursive syntax, it should be:
#echo off
FOR /R %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
You can just do:
FOR %%p IN (C:\Users\0300092544\Downloads\Ces_Sce_600) DO #ECHO %%p
to show the actual output.
this is it:
#echo off
:: del_ext
call :del_ext "*.txt"
call :del_ext "*.png"
call :del_ext "*.jpg"
:: funcion del_ext
#echo off
pause
goto:eof
:del_ext
set del_ext=%1
del /f /q "folder_path\%del_ext%"
goto:eof
pd: replace folder_path with your folder
Step 1:
Navigate to the folder in question using the cd command
For example:
cd C:\Users\tremanleo\Desktop\HoldLEOCMS
Step 2
Delete the the file type.
For Example:
DEL *.bak
If you are trying to delete certain .extensions in the C: drive use this cmd:
del /s c:\*.blaawbg
I had a customer that got a encryption virus and i needed to find all junk files and delete them.
I'm trying to make a program that will delete some files and perfrom rutine maintance on a computer by just clickin on one file. I'm testing it as I'm going along and realized that it's not deleting the folders. I want to delete everything within the folders but not the folders themselves. Here is my code so far:
#echo off
title SYSTEM Optimiation
echo Deleting Temp Folder
del /q /f "C:\Documents and Settings\%username%\Local Settings\TEMP"
echo.
echo DONE
echo.
echo Deleting Download folder
del /q /f "C:\Documents and Settings\%username%\My Documents\Downloads"
echo.
echo DONE
echo.
echo.
echo Hit any key to exit.
pause >nul
Try using wildcards and the /s switch on del:
del /q /s /f "%userprofile%\My Documents\Downloads\*"
but this will probably leave directories inside intact, but empty. Another option would be the quite explicit:
for /d /r "%userprofile%\My Documents\Downloads" %%x in (*) do rd /s /q "%%x"
for /r "%userprofile%\My Documents\Downloads" %%x in (*) do del /f "%%x"
Here much simpler than above. Current directory will be locked and therefore will not be deleted with others.
cd %Temp% && rmdir /s /q .