Special Character confilct in batch file - batch-file

I am new to batch files and created a batch file to back up a networked drive at work to my OneDrive folder so that I could access work files at home. Since then they have decided to install Office365 on everyones computer at work. Now instead of OneDrives folder just being called 'OneDrive' it is now called 'OneDrive - G&S Foods, Inc' My very simple batch file worked great until OneDrives name was changed. I cannot change the name back to just OneDrive, so I am trying to work around in my batch file. It seems the special characters are giving me an issue though, expecially the &. I now get the following error
Invalid number of parameters '&' is not recognized as an internal or
external command, operable program or batch file.
My Question is: What is the best way to work around the special characters issue?
Here are my current script lines:
#echo off
xcopy j:\Brandon C:\Users\bweibley\OneDrive - G&S Foods, Inc /m /e /y
xcopy J:\Joe's Folder\ChocScheduleBackUps C:\Users\bweibley\OneDrive - G&S Foods, Inc\ChocScheduleBackUps /m /e /y

Do so by enclosing the whole path in double quotes:
xcopy "j:\Brandon" "C:\Users\bweibley\OneDrive - G&S Foods, Inc" /m /e /y
You should already use double quotes if only a space is in there. So for your second line:
xcopy "J:\Joe's Folder\ChocScheduleBackUps" "C:\Users\bweibley\OneDrive - G&S Foods, Inc\ChocScheduleBackUps" /m /e /y

Try:
xcopy "j:\Brandon" "C:\Users\bweibley\OneDrive - G&S Foods, Inc" /m /e /y
xcopy "J:\Joe's Folder\ChocScheduleBackUps" "C:\Users\bweibley\OneDrive - G&S Foods, Inc\ChocScheduleBackUps" /m /e /y

Related

Batch File to move subfolder contents from mapped location to Server

I want to write a batch file to move all the content from mapped location to the server
ODM folder is already there on the server.
I tried both the code:
robocopy "\\eng1\d$\ODM" "\\200.200.200.9\c$\ODM" /s /z /move /mt:96 /log:C:\Temp\log.txt
and
xcopy /-y \\eng1\d$\ODM\*.* \\200.200.200.9\C$\ODM\ /d /c /y
pause
I am getting errors as
Invalid username or password for first code and
Invalid Drive specification for second
What is wrong with the code?
You can just run net use as also mentioned by Stephan in the comments above, to ensure it has a connection to both UNC paths, each time using username and password. (Remove domain\ if you do not use domain.
net use \\eng1\IPC$ /user:domain\user password
net use \\200.200.200.9\IPC$ /user:domain\user password
robocopy "\\eng1\d$\ODM" "\\200.200.200.9\c$\ODM" /s /z /move /mt:96 /log:C:\Temp\log.txt

xcopy /D flag copying all files instead of newer/newer files

I'm trying to make a simple batch file to "sync" everything between a file on my onedrive, and a file on a network drive.
I want to be able to run the batch file, and anything new on the onedrive will be copied to the network drive, but not the other way around.
Here is my batch file:
#echo off
echo : XCOPY started
xcopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /E /D /C /Y /EXCLUDE:C:\exclude.txt
echo : complete
when run, this file seems to copy anywhere between 80-83 files. It is different each time I run it, despite me making no changes to either directory.
My understanding is that the /D flag for xcopy will ensure that it only copies files that are new, or have a newer modified date. But in this case, it seems to copy everything that isn't in my exclude.txt, regardless of date modified.
I think you have the two options:
for /f %%G in (c:\exclude.txt) do set "_excludes=%_excludes% %%~G"
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR /XF %_excludes%
or
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR
for /f %%G in (c:\exclude.txt) do del "%%~G"
You test with the /L option to simply log what it would have done.

.bat file autocopy - how to only copy file once?

I want a .bat file that will copy all files and folders from one folder to another folder automatically. For example:
robocopy "c:\source" "D:\target" /e /MON:1 /xc /xn /xo
However I need it so that once a file has been copied, that file will not be copied again, even if the copy has been moved to a different directory. Is there any way to do this? Is it possible for robocopy to create a log and check against that before copying the file?
If robocopy can't do it, what can?
Use additionally the option /M on your robocopy (SS64 article) command line as suggested by Stephan because this option results in copying only files with archive attribute set in source folder tree and removing archive attribute by robocopy after successful copying the file.
%SystemRoot%\System32\robocopy.exe "C:\source" "D:\target" /M /E /MON:1 /XC /XN /XO
The archive attribute is automatically set again on file modification.
You could perhaps also use xcopy (SS64 article):
%SystemRoot%\System32\xcopy.exe "C:\source" "D:\target\" /M /E /C /I /Q /G /H /R /K /Y >nul
Important for your task is again option /M for copying only files with archive attribute set in source folder tree and clearing the archive attribute after copying the file.
Note: /I works without user prompt only with target folder path ending with a backslash.
Run in a command prompt window robocopy /? respectively xcopy /? for details on the other options or read the Microsoft documentations for robocopy and xcopy.
Alright So, the easiest way to do this is to copy each
file and folder individually, to said folder.
This may not be what you are looking for, but
I hope it helps! Sadly there is no way to
copy l files folders with a single command.

Batch file to copy directories recursively

Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
Look into xcopy, which will recursively copy files and subdirectories.
There are examples, 2/3 down the page. Of particular use is:
To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:
xcopy a: b: /s /e
After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):
robocopy source_dir dest_dir /s /e
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
copy *.* C:\dest\dir
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Windows Batch File Looping Through Directories to Process Files?
I wanted to replicate Unix/Linux's cp -r as closely as possible. I came up with the following:
xcopy /e /k /h /i srcdir destdir
Flag explanation:
/e Copies directories and subdirectories, including empty ones.
/k Copies attributes. Normal Xcopy will reset read-only attributes.
/h Copies hidden and system files also.
/i If destination does not exist and copying more than one file, assume destination is a directory.
I made the following into a batch file (cpr.bat) so that I didn't have to remember the flags:
xcopy /e /k /h /i %*
Usage: cpr srcdir destdir
You might also want to use the following flags, but I didn't:
/q Quiet. Do not display file names while copying.
/b Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
/o Copies directory and file ACLs. (requires UAC admin)

Directo copy batch file command

How can copy a folder(located in the location of batch file)containing another folders and files to another location using batch file...
i am trying using command below but not working..
xcopy /E /Y %~p\DagwoodLoki#hp.com\* C:\Loki
Please help me
I dont know what "%~p" does but i think that following should work
xcopy %~p\DagwoodLoki#hp.com\ C:\Loki /E /Y
the /E and /Y must be written later the source and the target.
Maybe this?
xcopy /E /Y "%~dp0\DagwoodLoki#hp.com\*" C:\Loki
%~dp0 is substituted with drive+path to the batch file, without quotes, if any.

Resources