How do I find last updated files since 1 week? - windows

I have a directory and there are about ~1000 files in there.
I want to get the file names which last updated since 7 days? And write this file names to another file.
I'm using windows 2012 and I want to do that with batch script. How may I do that?
UPDATE:
I tried
#echo off
setlocal enableextensions disabledelayedexpansion
set "folder=c:\some\where"
( for /f "tokens=*" %%a in ('
robocopy "%folder%" "%folder%" * /l /nocopy /is /maxage:7 /njh /njs /nc /ns /ndl
') do echo(%%a
) > outputFile.txt
However, I receive this error:
ERROR : No Destination Directory Specified.
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !

#echo off
setlocal enableextensions disabledelayedexpansion
set "folder=c:\some\where"
( for /f "tokens=*" %%a in ('
robocopy "%folder%" "%folder%" * /l /nocopy /is /maxage:7 /njh /njs /nc /ns /ndl
') do echo(%%a
) > outputFile.txt
This uses robocopy command to retrieve the list of the required files. It will only list /l, without copying anything /nocopy, all the files, including those considered the same file /is, with a max age of 7 days /maxage:7, without headers /njh, summary /njs, file class /nc, size /ns or directory listing /ndl.
The output of the command will include some blank columns that are removed with the for loop.

Related

Windows Batch Script- for /D delims Executing Multiple times

I am trying to set up a batch backup script on Win7. It is mostly working, but I have a line of code executing multiple times. I've done some searching around and I think it has to do with how batch files handle code in "( )" but I'm not sure the best way to fix it.
Any help would be awesome!
Script:
for /D %%d in (C:\toBackupDir\*.*) do (
for %%f in (C:\zipDir\*) do (
for /f "tokens=1 delims=_" %%i in ("%%~nf") do (
if "%%i"=="%%~nd" (
if "%%~tf"=="%%~td" (
echo "%%~nf" is "%%~nd" No Backup necessary.
) else (
7z.exe a -tzip -stl -mx=1 "C:\zipDir\%%~nd_%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%.zip" "%%d"
)
)
)
)
)
The line "7z.exe" is executed many times if "%%i"=="%%~nd" is true. My thought is that last for loop to split the file name is being executed many times, but once it gets %i and does the comparisons, I'm done with it. Can I break out of the loop once the "7z.exe" line is executed? I've read that breaking out in a batch file is tricky.
Let me know if you need a breakdown of the code.
Thank you in advance!
You also test every directory against every zip file.
These are more than 60000 compares with 250 directories and the matching zips. But this is not necessary at all. Then you can not against the time of the folder Compare - it does not update itself. The output from the DIR is therefore not at all suitable.
In addition - what is already with the existing zip fuses, these are also compared each time with. So with two backups of a folder, your batch would always make a new backup.
Your simple loop, which searches for the zipfiles, is practically not finished, because always new files are entered in the MFT and the loop this after the creation issues.
I've taken robocopy to list the files, because this time stamp is very well suited for comparison.
First, only the folder is checked and with it the appropriate zip file. The folder is checked recursively for the files as well as the appropriate zip file. The time stamps are placed in a list and sorted. The loop sets the last value so, if the last (ie the newest) file comes from the folder of the zips does not need a backup.
A suitable time stamp for the file name is also generated.
You may need to adjust the paths.
#echo off
setlocal
set "Folder=d:\toBackup"
set "Backup=d:\zipdir"
call :TAB
set "TS=."
for /d %%F in ("D:\files\*")do (
set "TsFn="
set "tozip="
for /f "tokens=2,3delims=%TAB%" %%A in ('
( robocopy /L "%backup%" ".. only listing ..\\" "%%~nF_*.zip" /njh /fp /ts /ns /nc /ndl /njs ^
^& robocopy /L /e "%%F" ".. List only ..\\" /njh /fp /ts /ns /nc /ndl /njs ^)^|sort
')do (
set "TsFn=%%A*%%~dpB"
if /i "%%~dpB"=="%Backup%\" ( set "tozip="
)else set "tozip=1"
)
if defined tozip call :timestamp
if defined TsFn (
setlocal enabledelayedexpansion
for /f "tokens=1-3delims=*" %%S in ("!TS!*!TsFn!")do (
endlocal
if NOT defined tozip ( echo %%T %%~nxF - No Backup necessary.
)else echo 7z.exe a -tzip -stl -mx=1 "%Backup%\%%~nF_%%S.zip" "%%F"
)
)
)
pause
exit /b
:TAB
for /f "delims= " %%T in ('robocopy /L . . /njh /njs') do set "TAB=%%T"
rem END TAB
exit /b
:Timestamp
rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L /njh "\|" .^|find "123" ') do (
set "TS=%%T%%U%%V-%%W%%X%%Y"
set "TSDATE=%%T%%U%%V"
set /a YY=%%T , MM=100%%U %%100 , TT=100%%V %%100
)
rem END Timestamp
exit /b
If there is not yet a zip file from the matching folder is available, of course, a zip is created.
A complete backup program
robocopy backup cmd

