Copying files from similar multiple folders into each of their child folders - robocopy

I would like to use a batch file to copy files in a folder to a backup folder (OLD) within, on a Windows server. And I want to do this for multiple folders.
So for example, I have multiple folders, named like this:
C:\A01
C:\A02
C:\A03
...
C:\A50
I will nickname them here as A##.
Each folder has hundreds of files.
Then each year before updating them, I copy those files into a backup folder called OLD, for each A##, so I have a backup of last year's files. Then I copy in new files into C:\A##, in another manual process, individually.
So there will be folders like:
C:\A01\OLD
C:\A02\OLD
C:\A03\OLD
...
C:\A50\OLD
When I do the copy, to the OLD folder, I keep the most recent version of each file. But then the files that I later manually copy in will overwrite in the C:\A## folder and be this year's files, while C:\A##\OLD will contain last year's files.
How can I script this so it will copy all the files for each C:\A## folder into their respective \OLD folder?
I assume I would use a variable for the path I want to copy from and to.
I could use Robocopy or Xcopy.

in a script file
#echo off
cd c:\
for /f "usebackq delims=:" %%a IN (`dir /AD /B ^| FINDSTR /I /R "A[0-9]*"`) DO (
#mkdir "%%a\OLD"
xcopy "%%a\*.*" "%%a\OLD\" /ECIFHRY /D
)
it copy only the updated files (/D) to the OLD subfolder.
if you want to mantain ACLs of the files, use /ECIFHRKOXY
I suggest you to test it in a test folder, that you can generate with this commands directly in the shell
#mkdir c:\test76
cd c:\test76
For /L %a IN (1,1,10) DO #mkdir A%a && echo "the quick brown fox jumps over the lazy dog">A%a\file.txt

Related

Move files from multiple folders to a single folder without extension

i am a windows user and i need help with something
i have a folder and in it it has multiple folder.and in those multiple folders i have more folders and in those folder i have files without any extension like jpg,txt etc
here is a sample
D:\test\1\something1\1235486[file]
D:\test\1\something2\1235486[file]
D:\test\2\something1\1235486[file]
D:\test\2\something2\1235486[file]
so now i want to move all those random number files i.e 123456789[these files are without any extension] to D:\newtest
so i will have files like
D:\newtest\123456789
D:\newtest\123456789
D:\newtest\123456789
D:\newtest\123456789
etc. i could have easily done this with search option if it had a extension.but what can i do now?
Finally Found a solution for this problem
First make another folder where you want to transfer file
lets say i have a folder named ABC and i want to transfer its files to XYZ [only files not folders]
then open Command prompt [cmd]
and enter this
for /f "tokens=*" %a in ('dir /b /s /a-d "d:\abc"') do #copy "%a" "d:\xyz
Change the folder names and hit enter.it will transfer all the files in it

How to Copy files that are in a directory to another directory recursively in Windows?

