Copy list of file names from multiple directories - list

i'm new to the forum. I need some advice. I need help with a code to take a text file with a list of file names and copy the files from multiple folders in to one folder, but it has to end up with the most recent modified date of the file. There are hundreds of folders containing the thousands of files.
I have a batch code below to copy the files, but it does not always end up with the newest file. If there is some way to make the code below search the folders in numerical order, that would work instead of it searching in a random order. I do not care if the code i end up using is batch, vbs, or whatever.
mkdir %userprofile%\desktop\print
set FIILELIST=%userprofile%\desktop\print.txt
set FILESPATH="\\server\folder"
set DESTPATH=%userprofile%\desktop\print
for /f %%X in (%FIILELIST%) do call :COPY_FILES "%%X"
goto :eof
:COPY_FILES
for /r %FILESPATH% %%I in (%~1) do copy "%%I" "%DESTPATH%"

for /r %FILESPATH% %%I in (%~1) do Xcopy /D "%%I" "%DESTPATH%"
XCOPY /D will only copy if the destination file does not exist or if the destination file is older than the source.

Related

I'm looking for a batch file to delete jpg's from a .txt file list

I'm looking for a batch file to delete jpg's from a .txt file list
E:\test\1 has 7 images 1.jpg to 7.jpg
list.txt has list of files to be deleted 1.jpg, 2.jpg
Thanks for your help
I found code that will copy the .jpgs in my list.txt to another folder.
Instead of copying I need it to delete,
#echo off
set src_folder=E:\test\1
set dst_folder=E:\test\2
set file_list=E:\test\list.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
I found this solution using excel
column A del
column B list of images
column C Concatenate(A1,B1) and copy and paste column C in a batch file run in same directory as images.
This should do it:
#Echo Off
(Set file_list=E:\test\list.txt)
For /F "UseBackQ Delims=" %%A In ("%file_list%") Do If Exist "%%~A" Del "%%~A"
You could even drag and drop your list.txt onto this single line batch script.
#For /F "UseBackQ Delims=" %%A In ("%~1") Do #(If Exist "%%~A" Del "%%~A")
This can be done with a single line batch file:
#for /F "usebackq delims=" %%I in ("E:\test\list.txt") do #del "%%~I" 2>nul
The command FOR processes each line from the specified text file and executes command DEL with the line read from the file.
No file existence check is made before deleting the file. Instead the file is deleted with error message suppressed by redirecting it from handle STDERR to the device NUL.
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.
del /?
for /?
Read also the Microsoft article Using command redirection operators.

Postbuild Event Copy build output to parent sibling undertermined folder

I have two solutions, WildCougarFarm and WildLionFarm both depending on a shared library
\Folder 1
\WildCougarFarm
\WildSharedLib (Separate Solution)
\Folder 2
\WildLionFarm
\WildSharedLib (Separate Solution)
When \WildSharedLib is built I want to run a post build script that automatically copies the output directories contents to the sibling \Wild[Lion|Cougar]Farm solution folder. Depending on whether WildLionFarm exists, I want it to copy with say wildHorseFarm in future.
I need an xcopy expression with a regex but xcopy doesn't support this.
Any ideas how I can accomplish this?
I figured it out.
This is pretty cool, because it uses a foreach type loop... so we ask if the parent folder of the solution contains any folders that end in farm. For any returned it will execute the xcopy statement to copy the files to that folder.
for /d %%a in ($(SolutionDir)..\*farm) do xcopy $(TargetDir)*.* "%%a\dependancies" /s /e /f /h /k /y /i

Move files based on filename with regex and delete files with lower number in a substring in the filename