BATCH - Mirrow two Folders in both Ways

I safe some Files at my Local Drive(Laptop) and also have Files saved at my Network Storage at Work.
I want that both Folders have the same Files, If I create/delete or change an File in one Folder, the other Folder should get Updated when I use the BATCH.
It should use the newest Version on an Document.
So I tried it with ROBOCOPY Folder01 Folder02 /MIR /R:3 /W.20, it worked but only in one Way, from Folder01 to Folder02, so if I created an File in Folder02 and used the Batch, the File got deleted.
Then I tried to copy both Folders into one TEMP Folder and then copy the TEMP-Files to both Folders. I used
ROBOCOPY Folder01 TEMP /XO /E /R:3 /W:20
ROBOCOPY Folder02 TEMP /XO /E /R:3 /W:20
ROBOCOPY TEMP Folder01 /MIR /R:3 /W.20
ROBOCOPY TEMP Folder02 /MIR /R:3 /W.20
this was almost perfect, always the newest Document was used and everything was there, but when I delete a File from Folder01, which still exists in Folder02, it will come back next time I use my BATCH.
Sorry for my English
Greetings, Tobias
Try this solution provided by #Sachadee with Xcopy
:://Synchro.bat
:://SachaDee 2014
#echo off&cls
:: We set Folders to synchonized
set "Folders= C:\HackooTest E:\Backup\Folder1 E:\Backup\Folder2 E:\Backup\Folder3"
for %%a in (%Folders%) do (
for %%b in (%Folders%) do (
if not "%%a"=="%%b" (
set "VAR%%a%%b=%%a %%b"
)
)
)
for /f "tokens=2,3 delims== " %%a in ('set VAR') Do (
echo xcopy "%%a" "%%b" /E /D /C /Y /I
)
pause

batch file to count all files in a folder and subfolder files and write output file in folder name using batch file

