How to rename a folder on flash drive using a batch file - batch-file

I am trying to make a batch file that will back up the contents of a folder onto my flash drive and rename the previous folder on the flash drive. Here is the code.
#echo off
echo Are you sure you want to erase the previous backup file? (Y,N)
set /p ans=
if %ans%==Y goto Backup
goto Exit
:Backup
rd "E:\Batch\BFiles Backup" /s
ren "E:\Batch\BFiles" "BFiles Backup"
:Copy
mkdir E:\Batch\BFiles
xcopy C:\Users\Habib\Documents\BFiles /E /Y E:\Batch\BFiles
echo Files have successfully been copied!
pause
:Exit
exit
When I run the batch file, it copies the files but doesn't rename the already existing folder because "Access is denied". I have tried running it in the administrator version of Cmd, but it still didn't work. My user is an administrator also, so i don't know why access has been denied.

First thing, remove your #ECHO OFF until you are done debugging your code..
Add PAUSE statements so you can stop and see what is going on..
Let's re-format your code a bit..
ECHO Are you sure you want to erase the previous backup file? (Y,N)
set /p ans=
if %ans%==Y goto Backup
PAUSE
goto Exit
:Backup
PAUSE
IF EXIST "E:\Batch\BFiles Backup\." rd "E:\Batch\BFiles Backup" /s
PAUSE
IF NOT EXIST "E:\Batch\BFiles Backup\." ren "E:\Batch\BFiles" "BFiles Backup"
PAUSE
:Copy
IF NOT EXIST "E:\Batch\BFiles\." mkdir E:\Batch\BFiles
PAUSE
xcopy C:\Users\Habib\Documents\BFiles /E /Y E:\Batch\BFiles
IF errorlevel 0 echo Files have successfully been copied!
pause
:Exit
exit
Then, when things start looking correct, remove the PAUSE statements one by one...
Hope this helps!

Related

Version backups and restoring latest backup with robocopy

I am trying to write a batch file and use the robocopy command to backup a directory. However, I need multiple backups to be kept. Let's say I have a directory "A" in "C:\Source Path" and a backup location "C:\Backup Path".
I need the following to happen:
During the backup, If "C:\Backup Path\A Dir" already exists then the copy result should make "C:\Backup Path\A Dir2"; or a similar numbering system.
During the restore, the highest-numbered backup directory in "C:\Backup Path" should replace "C:\Original Path\A Dir"
My current script does not do any versioning and looks like below:
#echo off
set originalPath="C:\Original Path\A"
set backupPath="C:\Backup Path\A"
:L1
echo 1. Backup save files
echo 2. Restore save files
echo 3. Exit
set /p choice=Select an option:
if %choice% equ 1 goto :BACKUP
if %choice% equ 2 goto :RESTORE
if %choice% equ 3 goto :EOF
goto :L1
:BACKUP
robocopy %originalPath% %backupPath% /E
goto :L1
:RESTORE
robocopy %backupPath% %originalPath% /E
goto :L1
:EOF
Is such an operation possible using robocopy alone?

Deleting and copying files using batch file

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

Batch - Error when renaming a file to timestamp

I have a program that takes EVERYTHING from the directory
C:\Users\Hensel\Desktop\mc-server\world, and it then it copies and pastes it in a separate directory
C:\Users\Hensel\Desktop\backups\
Then it renames the file called 'world' that it copied and renames it to a date/timestamp format.
#echo off
:backup
xcopy C:\Users\Hensel\Desktop\mc-server\world C:\Users\Hensel\Desktop\backups\world /I /E /Y /D
ren world Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%
ping localhost -n 2700 >nul
goto backup
But whenever I run it (I execute it within the backups directory), it returns the error of: "The syntax of the command is incorrect." Whenever I remove the line that renames 'world' it works fine, but the files won't have unique names when they are backed up unless it has a date and timestamp.
Without knowing your exact date/time format, I'd judge that you have spaces in the new filename.
try
set "newname=Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt"
set newname=%newname: =0%
ren world %newname%
and if that doesn't work (which it should) then tell us what happens with
echo newname="%newname%"
which should be inserted just before the REN
So the full new file should be:
#echo off
:backup
xcopy C:\Users\Hensel\Desktop\mc-server\world C:\Users\Hensel\Desktop\backups\world /I /E /Y /D
set "newname=Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt"
set newname=%newname: =0%
ren world %newname%
echo newname="%newname%"
ping localhost -n 2700 >nul
goto backup
where the echo newname="%newname%" line is optional
Meanwhile, in place of your PING line, you could try
timeout /t 2700 >nul
for a 45-minute delay in an "approved" manner.

