BAT File to find most recent files based on filename - batch-file

I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target

So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.

Related

how to copy only modified files using LogFile.txt?

when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.

Search all subfolders for a foldername that has a specific string

I need a batch file that will search all subfolders in a folder, find any folder named Reports (there will be many and file paths will be changing constantly) and then copy the reports folders contents (not the folder) back to a root directory for export. For example: I have a folder on my desktop called Cases. I need to search all sub folders for folders called Reports and then copy those files out.
Any help would be appreciated.
Here is a way you can do it (assuming you are talking about Cases folder in your desktop [in cmd]):
for /R "%userprofile%\Desktop\Cases" /D %A in (Reports.?) do #xcopy /s %~fA full_path_you_want
For batch file you should double the percent-signs (%%) just like this:
#echo off
for /R "%userprofile%\Desktop\Cases" /D %%A in (Reports.?) do xcopy /s %%~fA full_path_you_want
To learn more about commands and wildcards (.?) you can:
Type for /? in a fresh cmd
Check link https://www.robvanderwoude.com/battech_wildcards.php
Hope this helps!

How to differentiate between system and user files in the batch language?

I am looking to copy all text files on the C drive, but the program needs to avoid text files that are part of the system, and only copy the text files that have been created by the user.
Is there a built in way to do this, or do I need to get creative? If so, how would you go about it? I would like to keep it within batch and not involve powershell or anything like that.
I thought about using creation dates to determine whether or not a file is a system file, but that didn't work all the way.
You can generate a list of all systemfiles using the dir command. So
dir /b /as > %USERPROFILE%\excluded_Files.txt
Would generate a list of the systemfiles from that directory.
Using xcopy as described here you can now copy all files from one directory to another excluding the systemfiles.
The xcopy command should be something like this:
xcopy C:\firstDirectory\*.txt C:\secondDirectory /EXCLUDE:%USERPROFILE%\excluded_Files.txt
Edit
So your code that is there would copy all rtf and txt files from the whole C drive to Drive D folder E.
A batch-file using my method would look like this:
#echo off
cd /d C:\
dir /b /as /s > %USERPROFILE%\excluded_Files.txt
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
xcopy C:\*.txt D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
Explanation:
#echo off supresses redundant output of the commands that would be displayed before execution.
cd /d C:\ changes the current execution directory to the root of the C drive. /d is added for potential drive change.
dir /b /as /s > %USERPROFILE%\excluded_Files.txt redirects the output of the dir command into a file in your userprofile (Usually C:\Users\yourUserName). The switch /b will only show the filenames and not size, creation date etc. /as will only list system files and /s makes the dir command work recursively through all directories.
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s will copy all rtf files from C:\ to D:\E and exclude all files from the list previously created (that will contain all system txt and rtf files (as well as others)). /s makes xcopy work recursively as for dir.
Next line is a copy of the same with txt files.

how to copy files from one directory to other in batch command

I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"

Copying a Folder and renaming it using command prompt

I am trying to copy a folder and paste it in the same directory it was copied from.
For example
C:\Test is the main directory which consists of a folder ACDM, I would like to copy ACDM in the same directory and rename the new folder to ACDM1 which will have all the same files as ACDM has
I would like to do it using command prompt
I tried the following
C:>Xcopy C:\Test C:\Test\ACDM1 /E /U
Cannot perform a cyclic copy
0 File(s) copied
which fails, not sure hoe to add REN command with XCOPY command.
Need help ASAP as i would want to create a batch file which will create a copy of an existing folder and rename it according to a name retrieved from a text file..
xcopy "C:\Test\ACDM\*.*" "C:\Test\ACDM1\" /s/h/e/k/f/c
for /f "delims=" %%a in (yourtextfilename) do xcopy "C:\Test\ACDM" "C:\Test\%%a\" /E
as a .bat file line. Directly from the prompt, change each %% to %
I've assumed (for lack of futher information) that your textfile contains just the one line
ACDM1
neither do you specify the textfilename tou want to use.
xcopy C:\Test\ACDM C:\Test\ACDM1\ /E /Q

Resources