How to display directory path if a drive is named "\" - batch-file

I am trying to create a script to pull files off of a handheld bar code scanner, however, the drive on the device is named "\" and my script can't find the proceeding folders. Here is my script.
#echo off
title Scan Backup
xcopy "Computer\PASCO\\\My Documents" "C:\Users\VHATAMGrecoM0\Documents\Scan Backup" /E /C /I /Q /G /H /R /K /Y /Z /J
#pause

The >"Computer\PASCO\\My Documents"
Xcopy takes file paths.
\\server\share\path\file or drive:\path\file
Your drive is not called \ as it's illegal to call anything \.

Related

How to keep the original directory of a bat file's shortcut?

I have created a batch file which backs up some save game files before launching the game, this is the script:
#echo off
set SaveGameLocation=C:\Games\MK X\Binaries\Retail\3DMGAME\Player\remote\user
:: variables
set drive=%cd%\Backup
set dateNtime="%date:~0,2%-%date:~3,2%-%date:~6,6%_%time:~0,2%-%time:~3,2%"
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
echo ### Backing up your Sites directory...
%backupcmd% "%SaveGameLocation%" "%drive%\%dateNtime%"
"C:\Games\MK X\Binaries\Retail\MKX.exe"
This batch batch file is located in C:\Save Game Backup\MKX so I've created a shortcut of this file in the desktop, the problem is the current working directory %cd% has changed from the batch file location to the desktop, I know that I can simply change %cd% to C:\Save Game Backup\MKX but I'm always changing the directory and I want a final solution that keeps the file location of the shortcut as the current directory.
You shouldn't be using %CD%, which is the current directory, but instead usiing %~dp0 which is the drive and path (including tailing backslash), of the running batch file.
#Echo Off
Set "ExeLocation=C:\Games\MK X\Binaries\Retail"
Set "SaveGameLocation=%ExeLocation%\3DMGAME\Player\remote\user"
Set "drive=%~dp0Backup"
Set "backupcmd=XCopy /C /D /E /H /I /K /R /Y"
Set "dNow=%DATE%"
Set "tNow=%TIME%"
Set "dateNtime=%dNow:~0,2%-%dNow:~3,2%-%dNow:~6,6%_%tNow:~0,2%-%tNow:~3,2%"
Echo ### Backing up your Sites directory...
%backupcmd% "%SaveGameLocation%" "%drive%\%dateNtime%"
Start /D "%ExeLocation%" MKX.exe

How to copy specific subfolders if existing to new folders in a backup folder using a batch file?

I am a total beginner and I need some help to backup some folders with files from a server.
I need to copy some folders to my local computer.
Folders to copy are like this
\\DistantServer\path\RandomFolder\Config\old
\\DistantServer\path\Another Random Folder\Config\old
...
Those old folders should be copied to:
D:\Backup\RandomFolder\old
D:\Backup\Another Random Folder\old
I tried some codes, but I'm only having errors.
Could anyone help me?
This batch file should work for the task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=\\DistantServer\path"
set "TargetFolder=D:\Backup"
for /D %%I in ("%SourceFolder%\*") do (
if exist "%%I\Config\Old\" (
rd /Q /S "%TargetFolder%\%%~nxI\old" 2>nul
%SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "%TargetFolder%\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
)
)
endlocal
The command FOR searches in source folder as defined with environment variable SourceFolder for non hidden subfolders. Each subfolder found is assigned with full path not ending with a backslash and never enclosed in double quotes to loop variable I referenced with %%I.
The command IF checks for existence of the subfolder Config\old in current subfolder of source folder. The found subfolder is ignored if not containing the subfolder Config\old.
On existence of Config\old in current subfolder of source folder the command RD first removes quietly with all subfolders the appropriate target folder independent on its existence to make sure that the copy done next results in 1:1 folder copy. The RD command can be removed in case of target folder most likely never exists before the copying is done or if it is wanted that existing contents of target folder is merged with current contents of source folder.
%%~nxI references the current subfolder in source folder without path, i.e. without folder path as defined with environment variable SourceFolder.
The command XCOPY copies the entire folder Config\old in current subfolder of source folder to a subfolder old in target folder as defined by environment variable TargetFolder. The entire directory structure to appropriate target folder is automatically created by XCOPY if not already existing because of target specification ends with a backslash being a clear indication for XCOPY that the target string specifies a directory.
Note: The entire code above without command RD could be optimized also to a single command line.
#for /D %%I in ("\\DistantServer\path\*") do #if exist "%%I\Config\Old\" %SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "D:\Backup\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
Single command line with command RD:
#for /D %%I in ("\\DistantServer\path\*") do #if exist "%%I\Config\Old\" rd /Q /S "D:\Backup\%%~nxI\old" 2>nul & %SystemRoot%\System32\xcopy.exe "%%I\Config\Old" "D:\Backup\%%~nxI\old\" /C /E /H /I /K /Q /R /Y >nul
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
if /?
rd /?
set /?
setlocal /?
xcopy /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of >nul and 2>nul. And read answer on Single line with multiple commands using Windows batch file for an explanation of operator & as used in the last batch code (single command line with RD).

