Bat Error "invalid number of parameters" - batch-file

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

Related

Robocopy script to transfer user data from old PC to new PC

I am trying to write a script that I can use to remotely transfer data from an end user's computer, to a new one I am preparing for them. I will need to transfer data from multiple user profiles, so I have written it to cycle through the user profiles on the old machine, but I am unsure of the correct syntax in a couple places, specifically what variable I need to reference the current user folder as the script cycles through them (see the question marks in the directories listed below).
I wrote it to only copy user profiles that have been used in the last 90 days. I would like to copy a few profiles such as Public, Default, etc regardless of age, but I will probably just add a few more Robocopy lines to accomplish that.
Can anyone advise me on what the syntax needs to be where the question marks appear below? This would be $_ in Powershell, but I'm not sure what it is in a CMD batch file.
Thanks in advance,
Andrew
#echo off
Set /p OldPC=Please enter the old PC name:
Set /p NewPC=Please enter the new PC name:
for /D %%D in ("\\%OldPC%\USERS\*") do (robocopy "\\%OldPC%\USERS\?\Desktop" "\\%NewPC%\Users\?\Desktop" /E /Z /W:10 /COPYALL /MAXAGE:90
for /D %%D in ("\\%OldPC%\USERS\*") do (robocopy "\\%OldPC%\USERS\?\Documents" "\\%NewPC%\Users\?\Documents" /E /Z /W:10 /COPYALL /MAXAGE:90
for /D %%D in ("\\%OldPC%\USERS\*") do (robocopy "\\%OldPC%\USERS\?\Favorites" "\\%NewPC%\Users\?\Favorites" /E /Z /W:10 /COPYALL /MAXAGE:90
for /D %%D in ("\\%OldPC%\USERS\*") do (robocopy "\\%OldPC%\USERS\?\Pictures" "\\%NewPC%\Users\?\Pictures" /E /Z /W:10 /COPYALL /MAXAGE:90
I guess you needed more than what you asked.
From what I understood, you want to copy user profile folders for the users who have been using the computer since some date.
To solve the problem, it would be better to find users who logged on rather than finding any file that is edited. Finding edited files will consume too much time to just find the names of used user accounts.
Since there is wevtutil command that is used to deal with logged events, the code became quite simpler.
So the code looks like this:
#echo off
pushd %~dp0
setlocal EnableDelayedExpansion
::User Input
set /p OldPC=Please enter the old PC name:
set /p NewPC=Please enter the new PC name:
set /p UserName=Please enter the User name to use in %OldPC%:
set /p Password=Please enter the password to use in %OldPC%:
set /p MaxDate=Please enter the Maximum logon date:
cls
echo Deriving User Names that are used since %MaxDate%...
echo If this step takes too long, check if you typed correct user name and password.
wevtutil qe Security /r:%OldPC% /u:%UserName% /p:%Password% /f:text /q:"*[System[TimeCreated[#SystemTime>='%MaxDate%T00:00:00'] and (EventID=4624)]]" |^
findstr /b /c:" Account Name" >PossibleUserName.tmp1
dir /b "\\%OldPC%\Users" >UserProfileList.tmp1
findstr /g:"UserProfileList.tmp1" "PossibleUserName.tmp1"|sort >UserList.tmp2
del /q *.tmp1
cls
echo Formatting User List...
for /f "tokens=2 delims=: " %%a in (UserList.tmp2) do (
if not "!ln!"=="%%a" (
set "ln=%%a"
echo %%a>>FinalList.tmp
)
)
del /q *.tmp2
cls
echo Copying Files...
for /f "tokens=*" %%a in (FinalList.tmp) do (
echo robocopy "\\%OldPC%\Users\%%a\Desktop" "\\%NewPC%\Users\%%a\Desktop" /e /z /w:10 /copyall
echo robocopy "\\%OldPC%\Users\%%a\Documents" "\\%NewPC%\Users\%%a\Documents" /e /z /w:10 /copyall
echo robocopy "\\%OldPC%\Users\%%a\Favorites" "\\%NewPC%\Users\%%a\Favorites" /e /z /w:10 /copyall
echo robocopy "\\%OldPC%\Users\%%a\Pictures" "\\%NewPC%\Users\%%a\Pictures" /e /z /w:10 /copyall
)
echo Done!
del /q *.tmp
pause>nul
exit
Since you are trying to use the script from the remote computer, wevtutil command required log in information for your old PC.
Also, you need to input the date that you want to use in %MaxDate%, which will enable you to filter the logs since that date.
The date format should look like this: YYYY-MM-DD
For example, you should type 2017-01-01 if you want to filter users who used the computer since January 1, 2017.
If you set both old and new computers to share their Users Directories with correct properties, then you would be good to go with this code.
I hope this code would solve your problem.

Create batch file to copy entire directory to new variable directory

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. :)

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 File - Using For & Xcopy Getting Invalid Number of Parameters (Not Quotes)?

Basically what I'm trying to do is make a batch file that will run through the computer and grab everything from the Desktops and My Documents of ever user on the computer. Thing is, I won't know every users name because it will be used on unknown computers to back up. Trying to find a way to copy these things but so far I can not. I've been working on My Documents and I keep getting a "Invalid number of parameters".
#echo off
echo This script will copy all of the files from my documents onto a C drive.
pause
md "C:\TestForWork"
pause
for /D /r %%G in ("C:\Users") DO for /D /r "%%H" in ("%%G\My Documents\") do xcopy %%H /e /y "C:\TestFor Work"
pause
for /d %%u in ("c:\users\*") do for %%f in ("Desktop" "Documents") do (
robocopy "%%~u\%%~f" "c:\test for work\%%~u\%%~f" /s
)
Or, for xcopy
for /d %%u in ("c:\users\*") do for %%f in ("Desktop" "Documents") do (
xcopy "%%~u\%%~f" "c:\test for work\%%~u\%%~f" /e /y /i
)
You need to enclose the first parameter to xcopy in double-quotes because it is a directory name containing spaces:
for /D /r %%G in ("C:\Users") DO (
for /D /r %%H in ("%%~G\My Documents\") do (
xcopy "%%~H" /e /y "C:\TestFor Work"
)
)
You also need to use the ~ to prevent double-quotes from being included in the variable expansion.

Batch file won't delete contents of a folder

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 .

Resources