Recreate directory structure and copy newest file - batch-file

I have 100+ sub-directories all under the same folder that I'm looking to copy the newest file to backup location with the directory structure intact.
\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
Here's what I have so far, and i can't seem to piece it together. Any help would be greatly appreciated.
#echo off
set source="c:\data"
set dest="n:\backup"
if not exist %dest% md %dest%
cd /d %source%
for /d %%x in ("%source%"/*.*) do (
if not exist "%dest%\%%x" md "%dest%\%%x"
FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & #ECHO %%I COPIED TO "%DEST%\%%X"
)

I would try to do this with robocopy if I were you, because this will most likely be a more robust solution.

Windows' built-in xcopy program provides a flag to include empty directories.
C:\>xcopy "%source%" "%dest%" /E
But, it sounds like you may want to only copy newer/missing files. If that is the case, then #Marged has it right. You should use robocopy.
C:\>robocopy "%source%" "%dest%" /E
Check out robocopy /? for all the details and additional commands.

The metavariable %%x in your for statement is CaSe-SeNsItIvE so you must use %%x throughout the loop, but you are using %%X in the copy statement.
Since you only want to copy the first file, you should append &goto alabelotsideoftheforloop which terminates the for..x.. after the first file has been copied.

Related

Xcopy directory pattern matching

I need to copy source file to destination folder using bat file.
I have created this:
#echo off
set source="D:\Folder1\file.dll"
set destination="D:\Test\TestCopy*\Test\"
xcopy /s /y %source% %destination%
pause
The destination path I have is TestCopy.2.5.3.6. This numbers can change. So specifying TestCopy* is not working. Specifying TestCopy.*.*.*.* also not working.
How can I solve this?
That is not the way xcopy is used; it cannot copy directory structures/files to multiple folders, e.g. copy file random.ext to C:\folder1\test, C:\folder2\test, etc.
Also, no need to use xcopy to copy files. Just use copy instead.
To achieve this, use:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do (
copy %source% "%%~fA\Test\"
)
popd
Or, better one-line for /F:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do copy %source% "%%~fA\Test\"
popd
This is for the situation when you have multiple TestCopy* subfolders:
::It's better to comment out #echo off when you are testing the batch file
::#echo off
::Moved the open quote's position, so the source variable won't have quotes inside.
set "source=D:\Folder1\file.dll"
for /F "delims=" %%i IN ('dir /ad /b "D:\Test\TestCopy*"') do call :fcopy "%%i"
::Comment or remove pause when it's okay.
pause
goto :eof
:fcopy
if not exist "D:\Test\%1\Test\" goto :eof
xcopy "%source%" "D:\Test\%1\Test\"
Used a sub procedure :fcopy, pass each TestCopy* subfolder name to it.
All :: starting lines are comment lines. They are not executed.
I think it's a good habit to quote every path variable when used, but don't include the quotes themselves in the variable -- that's why I moved the quote in the set source line.

batch file to Copy FOLDER with newest time stamp from network drive to local

I need to write a batch file to copy a folder, not file, from a network drive, that has been the most recently updated, to my local machine.
This is what I have, but it cannot find anything to copy because I want to copy a folder, not a file.
for /f "delims=" %%a in ('dir S:\Development\NightlyBuilds /B /A-D /O-D') do copy "%%a" U:\PWJ
Instead of copy, you can use xcopy. Use the /s flag to copy recursively. Make sure to terminate your PWJ folder with a \ to indicate that the destination is a folder. You can also use robocopy. It's a lot more robust. You can look up its parameters in robocopy /?.
You were using the wrong options for FOR and DIR commands, performing a copy for every file in date newest to oldest.
#ECHO OFF
(SET SrcDir=S:\Development\NightlyBuilds)
(SET DstDir=U:\PWJ)
IF NOT EXIST "%SrcDir%\" EXIT/B
IF NOT EXIST "U:\PWJ\" EXIT/B
IF /I NOT "%CD%"=="%SrcDir%" PushD "%SrcDir%"
FOR /F "DELIMS=" %%A IN ('DIR/B/AD-L/OD *') DO SET Latest="%%A"
ROBOCOPY %Latest% "%DstDir%\%Latest%" /E
EXIT/B

Delete everything from Subfolders through Batch file

I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..

Copy only htm not html files batch windows

I created a windows batch file to copy only files with specific extensions into a different folder. Here is the line of code I used:
for /R "%cd%" %%f in (*.htm) do copy "%%f" "%cd%\myfolder"
The issue is that this will copy any extension that starts with .htm, i.e. .html, which I do not want; only .htm. How is this copy prevented?
I've tried
"(*.htm)"
("*.htm")
(*".htm")
(*."htm")
(*.htm*)
Thanks
Solution:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
Thanks #Aacini and #Monacraft
This should work:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
A couple comments unrelated to your problem:
%cd% is a variable that is always replaced by the current folder. If you give any name without a previous path, the name is assumed to be in the current folder. This way name and %cd%\name is exactly the same and the second one is customarily never used.
In for /R [path] %%f ... command, if the path is not given, the current folder is assumed.
You could check using an if statement:
for /R "%cd%" %%f in (*.*) do if /i "%%~xf"==".htm" copy "%%f" "%cd%\myfolder"
And that is the logical way to do this in batch.
Mona
The reason *.htm matches .html files is because of short 8.3 file names. A file with .html extension will have a short name with .htm extension.
Monacraft and Aacini have provided working solutions using IF statemnts within the body of the loop.
Here is a solution that uses DIR /B piped to FINDSTR within a FOR /F IN() clause.
for /f "eol=: delims=" %%F in ('dir /b /s /a-d-h-s *.html ^| findstr /lie .htm') do copy "%%F" "myfolder"
There's probably a better way, but (*.ht?) should do it.
If you are working with a windows vista or later OS, you can use robocopy and exclude not needed files
robocopy "%cd%" "%cd%\myFolder" *.htm /xf *.html

How do I copy file and folder structure using xcopy from a text file?

I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:
"C:\FOLDER"
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"
For a given output directory "C:\OUTPUT" I would like to replicate the entire structure, so:
"C:\OUTPUT\FOLDER"
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"
How can I accomplish this? So far I have written a for loop that reads in each line of the file, but it copies all files if the line is a folder. What I want to do is only copy and create the files and folders that are mentioned in the text file.
#echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
XCOPY /S /E %%a "C:\OUTPUT"
)
Am I on the right track?
Thank you and best regards,
Andrew
Yes, you are close. Just need to use the existing path as the appended destination path.
Update
#echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
md "C:\Output\%%~pA"
copy /y "%%~fA" "C:\Output\%%~pnxA"
)
Original
If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)
The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments
Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.
Post about xcopy prompting issue. and fix #2.
Alternate Setup, since you most likely will not need the xcopy abilities.
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)

Resources