Creating a batch file with options

I have reviewed the current posts about this subject and have tried the various suggestions for creating the batch file. This is what I have come up with so far. However, the batch file seems to want to only run Copy to Laptop, regardless of the choice selected. Please help!
#ECHO OFF
CLS
ECHO 1.Copy to Desktop?
ECHO 2.Copy FROM Desktop to Flash Drive?
ECHO 3.Copy to Laptop?
ECHO 4.Copy FROM Laptop to Flash Drive?
ECHO.
CHOICE /C 1234 /M "Enter your Choice:"
IF ERRORLEVEL 4 GOTO Copy FROM Laptop to Flash Drive?
IF ERRORLEVEL 3 GOTO Copy to Laptop?
IF ERRORLEVEL 2 GOTO Copy FROM Desktop to Flash Drive?
IF ERRORLEVEL 1 GOTO Copy to Desktop?
:Copy to Laptop?
ECHO Copy to Laptop
xcopy H:\ "C:\Users\8888\Documents\My Games" /e /y
GOTO End
:Copy FROM Laptop to Flash Drive?
ECHO Copy FROM Laptop to Flash Drive
xcopy "C:\Users\8888\Documents\My Games" H:\ /e /y
GOTO End
:Copy to Desktop?
ECHO Copy to Desktop
xcopy H:\ "C:\Users\****\Documents\My Games" /e /y
GOTO End
:Copy FROM Desktop to Flash Drive?
ECHO Copy FROM Desktop to Flash Drive
xcopy "C:\Users\****\Documents\My Games" H:\ /e /y
GOTO End
:End
Spaces are not allowed in GOTO statements.
So all your GOTOs goes to the first :Copy. The rest "to Desktop?" and so on is ignored.
The problem you're having is that all of the labels are using only the first word, which in all cases is COPY (the first space seems to be where it stops looking at the label).
This works perfectly fine for me. Notice that I've changed all of the :Label names to something still meaningful by removing the spaces and the ? at the end. (I also removed the xcopy lines, because they weren't necessary to demonstrate the solution.)
#ECHO OFF
CLS
ECHO 1.Copy to Desktop?
ECHO 2.Copy FROM Desktop to Flash Drive?
ECHO 3.Copy to Laptop?
ECHO 4.Copy FROM Laptop to Flash Drive?
ECHO.
CHOICE /C 1234 /M "Enter your Choice:"
IF ERRORLEVEL 4 GOTO LapTopToFlashDrive
IF ERRORLEVEL 3 GOTO CopyToLaptop
IF ERRORLEVEL 2 GOTO DesktopToFlashDrive
IF ERRORLEVEL 1 GOTO CopyToDesktop
:CopyToLaptop
ECHO Copy to Laptop
GOTO End
:LaptopToFlashDrive
ECHO Copy FROM Laptop to Flash Drive
GOTO End
:CopyToDesktop
ECHO Copy to Desktop
GOTO End
:DesktopToFlashDrive
ECHO Copy FROM Desktop to Flash Drive
GOTO End
:End
Pause

Copy command not working "Batch Programming"

Want to make a installer for a game i play:
#echo off
echo.
echo Choose One
echo __________
echo.
echo 32
echo.
echo 64
echo.
echo Idk
echo.
echo __________
echo.
echo.
set/p bit=
goto %bit%
:32
xcopy "warcraft3" C:\Program Files
pause
goto done
:64
xcopy "warcraft3" C:\Program Files(x86)
pause
goto done
:idk
xcopy "warcraft3" C:\Program Files
pause
goto done
:done
the "warcraft3" is a folder not a zip or anything, it says it cant find the specified file.
Need quotes around folders with spaces
xcopy "warcraft3" "C:\Program Files\"
also good practice to put a trailing slash on destination folders.

Resources