Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch file copy cmds not working correctly.

05 Jan 2019   #1
SnakeFist

Windows 7 Ultimate x64 SP1
 
 
Batch file copy cmds not working correctly.

I'm trying to learn batch cmds and i figured the best way to learn is to make one.
I started this about 6 months ago and got it to half work. Tried to get it to work and gave up. Saw it while cleaning my drives up and figured i give it another shot.

Heres the problem.
After choosing 1 and letting it complete, it will go back to the menu like i want it to. But when i go to choose 2 it will do 1 again. If i choose 2 it will redo 1 again. Same for if i close it out and choose 2 then let it finish then choose 3 it will do 2 again same if i chose 3 first. When i close out and do roaming or local it will either copy over half the folder or copy over the folder plus the ubisoft folder.

So trying to fix it it now returns Invalid drive specification error. Even when i chose 7 to exit on start up it gives an Invalid drive specification error.

I was trying to have the batch file after getting everything right to copy all the locations to the folder the batch file is in. I was also going to make it where it copied it back to the original locations so it will be a back up and restore.

So can anyone help me and see where i went wrong? I figured if i can get this right then restoring them would just mean a reversal of the codes.

Game Saves Backup & Restore.bat




My System SpecsSystem Spec
.
05 Jan 2019   #2
dg1261

Windows 7/8.1/10 multiboot
 
 

The problem is right after the command:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:
That command tells the batch file to get the user's selection (1-6), but then there are no subsequent commands telling the script to branch elsewhere depending on that selection. As a result, regardless of the selection the script always executes the immediately following commands:
Code:
IF %M%==1 set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
The script never gets below that because the first "GOTO Menu" always short-circuits it.

To show you what's happening, substitute the following code for the entire bottom half of your script:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:

cls
echo You selected: 

IF %M%==1 echo 1
echo you should only be here if 1 is selected
GOTO Menu
IF %M%==2 echo 2
GOTO Menu
IF %M%==3 echo 3
GOTO Menu
IF %M%==4 echo 4
GOTO Menu
IF %M%==5 echo 5
GOTO Menu
IF %M%==6 echo 6
GOTO Menu
IF %M%==7 EXIT
Pause
This is basically your code except I've stripped out all the "meat" of what each selection is supposed to perform just to focus on the program flow. Run that script and notice the responses as you press each selection.

Now try replacing the bottom half of your script with the following instead:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:

cls
echo You selected: 

goto %M%

:1
echo 1
GOTO Menu
:2
echo 2
GOTO Menu
:3
echo 3
GOTO Menu
:4
echo 4
GOTO Menu
:5
echo 5
GOTO Menu
:6
echo 6
GOTO Menu
:7
EXIT
Pause
Now your program flow branches via the "goto %M%" to the additional labels identifying each branch. Run this script and notice the difference.

BTW, you can then also eliminate the "IF %M%==" conditions because the program is only going to get to that branch when the condition is met anyway, so the "IF %M%" becomes redundant.
My System SpecsSystem Spec
05 Jan 2019   #3
samuria

win 8 32 bit
 
 

look at AutoIt Scripting Language - AutoIt it has lots of built incmds to make life simple and you can have graphics and more and its free with lots of support and you can create an .exe with it
My System SpecsSystem Spec
.

05 Jan 2019   #4
SIW2

Microsoft Community Contributor Award Recipient

Vista x64 / 7 X64
 
 

Another way is use brackets following the IF statement


IF %M% EQU1 (set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
)
My System SpecsSystem Spec
05 Jan 2019   #5
SnakeFist

Windows 7 Ultimate x64 SP1
 
 

Quote   Quote: Originally Posted by dg1261 View Post
The problem is right after the command:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:
That command tells the batch file to get the user's selection (1-6), but then there are no subsequent commands telling the script to branch elsewhere depending on that selection. As a result, regardless of the selection the script always executes the immediately following commands:
Code:
IF %M%==1 set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
The script never gets below that because the first "GOTO Menu" always short-circuits it.

