Copy only newest folder via batch file - 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.

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.

Xcopy - Trying to copy a folder with certain arguments to another location

I'm trying to xcopy certain folders from a directory of 5000 folders, that start with the number 9097*
Approx 2000 folders start with 9097.
for /f %%A in ('DIR /b /a:D') do (
set temp=%%A
set ID=!temp:~0,4!
if !ID!==9097 xcopy *!ID!* F:\%INPUT% /S /i
My problem is that when it finds the first folder starting with 9097, the code then goes and copies all contents of all the folders, I want it to only copy the 9097 folders. I understand why the code above is doing what it is doing but I can't seem to think of the way for it to just copy the folders and contents starting with 9097.
Any advice would be greatly appreciated.
for /d %%a in (9097*) do xcopy "%%~fa" "f:\%input%\%%~nxa" /s /i
For each folder with a name starting with 9097, xcopy from the full path of the source folder to the target inside a folder with the name and extension of the source folder.

How can I batch copy certain files from multiple folders into multiple folders?

I am looking to copy files that are like "MAN" that reside inside multiple folders in one directory into all the folders in another directory.
This is what I have now, it copies the files correctly but not into all the folders inside the directory. just into the directory itself.
for /d %%a in ("C:\test123\*") do #copy "%%~Fa\MAN*" /d "c:\Test Destination\*" 2>NUL
to reiterate the problem, the files are copied into c:\Test Destination* but I want it to copy to every folder in that folder. It would be great if the file already exists there, to not copy it at all.
Thanks!
This may work, but is untested. Test it on some sample files.
#echo off
for /r "C:\test123" %%a in (man*) do (
pushd "c:\Test Destination"
for /d /r %%b in (*) do if not exist "%%b\%%~nxa" copy "%%a" "%%b"
popd
)
pause

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.

Resources