Exclude folders in batch-copy-script - batch-file

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.

Related

Ignore one folder and its contents in batch operation

I've tried to create a batch file that will remove all files and sub-directories from a folder. The command I found that worked is this
FORFILES /p “X:\DAILY\1 MONDAY” /m *.* /c “cmd /c Del /F /Q #path” /d -7 /s & FORFILES /p “X:\DAILY\1 MONDAY” /S /D -7 /C “cmd /c IF #isdir == TRUE rd /S /Q #path”
Now my users tell me there is a Reference folder under 1 MONDAY that they don't want purged (but all other subdirectories should be emptied and deleted.) Can anyone advise how I might accomplish that?
Thank you!
for /f %%a in ("X:\Daily\1 MONDAY\*") do (if %%a neq "<filename you want to save>" (del %%a))
NOT YET TESTED
This should work though. Put the files you want to save in <filename you want to save>
It is extremely inefficient to use FORFILES for such a task because FORFILES has to start cmd.exe for each file found to delete the file older than seven days and for each folder to remove.
A much better solution would be:
#echo off
%SystemRoot%\System32\robocopy.exe "X:\DAILY\1 MONDAY" "X:\DAILY\1 MONDAY\WeeklyDelete" /E /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" /MINAGE:7 /MOVE /NDL /NFL /NJH /NJS
if exist "X:\DAILY\1 MONDAY\WeeklyDelete\" rd /Q /S "X:\DAILY\1 MONDAY\WeeklyDelete"
ROBOCOPY searches
for all files in the directory X:\DAILY\1 MONDAY
and its subdirectories including empty directories because of option /E with
excluding the files in the two directories X:\DAILY\1 MONDAY\WeeklyDelete and X:\DAILY\1 MONDAY\Reference and all their subdirectories because of option /XD "X:\DAILY\1 MONDAY\WeeklyDelete" "X:\DAILY\1 MONDAY\Reference" with
excluding all files last modified in last seven days because of option /MINAGE:7.
The found files and folders matching theses criteria are moved to X:\DAILY\1 MONDAY\WeeklyDelete whereby the destination folder is automatically created on not already existing.
The options /NDL /NFL /NJH /NJS are for not printing the list of moved directories, list of moved files, the header and the summary.
A folder is moved only if being empty after moving all files matched by these criteria. So a folder is not moved on containing a file or subfolder.
The option /S instead of /E and the option /MOV instead of /MOVE can be used to move only files and do not move also folders being empty before or after moving the files.
The file and folder movements are done very fast by ROBOCOPY because of destination folder is on same drive as source folder which means just the file system of this drive must be updated and no file data must be moved at all.
An IF condition is used after ROBOCOPY finished with updating the file system to move the files and folders to verify if the destination folder X:\DAILY\1 MONDAY\WeeklyDelete exists. In this case command RD is used to delete this folder quietly because of option /Q and with all files and subdirectories because of option /S. This action is again just a file system update not really deleting file data stored on storage media and so is processed very fast.
Open a command prompt window and execute there the following commands to read more about the used commands in the three lines above.
echo /?
if /?
rd /?
robocopy /?
The commands are described also
by Microsoft with the documentations for the Windows Commands and
by SS64.com with A-Z index of Windows CMD commands and on many other websites.
Based upon your provided information, I would suggest this as your updated batch file:
#%__AppDir__%forfiles.exe /P "X:\DAILY\1 MONDAY" /C "cmd /C If #IsDir == FALSE 0x28Del /A /F #Path0x29 Else If /I Not #File == 0x22Reference0x22 RD /S /Q #Path" /D -7
That's it, nothing else to be added or modified.

Batch Script robocopy exclude files but not in every folder

I am trying to copy contents of the C:\ drive from one computer to a new computer. I am only trying to copy any user files and not system files. This is my code so far.
#echo off
cls
#echo Type the old Computer Name
set /p asset=
#echo.
#echo What is the user's AIU?
set /p useraiu=
robocopy.exe \\%asset%\c$\ C:\ /S /Z /XJD /XJ /XA:SH /XA:T /XD "Dir1" "Dir2" /XF *.dll *.log *.txt *.exe /log+:"\\server\path\%asset%-to-%computername%-Transfer.log" /NP /FP /V /TEE
I have the exclude Hidden Files, System Files and Temporary Files. I wanted to exclude .dll .log .txt .exe files from the ROOT of C but not from the folders being transferred.
Is it possible to only exculde the files from the Root of C but still transfer them if they exist in the folders?
You could use xcopy
It doesn't copy hidden files or system files. Add the /e parameter to copy empty folders. Add the /c parameter to ignore errors.
xcopy %src% %dest% /exclude:extlist.txt /s
extlist.txt
.dll\
.txt\
.exe\
.log\
.tmp\
EDIT:
Misunderstood your question. Didn't realize you were copying from a networked computer. Xcopy won't work then.

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

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 \.

batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory

So if I have C:\drivers\* with * indicating many sub-folders, I want to find out where my inf files are located, and then copy ALL files that are located in the same directory where my inf files are located and all sub-directories.
It has been easy to create a script that will copy all .inf files found to my directory:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y %%a C:\test
But copying the other files that are located in the same directory and all sub-directories has been difficult.
Such as, if the inf file is located under C:\drivers\sbdrv\hseries\usb30\amdhub\w7 and the sys file is located in the sub-folder of x86, I need the sys file to be kept in the same sub-folder but under the destination of C:\test\x86.
Any ideas?
EDIT: Maybe this will make it easier. As soon as it finds one .inf file in a folder, it should copy the entire folder over to test as well as all sub-folders and move on to the next one. So if it sees the first .inf file located C:\drivers\sb3045\sb407.inf it should copy all files and folders under sb3045 without copying the folder sb3045 itself, and then move on to folder C:\drivers\sb4055\drivers\oem\intel\id6077.inf and copy all files and folders under the intel folder without copying the intel folder itself.
EDIT2:
It looks like this will work, but it is slow as it finds every .inf and is copying over any old files if there is more than one .inf file per folder
#ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION
CD\
CD drivers
FOR /f "tokens=* delims=" %%B IN ('DIR /b /s /o:gen .inf') DO (
XCOPY "%%~dpB.*" "C:\test\" /e /c /h /y
If anyone has a cleaner or quicker idea, let me know please. Until then, I'll have to work with this one.
try this:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y "%%~dpa*" C:\test
for /R C:\WINDOWS\inf %%a in (*.inf*) do (
xcopy /qv /T %%a .\test\
xcopy /qv %%a .\test\
)
pause
this code worked for me.

Copy only newest folder via batch file

I need to copy over only the latest folder from a folder. All the examples I see online are for copying just the newest file. Is it possible to copy over the latest folder via batch file?
CD folder
FOR /F "USEBACKQ tokens=*" %%A IN (`DIR /A:D /O:-D`) DO (
XCOPY "%%A" "new folder" /E /H /K /Y
GOTO:OUT
)
:OUT
Change directory to the folder in question. Use a FOR loop over the listing of all folders in current folder, arrange them by date with the newest on top, then copy all the contents of that folder to the new destination (/E Copies all directories and subdirectories even if empty, /H include all hidden and system files, /K keeps attribute settings, /Y suppresses the prompt to copy), then leave the loop after the first iteration (i.e. newest file)
Have you tried Robocopy?
Edit to address downvote
robocopy .\src .\dest /e /xo /dcopy:T
Options
/xo
Excludes older files.
/e
Copies subdirectories. Note that this option includes empty directories.
/dcopy:T
Copies directory time stamps.

Resources