how to search a file from multiple path and paste it in other locations using batch file

I need to search a file from two paths either program files or program files x86 and copy the file and paste it in some other location.
Now I have used the script as
#echo off
xcopy C:\"Program Files (x86)"\Adobe\"Adobe Presenter 10.0"\"Gallery"\"interactions"\"Collaboration" %~dp0 /e /h /k /o /y
Echo Files copied!
pause
Appreciate any help on this.
You should fix your quotes. There should only be quotes at the beginning and end of each path. Like this:
"C:\Program Files (x86)\Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration"
You may also want to put quotes around:
"%~dp0"
in case the path has spaces in it.
I'd probably just make a quick, simple script like:
#echo off
if exist "C:\Program Files (x86)\Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration" (
xcopy "C:\Program Files (x86)\Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration" "%~dp0" /e /h /k /o /y
echo copied from Program Files (x86)
)
if exist "C:\Program Files\Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration" (
xcopy "C:\Program Files\Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration" "%~dp0" /e /h /k /o /y
echo copied from Program Files
)
pause
#echo off
rem Define an environment variable with folder name for easier reading and changing in future.
set "MyFolder=Adobe\Adobe Presenter 10.0\Gallery\interactions\Collaboration"
if exist "%ProgramFiles(x86)%\%MyFolder%" (
xcopy "%ProgramFiles(x86)%\%MyFolder%" "%~dp0" /e /h /k /o /y
echo Files copied!
) else (
if exist "%ProgramFiles%\%MyFolder%" (
xcopy "%ProgramFiles%\%MyFolder%" "%~dp0" /e /h /k /o /y
echo Files copied!
)
)
rem Not really necessary here, but good practice is to delete finally the temporary used environment variable.
set MyFolder=
For details about command IF open a command prompt window an enter IF /? to get displayed help about this command.
The enviroment variable ProgramFiles(x86) is by default set in all 64-bit Windows, but does not exist on 32-bit Windows.
The enviroment variable ProgramFiles exists for 32- and 64-bit Windows.
Double quotes must be just used (usually) at beginning and end of a parameter string and the file name of an application with full path containing a space character. The full path to the folder is a parameter. There is no need to use inside a file/path string also double quotes. Some applications could parse them wrong.

Exclude folders in batch-copy-script

I am using a batch file on a USB stick to backup my pictures. I use the following command:
for /r C:\ %%x in (*.jpg *.png *.gif) do #copy /y %%x .
I want to exclude files in the mailfolder WINDOWS and PROGRAM FILES.
Does anyone have an idea how I can do this with a batch file?
Drop COPY and use ROBOCOPY which exists in Windows Vista+ & is downloadable for prior versions.
It supports /XD to exclude specific directories & /XF to exclude file masks at the command line.
E.g.
robocopy.exe c:\ c:\destination\ *.jpg *.png *.gif /xd "Program files" "windows" /S
(Note this will recreate the directory structure in c:\destination\, which thinking about it may not be what you want)
Turn copy into xcopy and then you can use it's /EXCLUDE switch
#xcopy %%x /y /EXCLUDE:\WINDOWS\
See xcopy /? for the details.

batch to copy files with xcopy

I have checked some examples on internet but I can't get my (first) batch file to work. I would like to copy automatically my file from a folder to another one but nothing happen.
#echo off
xcopy "C:\source\" "C:\target\" /c /d /i /y
exit
Could you see anything wrong?
Thanks!!
Update: I have done the command given by Bali C but it still doesn't work. See snapshot
xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y
xcopy C:\folder1 C:\folder2\ /t /e /i /y
Image:
I have to stop it with CTRL + C.
PS: I'm on Win 7
Update (Solution):
It works! The problem was the name xcopy,bat on my Desktop, and I was running the command from there, so it was executing the xcopy.bat file of my desktop instead of the Windows one.. I had to rename the file with "myxcopy.bat" :
#echo off
xcopy "C:\source" "C:\target" /c /d /i /y
exit
After testing most of the switches this worked for me:
xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y
This will copy the folder folder1 into the folder folder2. So the directory tree would look like:
C:
Folder1
Folder2
Folder1
Based on xcopy help, I tried and found that following works perfectly for me (tried on Win 7)
xcopy C:\folder1 C:\folder2\folder1 /E /C /I /Q /G /H /R /K /Y /Z /J
If the requirement is to copy all files in "\Publish\Appfolder" into the parent "\Publish\" folder (inclusive of any subfolders, following works for me)
The switch '/s' allows copying of all subfolders, recursively.
xcopy src\main\Publish\Appfolder\*.* /s src\main\Publish\
You must specify your file in the copy:
xcopy C:\source\myfile.txt C:\target
Or if you want to copy all txt files for example
xcopy C:\source\*.txt C:\target

Resources