I have to create an script to copy files from a folder structure to other.
My source folder structure is similar to this:
-RootFolder
--ParentFolder1
--SubParentFolder1
--ToCopy
/*Here are the files to copy*/
--SubParentFolder2
--ParentFolder2
--OtherSubParentFolder
--ToCopy
/*Here are the files to copy*/
--ParentFolder3
--OtherSubParentFolder2
I want to copy the files that are in the "ToCopy" folders, into another folder, with this structure:
Destination folder structure:
--TargetDirectory
--SubParentFolder1
//Here the files that were in the ToCopy folder inside the SubParentsFolder1
--OtherSubParentFolder
//Here the files that were in the ToCopy folder inside the OtherSubParentFolder
Notice that I use the name of the "ToCopy" parent folder in the destination subfolders.
I know how I would do this with code (like C#), but I am at a lost on how to achieve it with a Batch file. Is it even possible? Or I would need to use something like powershell?
How can I copy my files following the structure I described?
I think, this should work...
$Folder= gci -path "d:\pstest" -recurse -Filter "ToCopy" | where { $_.psiscontainer }
Foreach ($Foldername in $Folder) {
$Destinationfolder=$Foldername.Parent
copy-item $Foldername.fullname -Destination "d:\Outputfolder\$Destinationfolder" -recurse
}
Hi to follow is a script I hacked away (via help from stack overflow), that reads the files from a txt document, then requests destination folder input and also src folder name it then just goes and recursively copies all the files to the new folder without keeping the old subfolder structure.
I will update this in future with the link to the person that I got the base template from for the admin area, but to keep in mind once you click that Batch can run as though it was a php script then everything makes sense. Took me whole day to research every command and alternative on SS64.com
Major thing to note is the pushd "%~dp0" this I use to make sure batch always uses my current directory as root.
As said I will do a proper write up on this and further stream lining since I am using it actively for moving files during a woocommerce shop update. P.S. the text file name should be entered without the .txt extention and every file name should start on a new line. Also if the destination directory does not exist it will create it. Use excel maybe to list the names then for renaming could output to new column and compile the batch rename command copy to new batch run first batch to fetch files and second batch to rename to preferred title, I do it in steps to keep my sanity.
Sorry was just a example of how I use it, but yes go ahead and enjoy hope this works for you.
#echo off
CLS
setlocal EnableDelayedExpansion
REM Changes root path to relative of current bat folder
pushd "%~dp0"
REM finds files in provided .txt file and copies them to destination directory
REM CHECK FOR ADMIN RIGHTS
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
IF ERRORLEVEL 1 GOTO:NONADMIN
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
:ADMIN
REM GOT ADMIN RIGHTS
COLOR 1F
ECHO Hi, %USERNAME%!
ECHO Please wait...
set /p DEST_DIR="Copy files to:"%=%
set /p SEARCH_DIR="Copy files from:"%=%
#echo.
#echo Please check folder name for accuracy.
#echo Copy files to: %DEST_DIR%
#echo Copy files from: %SEARCH_DIR%
set /p CORRECT_FOLDERS="Are these correct? (please check spelling) y/n:"
if '%CORRECT_FOLDERS%'=='y' GOTO:YES_ANSWER
if '%CORRECT_FOLDERS%'=='n' GOTO:NO_ANSWER
COLOR 2F
ECHO.
PAUSE
GOTO:EOF
:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF
:YES_ANSWER
#echo.
#echo you answered yes
#echo.
if exist %DEST_DIR% GOTO:READ_DATA
if not exist %DEST_DIR% md %DEST_DIR%&GOTO:READ_DATA
PAUSE
:NO_ANSWER
#echo.
#echo you answered no
set /p TRY_AGAIN="Try again? y/n:"
if '%TRY_AGAIN%'=='y' GOTO:YES_ANSWER
if '%TRY_AGAIN%'=='n' GOTO:EXIT_PROGRAM
PAUSE
:EXIT_PROGRAM
#echo.
#echo "So Sorry"
PAUSE
GOTO:EOF
:READ_DATA
#echo.
set /p GET_FILENAMES="What is the name of the text file your filenames are stored in?"%=%
if exist %GET_FILENAMES%.txt #echo We will now read and copy the files for you, have some coffee might take awhile & GOTO:WRITE_DATA
if not exist %GET_FILENAMES%.txt #echo Filename does not match, please type only the name without .txt extention & GOTO:READ_DATA
PAUSE
:WRITE_DATA
#echo.
#echo reading file name...
for /f "usebackq delims=" %%a in ("%GET_FILENAMES%.txt") do (
for /r "%SEARCH_DIR%" %%b in ("%%a*") do (
#echo Copy Started...
copy "%%b" "%DEST_DIR%\%%~nxb"
)
)
#echo Copy finished, please review actions. Lekker Man.
PAUSE``

Copy Files Created Today from Multiple Directories into a Single Folder

I'm trying to copy files from multiple subfolders into a single folder without re-creating the subfolders in the destination folder. I have a command that will do this, however, the problem that I have is this is something that I need to run daily as new files are created. So I don't want to re-copy the files from the previous day. I only need to copy files that are created on the current day. The command that I am using is:
for /f "tokens=*" %a in ('dir /b /s /a-d "e:\testfrom" ') do #copy "%a" "e:\testto"
This does copy everything that is in the testfrom folder and puts it into the testto folder.
How can I get this to copy files in the testfrom folder that are created on the current day?
Thanks!!

MSDOS command(s) to copy files matching pattern in directory structure to local directory

I have a job that periodically runs and archives files into a folder structure that looks like this:
ArchiveFolder
TimestampFolder
JobnameFolder
job<timestamp>.xml
For a given job, I'd like to collect all xml files in the archive folder into a flat directory (no subdirectories, just all the files) without having to drill down into each one, examine for the proper job, then copy the file.
It seems there should be a fairly straigtforward way of doing this. Any suggestions?
EDIT: I guess I wasn't clear here. The TimeStampFolder will have a name of something like 2011-07-24, the JobnameFolder will have a name like FooFeed or BarFeed, and the job file will have a name like job2011-07-24.xml. There are hundreds to thousands of TimeStampFolders, and each one may have one or more job folders in it. Given a specific job name, I want to collect all the files in all the directories that match that job type, and dump them into the local folder, with no subdirectories.
EDIT1:
SET JOB=JobName
SET OF=OutputFolder
START /wait NET USE Z: "\\ServerName\Sharename\ArchiveFolder" password password_here /USER:domainname\username /P:NO
PUSHD Z:\
FOR /F "USEBACKQ tokens=*" %%A IN (`DIR /b /a:d /s ^| FIND /I "%JOB%"`) DO (
FOR /R %%F IN (%%A) DO (
COPY /Y "%%~fF" "%OF%"
)
)
POPD
It basically locates each subdirectory of ArchiveFolder that includes the JobName in it, then digs into each one that it finds to copy the files out of them.
EDIT2:
Added NET USE to access your network share to perform tasks on the files. If your local machine already has the UNC assigned to a driveletter, you can remove the NET USE command line and change Z: to the assigned driveletter.
#ECHO OFF
FOR /R %%v IN (job*.xml) DO COPY "%%v" c:\out\

Batch file to copy all new files except the most recent

I would like to write a batch file containing DOS commands (unfortunately perl or another language is not an option) to do the following task.
In one directory (c:\MyData\Directory1) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin
FileC.bin
File8.bin
File2.bin
These files all have different creation dates. The most recently created *.bin file is File2.bin in this example, but it could be any randomly named *.bin file.
In another directory (c:\MyData\Directory2) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin
Here is what I want to do:
Copy all files with the extension *.bin in Directory1 that do not already exist in Directory2 except for the most recently created *.bin file in Directory1. So the only files that should be copied into Directory2 are:
FileC.bin - Copy because it's a bin file that's not yet in Directory2
File8.bin - Copy because it's a bin file that's not yet in Directory2
The following files should not be copied into Directory2:
File2.txt - Wrong extension so don't copy it
File2.dat - Wrong extension so don't copy it
FileA.bin - Already exists in Directory2 so don't copy it
FileQ.bin - Already exists in Directory2 so don't copy it
File2.bin - Most recent *.bin file so don't copy it
Thanks for any help!
#echo off
#rem Sorry for excessive commenting - I am a batch file newbie
#rem Batch file will not work if there are spaces in names of directory or copied files
#rem Next line allows for/do loop to work correctly
setlocal enabledelayedexpansion
#rem Make temporary file that lists files from newest to oldest
DIR /o-d /b c:\temp\Directory1\*.bin > FileList.txt
#rem Counter will be used to avoid copying newest file which is listed first
set /A Counter=1
#rem Read in names of all files with chosen extension in the first directory
#rem Names will be stored in the variable %%a
for /F "delims=" %%a in (C:\temp\FileList.txt) do (
#rem Increment the counter
set /A Counter+=1
#rem Only copy files that are not the most recent one, so Counter>1
#rem Requires the exclamation points because this is a string not number comparison
if !Counter! gtr 1 (
#rem If the file does not already exist in Directory2, copy it
if not exist C:\temp\Directory2\%%a (
echo Copying C:\temp\Directory1\%%a to C:\temp\Directory2\%%a
copy C:\temp\Directory1\%%a C:\temp\Directory2\%%a
)
)
)
#rem Remove the temporary file
del FileList.txt
You can use DIR *.bin /o-d /b > Files.txt to get a list of bin files ordered most recent to last. Do this on both folders (to separate output files), and then set up a FOR loop, maybe two nested FOR loops, to go through the two files, pick out the ones to copy (with special handling for the first one in the date-ordered list), and copy them from within the loop. Silly tricks would be done with setting the Attribute setting and then using XCOPY /M to copy them all at the same time, but that seems overly fussy.
I've always found FOR loops to be cantankerous beasts, and if you can find a non-batch-file way, or some form of third-party plug in to help, you'd be ahead of the game.
I don't have Robocopy on my machine, otherwise I would do a /? and tell you. But as I recall it has many more capabilities (especially wrt timestamps). It's a windows tool.
http://en.wikipedia.org/wiki/Robocopy