Batch file to copy all new files except the most recent - copy

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

Related

Batch File to copy files to new directory while renaming, skipping existing files, and without confirmation

I am creating a batch file to be run later which will be used to copy files from one location to another while renaming the files, skipping any existing files, and without prompting the user. Examples of files to be copied:
00021001.txt
00021001.xyz
00021001.abc
00021001001.jpg
Copied files will have the names:
00022001.txt
00022001.xyz
00022001.abc
00022001001.jpg
Things I have tried:
xcopy C:\Testing\1000012\21\00021*.* C:\Testing\1000013\22\00022*.* /D
This almost does it. It copies all the files starting with "00021" in the first location into the second location while properly renaming them to start with "00022". It skips all the files with the same name and date stamp, but ends up prompting to copy any files from the source which are newer than the target.
robocopy C:\Testing\1000012\21\ C:\Testing\1000013\22\ 00021*.* /xo /xn /xc
I was hoping that by excluding older, newer, and same date files it would work (even if it doesn't rename - I would just do that in a separate step.) Unfortunately, this just ends up overwriting newer source files over existing target files if they are a different filesize.
I have even tried the Copy-Item command in PowerShell. But it doesn't do the renaming like Xcopy, and it doesn't skip existing files (although I can get it to confirm and say "No to All".)
Copy-Item -Path "C:\CWUImageCompare\Testing\1000012\CWU\chemistry\129\21\00021*.*" -Destination "C:\CWUImageCompare\Testing\1000013\CWU\chemistry\129\20\" -Confirm
If xcopy had the "Skip if existing" flag I'd be all set, but it doesn't.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "skip=1delims=" %%b IN ('xcopy /L /Y "%sourcedir%\00021*.*" "%destdir%\00022*.*" ^|sort') DO (
SET "oname=%%~nxb"
IF EXIST "%destdir%\00022!oname:~5!" (ECHO "%%b" skipped) ELSE (ECHO COPY /y "%%b" "%destdir%\00022!oname:~5!")
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Seems a little complicated, but essentially, execute the xcopy (I omitted the /D for testing) with /L /Y to simply produce a list.
Since the list has a last line that begins with a numeric, whereas the other lines start with a drive-letter, sort the list and skip the first line.
This would implement the date-requirement.
Then grab the part after the first 5 characters of the name+extension, test whether the new name exists and either report or copy as appropriate (The copy command is disarmed for testing)

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

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

Find and replace text between certain characters with filename

Find and replace text between certain characters with filename, have about 100 files that require this so I'm thinking it needs a occurrence or something.
Original file name: test1.txt
Inside file replace the following two lines with the file name (inside each file the below strings might not be right after each other):
location000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
Output in file test1.txt
location000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
#echo off
if not exist "%~dp0jrepl.bat" (
echo ERROR: JREPL.BAT missing in directory "%~dp0".
echo/
pause
goto :EOF
)
for %%I in ("C:\Temp\*.txt") do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "%%I" /O -
This batch file works only on NTFS drives. It can result in an endless running loop on FAT16, FAT32 or ExFAT drives or skipping some text files. For a working solution independent on file system replace the last command line by:
for /F "eol=| delims=" %%I in ('dir "C:\Temp\*.txt" /A-D-H /B /ON 2^>nul') do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "C:\Temp\%%I" /O -
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks first if JREPL.BAT really exists in directory of the batch file and if this is not the case, outputs an error message, halts script execution to make it possible for a user to read that error message and then exits. See Where does GOTO :EOF return to?
The command FOR searches in specified directory C:\Temp for non hidden files matching the wildcard pattern *.txt and calls for each found text file the batch file JREPL.BAT to replace the file name between last / and first string starting with ., having one or more characters not being a dot or a comma with next character being a , (= file extension and comma) on lines starting case-sensitive with location000:/ or newlocation000:/ by the file name of the current *.txt file without file extension. So a file name in existing file can contain also one or more . in file name before file extension.
The solution working also on FAT drives uses command DIR to get a captured list of *.txt files with just file name and file extension without path and FOR processes this file names list line by line, i.e. file name by file name. So the list of *.txt files to process does not change on running FOR calling JREPL.BAT as it would be the case on using FOR directly to find the *.txt files on FAT drives.
NTFS returns a list of directory entries matching a wildcard pattern sorted alphabetically and so the list of *.txt files does not modify during FOR iterations in this case. But all FAT file systems return the list of directory entries matching a wildcard pattern according to last modification in directory with last modified file at end of the list. So while FOR gets one file name after the other on using for %%I in ("C:\Temp\*.txt") do from file system and processes the file with calling jrepl.bat which modifies the file, the file list changes on FAT drives and next directory entry returned to FOR on its search for *.txt is either the file just modified (= endless loop) or another file after skipping a file which should be also modified because of file list changed since last directory access by FOR.
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.
call /? ... explains also %~dp0 ... drive and path of argument 0 being the batch file itself.
dir /?
echo /?
for /?
goto /?
if /?
pause /?
jrepl.bat /?

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``

Batch file for loop to unzip files and get timestamps of zip file contents

I have a daily process which generates some zip files out of files that are being created by other processes. I need to create a daily log file which indicates the timestamps the contents of one specific file of each zip file that is found.
I created the following batch script which seemed to work yesterday on my test system, but not anymore today, no idea why...
set VersionDirectory=C:\Test\VersionX\
set ResultOutputFile=C:\Test\LogFile.txt
for /f %%f in ('dir /b %VersionDirectory%\Installable\Packages\pattern*.zip') do (
mkdir %temp%\%%f\
unzip -extract -dir %VersionDirectory%\Installable\Packages\%%f %temp%\%%f\ > nul
for %%a in (%temp%\%%f\InstallScript.xml) do set InstallScriptXMLDate=%%~ta
rmdir /s /q %temp%\%%f\
echo %%f [package from %InstallScriptXMLDate%] >> %ResultOutputFile%
)
Short summary of what this file is supposed to do:
Loop through each pattern*.zip file in C:\Test\VersionX\ directory
unzip this file to the %temp%\%%f directory (where %%f is the filename)
Get the timestamp of the %temp%\%%f\InstallScript.xml and put it in the %InstallScriptXMLDate% variable
Delete the %temp%\%%f directory
Echo the filename (%%f) and timestamp (%InstallScriptXMLDate%) into the log file
As of now the log file just contains the filenames, followed by the string '[package from ]' string, but missing the actual date timestamp
The unzipping and removing of the zip files is working flawlessly, it's just the timestamp that's not being set.
You are setting a variable and using it in the same block. This cannot work in cmd because environment variables are expanded when a statement is parsed not when it's executed. So when the loop is run all variables have already been replaced with the values they had before the loop.
Put
setlocal enabledelayedexpansion
at the start of your batch and use !InstallScriptXmlDate! instead of %InstallScriptXmlDate%.
Another note: for is perfectly capable of iterating over files by itself, you almost never need to iterate over dir /b output with for /f. In fact, it can introduce problems that can be avoided with
for %%f in (%VersionDirectory%\Installable\Packages\pattern*.zip)