To show you what's happening, substitute the following code for the entire bottom half of your script:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:

cls
echo You selected: 

IF %M%==1 echo 1
echo you should only be here if 1 is selected
GOTO Menu
IF %M%==2 echo 2
GOTO Menu
IF %M%==3 echo 3
GOTO Menu
IF %M%==4 echo 4
GOTO Menu
IF %M%==5 echo 5
GOTO Menu
IF %M%==6 echo 6
GOTO Menu
IF %M%==7 EXIT
Pause
This is basically your code except I've stripped out all the "meat" of what each selection is supposed to perform just to focus on the program flow. Run that script and notice the responses as you press each selection.

Now try replacing the bottom half of your script with the following instead:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:

cls
echo You selected: 

goto %M%

:1
echo 1
GOTO Menu
:2
echo 2
GOTO Menu
:3
echo 3
GOTO Menu
:4
echo 4
GOTO Menu
:5
echo 5
GOTO Menu
:6
echo 6
GOTO Menu
:7
EXIT
Pause
Now your program flow branches via the "goto %M%" to the additional labels identifying each branch. Run this script and notice the difference.

BTW, you can then also eliminate the "IF %M%==" conditions because the program is only going to get to that branch when the condition is met anyway, so the "IF %M%" becomes redundant.
So after 1 in your code do i put set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"?
Like i said i'm just learning this. But i see i didn't learn much at all from my research except the wrong way to do stuff.

I used this as the basis of my batch file that is why i thought it was good.

Batch Files - Create a Menu to Execute Commands
My System SpecsSystem Spec
05 Jan 2019   #6
SnakeFist

Windows 7 Ultimate x64 SP1
 
 

Quote   Quote: Originally Posted by SIW2 View Post
Another way is use brackets following the IF statement


IF %M% EQU1 (set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
)
If i do that, i only did the first one as a test.

I get this error.

'"C:\Program Files (x86)\Steam\userdata"' is not recognized as an internal or external command,

operable program or batch file.

Here is what i did exact as yours. And got error above.

(set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
)
My System SpecsSystem Spec
05 Jan 2019   #7
SnakeFist

Windows 7 Ultimate x64 SP1
 
 

Quote   Quote: Originally Posted by samuria View Post
look at AutoIt Scripting Language - AutoIt it has lots of built incmds to make life simple and you can have graphics and more and its free with lots of support and you can create an .exe with it
I'll look into this and see what i scripting language i have to learn to use it.
My System SpecsSystem Spec
06 Jan 2019   #8
dg1261

Windows 7/8.1/10 multiboot
 
 

Quote   Quote: Originally Posted by SnakeFist View Post
So after 1 in your code do i put set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"?
Correct. Where you have:
Code:
IF %M%==1 set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
replace with:
Code:
:1
set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
(Disclaimer: I didn't troubleshoot those commands, but I'm assuming you had at least the "1" option working correctly and your only issue was getting the "2" through "6" options working similarly. I trust there really exists a folder called "F:\Save Game Files\Goes Back To Steam Userdata", for instance.)

What this change does is setup a jump destination labeled "1" for the commands you want to execute when "1" is pressed. Note you're only going to get here if %M% is 1, so the "if %M%==1" is redundant here and can be eliminated.

Make similar changes for the "2" through "7" subsections.

You want each of these subsections to execute if and only if the corresponding number is selected from the menu. To accomplish that kind of discrimination, precede the whole lot with a "goto %M%" jump instruction right after the "SET /P M=Type" command, ala:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:
cls
goto %M%
What the SET command does is set an environment variable M to whatever number is pressed (i.e., it will perform a "set M=1", or "set M=2", or whatever), then the following "goto %M%" tells the script to jump to the corresponding subsection (i.e., it will translate to "goto 1", or "goto 2", or whatever).