How to count files in a folder and subfolder files also. folder name and file count and sub folder name and file count.
Ex:
test1 : 2
test1\test11 :3
newfolder : 5
newfolder : 10
like this.
robocopy "x:\target\folder" "x:\target\folder" /l /nocopy /is /e /nfl /njh /njs
or
pushd "x:\target\folder" && (
robocopy . . /l /nocopy /is /e /nfl /njh /njs
popd
)
This will simply call robocopy, but instead of copying anything, we will request that nothing will be copied and only return the list of what should be processed (/nocopy /l). We request to copy from the current folder (a previous pushd command is used) to the current folder, including identical files in the process (/is), processing empty subfolder (/e, to include folders with 0 files), but only process two levels (/lev:2 the current folder and the one below), without generating a file list (/nfl), no job header (/njh) and no job summary (/njs)
The result will be the same list but with the folder name and the number of files in changed columns
To keep the original output format
#echo off
setlocal enableextensions disabledelayedexpansion
pushd "x:\target\folder" && (
for /f "tokens=1,*" %%a in ('
robocopy . . /l /nocopy /is /e /nfl /njh /njs
') do echo %%~fb : %%a
popd
)
This will use a for /f to process the previous robocopy command, splitting the line in two tokens, the first will contain the number of files and will be stored in %%a, and the second the rest of the line and will be stored in %%b. For each line in the output of the inner robocopy command, the code in the do clause is executed: just echo to console the two tokens in reverse order.
If robocopy can not be used (OS older than Vista), then
#echo off
setlocal enableextensions disabledelayedexpansion
for /d /r "x:\target\folder" %%a in (*) do for /f "tokens=1,5" %%b in ('
dir /a-d "%%~fa.\*" 2^> nul ^| findstr /b /c:" " ^|^| echo 0
') do if "%%c"=="" echo %%~fa : %%b
This will
For each folder under the start directory (for /r /d) grab a reference and store it in %%a replaceable parameter
Run a dir command with the full path of the folder %%~fa
Use a pipe (|) to filter the list to only retrieve the lines that start with two spaces (the footer lines)
If no lines are found (that is, the dir command failed) output a 0
The lines generated by the dir | findstr are handled with a for /f command. We will read the first token (the number of files in the adecuated line) and the fifth (only present in the footer line with the directories info)
If the fifth element is empty, this line has information about the files, not the folders, so, echo the folder path and the files inside it
At the end, restore the previous active directory
The problem with this approach is that the dir | findstr is executed for each of the subfolders. This will make this slower than the robocopy solution.
It can be faster, but more code is needed
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%cd%"
set "folder="
for %%r in ("%root%") do (
set "rootDrive=%%~dr\"
if not defined rootDrive set "rootDrive=\\"
for /f "delims=" %%a in ('
dir /s /a "%%~fr.\*" 2^>nul ^| findstr /r /c:"^ " /c:"^ .*\\."
') do for /f "tokens=1,* delims=\" %%b in ("%%~a") do if not "%%c"=="" (
set "folder=%%c"
) else if defined folder (
for /f %%d in ("%%~a") do (
setlocal enabledelayedexpansion
echo(!rootDrive!!folder! : %%d
endlocal
)
set "folder="
)
)
In this case, we will execute only one dir command to retrieve the full list and filter with findstr the list to only retrieve the lines with the name of the folder and the footer for each folder.
The code will iterate (for /f %%a) over this list. When a folder line is found (it contains a backslash and can be splitted by the for /f %%b), the folder name is saved in a variable. When the corresponding footer line is readed (it was not splitted by a backslash), the line is splitted to get the number of files (for /f %%d) and the folder name and the number of files are echoed to console (here delayed expansion is needed to retrieve the variable contents, as they were set inside the same loop)
Note: Using a for /f to process a large list of data generated by executing a command can be really slow (there is a bad behaviour in how the command handles memory). For this cases it is necessary to first execute the command that generates the data (dir | findstr in this case) sending its output to a file and then use the for /f to process the file.
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%~1"
if not defined root for %%a in (.) do set "root=%%~fa"
set "folder="
for %%t in ("%temp%\%~nx0.%random%%random%.tmp") do for %%r in ("%root%") do (
set "rootDrive=%%~dr\"
if not defined rootDrive set "rootDrive=\\"
( dir /s /a "%%~fr.\*" 2^> nul | findstr /r /c:"^ " /c:"^ .*\\." ) > "%%~ft"
for /f "usebackq delims=" %%a in ("%%~ft") do (
for /f "tokens=1,* delims=\" %%b in ("%%~a") do if not "%%c"=="" (
set "folder=%%c"
) else if defined folder (
for /f %%d in ("%%~a") do (
setlocal enabledelayedexpansion
echo(!rootDrive!!folder! : %%d
endlocal
)
set "folder="
)
)
del /q "%%~ft"
)
edited to adapt to comments: robocopy solution changed to include total number of files
#echo off
setlocal enableextensions disabledelayedexpansion
set "total=0"
pushd "x:\target\folder" && (
for /f "tokens=1,*" %%a in ('
robocopy . . /l /nocopy /is /e /nfl /njh /njs
') do (
echo %%~fb : %%a
set /a "total+=%%a"
)
popd
)
echo Total files: %total%

Compare in windows command prompt

Description:
I want to use a syntax like COMP I:\folder1 Z:\folder2 to compare all files in my I drive with the content of my z drive. I only need to compare their names to see if one exsists in the other. I need to recurse into the subdirectories because there are many located in both drives, I understand I need to use a batch script using a FOR loop and the PUSHD and POPD command.
QUESTION:
How do I do this?
From the output from FOR /?
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.
So you'd do something like
#echo off
setlocal enabledelayedexpansion ENABLEEXTENSIONS
for /R P:\ %%F in (*.*) DO (
set fileFull=%%~fF
set filePath=%%~pF
set fileDrive=%%~dF
set fileName=%%~nF
set fileExtension=%%~xF
call :checker "!filePath!" "!fileName!" "!fileExtension!"
)
goto :eof
:checker
set fileTarget="c:%~1%~2%~3"
if not exist %fileTarget% echo %fileTarget% not found
goto :eof
In this case, the script is getting all the filenames in P:\ and its subdirectories and telling me if the file doesn't exist on the same path in C:
List folder differences over a tree:
This uses robocopy to create the list - don't remove /L as this switch makes robocopy only list the information.
robocopy "d:\source\folder one" "c:\target\folder two" /L /fp /njs /njh /ndl /ns /np /mir
If you really need to avoid third party utilities:
#ECHO OFF
SET FOLDER1=I:\Folder1\
SET FOLDER2=Z:\Folder2\
ECHO SET FNAME=%%1 ^& #ECHO %%FNAME:%FOLDER1%=%%^>^>FILES1.TXT>FILES1.BAT
ECHO SET FNAME=%%1 ^& #ECHO %%FNAME:%FOLDER2%=%%^>^>FILES2.TXT>FILES2.BAT
IF EXIST FILES1.TXT DEL FILES1.TXT
IF EXIST FILES2.TXT DEL FILES2.TXT
FOR /F "tokens=*" %%* IN ('DIR /S /B /ON "%FOLDER1%"') DO CALL FILES1.BAT "%%*"
FOR /F "tokens=*" %%* IN ('DIR /S /B /ON "%FOLDER2%"') DO CALL FILES2.BAT "%%*"
ECHO Files from "%FOLDER1%" which are not found in "%FOLDER2%"
FOR /F "tokens=*" %%* IN (FILES1.TXT) DO (FIND %%* FILES2.TXT >NUL || ECHO %%*)
ECHO Files from "%FOLDER2%" which are not found in "%FOLDER1%"
FOR /F "tokens=*" %%* IN (FILES2.TXT) DO (FIND %%* FILES1.TXT >NUL || ECHO %%*)

how to traverse specified subfolders in a windows batch file?

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Resources