I have files with the following syntax:
LWD_???_??????_???_??_??_LP?_??_?_*.PDF
Example:
LWD_ARC_G10000_102_UE_XX_LP5_08_E_Uebersicht_Bodenplatten
I want to extract substrings out of the file name and put the file into a folder with the path based on that file name like this:
C:\Lp5\ARC\G10\
First folder is the 7th part of the file name, the 2nd part is the second folder and the first 3 chars of the 3rd part is the last folder.
Then in addition to that I need an extra delete: When the files are copied to the specific folder there is a consecutively numbered part in the file name. I need the "older" files deleted so that only the "last" file is in this folder. The numbers/index is always the 30th and the 31st char.
LWD_FEU_L20000_005_IZ_00_LP8_XX_F.pdf Index 00
LWD_FEU_L20000_005_IZ_00_LP8_01_F.pdf Index 01
For now I only have a batch with static folders:
FOR /R "E:\Downloads" %%i in (LWD_ELT_A10?00_???_??_??_LP5*) do move "%%i" "C:\Lp5\ELT\A10"
FOR /R "E:\Downloads" %%i in (LWD_???_A10000_???_??_??_LP5*) do del "%%i"
...
Does anyone have an idea how to do that without VBS or sth. like that - only Windows Batch or PowerShell?
My batch strategy is as follows:
1) get list of PDF files using DIR /B
2) parse each file name into a string consisting of (pipe delimited)
file mask that matches name with ?? wildcard for positions 30,31
destination path
full file name
3) sort the strings descending, resulting in the most recent version at the top of each file name grouping.
4) process the output with FOR /F, parsing out the file mask, destination path, and full name
5) for each iteration, create the destination folder if it does not already exist, and then conditionally copy the file to the destination if there does not yet exist a file that matches the file mask.
The above strategy is non-destructive, as the original files all remain in place. You could modify step 5 to be destructive - move the newest files instead of copy, and delete the rest.
You could implement the above strategy fairly easily with pure batch. But I would use my REPL.BAT utility - a hybrid JScript/batch script that can efficiently perform sophisticated regular expression replacements. JREPL.BAT is pure script that runs natively on any Windows machine from XP onward.
The following are untested
Non-destructive version
#echo off
for /f "tokens=1,2,3 delims=|" %%A in (
'dir /b /a-d LWD_*.PDF ^| jrepl "^(LWD_(...)_(...)..._..._.._.._(LP.)_)..(_._.*\.PDF)$" "$1??$5|c:\$4\$2\$3|$&" /i /a ^| sort /r'
) do (
md "%%B" >nul 2>nul
if not exist "%%B\%%A" copy "%%C" "%%B" >nul
)
Destructive version
#echo off
for /f "tokens=1,2,3 delims=|" %%A in (
'dir /b /a-d LWD_*.PDF ^| jrepl "^(LWD_(...)_(...)..._..._.._.._(LP.)_)..(_._.*\.PDF)$" "$1??$5|c:\$4\$2\$3|$&" /i /a ^| sort /r'
) do (
md "%%B" >nul 2>nul
if not exist "%%B\%%A" (move "%%C" "%%B" >nul) else del "%%C"
)

Batch: Create a list of folders and then copy multiple files/folders into all of them

I am a CAD manager in need of some help. I am no programmer, but I have a very basic understanding of DOS commands so I thought I could use XCOPY to do this, but I don't know how.
I have a folder, under which are hundreds of subfolders that change with time (each employee has one folder) so maintaining a manual listing (as I have been doing) is not easy.
I also have a _TEMPLATE subfolder that contains several files and folders.
I would like to copy the contents of the _TEMPLATE folder into all the other subfolders, overwriting all existing files and creating all non-existing folders.
From what I've seen with some searching in here it seems to pretty easy to accomplish these two tasks (to create the list of folders and to copy into a list of folders) but I'm just too dumb to understand how to combine the two into one file :)
EDIT :
Well, I've found a PowerShell script that does what I need perfectly. Since I managed to run the PowerShell I guess my problem is solved :D
$folder = Get-Childitem -name -exclude *.*,_TEMPLATE
foreach ($f in $folder)
{xcopy "_TEMPLATE\*.*" "$f" /e /r /y }
Thanks for all the fish ;)
Try this:
#echo off &setlocal
cd /d "RootFolder=X:\folder\to\my data"
for /d %%a in (*) do xcopy "_TEMPLATE" "%%~fa" /sihrky
For xcopy options see xcopy /?, to test the command without writing add option /l.

Copy Directory - Post Build Event

How do I copy some directory from one place to another (not file by file)
in post build event (whats the comman line??). im using vs 2005 (c++ project)
For more clarification, here is an example that copies a folder called "ApplicationFiles" from the root of your project to the destination (binary) folder:
xcopy "$(ProjectDir)ApplicationFiles" "$(TargetDir)ApplicationFiles" /e /y /i /r
Thanks, just what I needed. Options documented here for future reference:
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
/R Overwrites read-only files.
The commandline is simply a batch script that is executed upon completion of the build. Therefore, you can just use regular Windows shell commands, such as mkdir, copy, ... To copy whole directories recursively, use xcopy <src> <dest> /E.