The "cls" is optional, but I added it just to clear the display after each selection to make it look a little cleaner.
My System SpecsSystem Spec
07 Jan 2019   #9
SnakeFist

Windows 7 Ultimate x64 SP1
 
 

Quote   Quote: Originally Posted by dg1261 View Post
Correct. Where you have:
Code:
IF %M%==1 set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
replace with:
Code:
:1
set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
(Disclaimer: I didn't troubleshoot those commands, but I'm assuming you had at least the "1" option working correctly and your only issue was getting the "2" through "6" options working similarly. I trust there really exists a folder called "F:\Save Game Files\Goes Back To Steam Userdata", for instance.)

What this change does is setup a jump destination labeled "1" for the commands you want to execute when "1" is pressed. Note you're only going to get here if %M% is 1, so the "if %M%==1" is redundant here and can be eliminated.

Make similar changes for the "2" through "7" subsections.

You want each of these subsections to execute if and only if the corresponding number is selected from the menu. To accomplish that kind of discrimination, precede the whole lot with a "goto %M%" jump instruction right after the "SET /P M=Type" command, ala:
Code:
SET /P M=Type 1, 2, 3, 4, 5 or 6 then press ENTER:
cls
goto %M%
What the SET command does is set an environment variable M to whatever number is pressed (i.e., it will perform a "set M=1", or "set M=2", or whatever), then the following "goto %M%" tells the script to jump to the corresponding subsection (i.e., it will translate to "goto 1", or "goto 2", or whatever).

The "cls" is optional, but I added it just to clear the display after each selection to make it look a little cleaner.
Ok i went ahead and did the changes you said to make.

Code:
SET /P M=Type 1, 2, 3, 4, 5, 6 or 7 then press ENTER:
cls
goto %M%
:1
set drive=F:\Save Game Files\Goes Back To Steam Userdata
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Steam\userdata" "%drive%"
GOTO Menu
:2
set drive=F:\Save Game Files\Goes Back To Documents Folder
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "%userprofile%\documents" "%drive%"
GOTO Menu
:3
set drive=F:\Save Game Files\Goes Back To Saved Games Folder
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "%userprofile%\Saved Games" "%drive%"
GOTO Menu
:4
set drive=F:\Save Game Files\Goes Back To AppData Folder
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "%AppData%\Local" "%drive%"
GOTO Menu
:5
set drive=F:\Save Game Files\Goes Back To AppData Roaming Folder
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "%AppData%\Roaming" "%drive%"
GOTO Menu
:6
set drive=F:\Save Game Files\Goes Back To Ubisoft Save Games Folder
set backupcmd=xcopy /s /r /y /k /v /e /i
%backupcmd% "C:\Program Files (x86)\Ubisoft\Ubisoft Game Launcher\savegames" "%drive%"
GOTO Menu
:7
EXIT
Pause
Everything is working except the appdata\local and appdata\roaming options.
When i choose option 4 i will get the error File not found - Local
0 File(s) copied
When i choose option 5 i will get the error File not found - Roaming
0 File(s) copied
Other then them 2 options not working the rest work great.


The folder F:\Save Game Files\Goes Back To Steam Userdata is just for internal testing.

So basically each location that gets backed up goes into it's own folder inside the save game files folder.

I know that not everyone will have an F:\ drive. I used that location for internal testing purposes. What i want to do is after i get everything working is to have the folder save game files created inside the same folder as where the batch file is.
So the batch file and the save game files are in the same folder cause i also want to if possible to also make it to restore the save game files back to the correct folders where they was backed up from.

So after i get everything correct. I want to add options 7 through 12 to restore what was backed up in options 1 through 6. So 13 will become exit. To make it easy for people who don't know much about computers to be able to backup and restore their save game files with one batch file.
My System SpecsSystem Spec
08 Jan 2019   #10
dg1261

Windows 7/8.1/10 multiboot
 
 

Quote   Quote: Originally Posted by SnakeFist View Post
Ok i went ahead and did the changes you said to make.
[...]
Everything is working except the appdata\local and appdata\roaming options.
When i choose option 4 i will get the error File not found - Local
0 File(s) copied
When i choose option 5 i will get the error File not found - Roaming
0 File(s) copied
Other then them 2 options not working the rest work great.
For troubleshooting purposes, temporarily precede the "%backupcmd% ..." lines in the 4 and 5 subsections with "echo %backupcmd% ..." This will tell the script to echo on screen what command it's designed to perform, in lieu of actually trying to perform it.

This can be system-dependent, but on my system it reveals subsection 4 is trying to execute:
xcopy /s /r /y /k /v /e /i "C:\Users\Dan\AppData\Roaming\Local" "F:\Save Game Files\Goes Back To AppData Folder"

while subsection 5 is trying to execute:
xcopy /s /r /y /k /v /e /i "C:\Users\Dan\AppData\Roaming\Roaming" "F:\Save Game Files\Goes Back To AppData Roaming Folder"

Obviously, these commands are only going to work if these folders exist:
"C:\Users\Dan\AppData\Roaming\Local"
"F:\Save Game Files\Goes Back To AppData Folder"
"C:\Users\Dan\AppData\Roaming\Roaming"
"F:\Save Game Files\Goes Back To AppData Roaming Folder"
The two C: folder locations look particularly suspicious to me. Check your system to see if Roaming\Local and Roaming\Roaming actually exist. They don't on mine ... hence, the error messages.

Looking at your script, I see those subsections are drawing part of the filespec from %AppData%, so that's where you're probably running into trouble. You need to modify that or use something else.


Technical aside: your filespec in subsection 4 finishes with "Local", but you're not telling the OS whether that refers to a file or a folder. If there's a folder named "Local" at "C:\Users\Dan\AppData\Roaming" it will correctly guess you want all the files in the "Local" folder, but if there's no such folder there, it will assume you must be referring to a file named "Local" at that location.

Since my system has neither a file nor a folder called "Local" in "C:\Users\Dan\AppData\Roaming", the OS is assuming your script is referring to a file and hence the "File not found - Local" error message.

The same goes for subsection 5.
My System SpecsSystem Spec
Reply

 Batch file copy cmds not working correctly.




Thread Tools




Similar help and support threads
Thread Forum
Batch File .bat to copy file from File Sharing & FTP Hosting
Hi all, Any idea how to write Batch File .bat to copy file from File Sharing & FTP Hosting to local directory. Current the File Sharing & FTP Hosting is SMARTFILE. I don't want to use WINSCP as the connector. Thanks
General Discussion
Unable to correctly parse lines in a .txt file through batch
Good Afternoon All, This is my first post so if I inadvertently insult anyone through my mannerism I apologize in advance. So the puzzle is that my company creates programs for our clients and would like me to create a batch file that can be placed on the desktop and auto-copy all of the...
General Discussion
Batch file to copy files from C drive to USB
Hi all, To increase our backup solutions, we have decided to backup our SQL database every hour to a folder within the C drive (we couldn't get it to backup straight to a USB drive). So now we need a way (batch file best solution?) to copy these files to the USB drive, lets say every 1.05hours...
Backup and Restore
Windows7 Batch File Copy Possible?
Fellow Forum Members, I have two external hard drives I'm calling Hard Drive A and Hard Drive B. Hard Drive A & B are mounted to external USB cradles. Both USB cradles are connected to the same PC containing Hard Drive C. My objective is to transfer all data files, folders, sub folders from both...
Performance & Maintenance
How to copy a file from 1 pc to another pc in network using batch file
Help me make a batch file that copies a file from one computer to another computer in a network... PC1 = Source PC2 = Destination I want to copy a file from PC1's Drive E: to PC2's Drive E:
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 23:40.
Twitter Facebook Google+