Need your help
I need create a batch file (command prompt) to
⁃ Show a list of folders and sub folders
⁃ within them are exe files
⁃ Only show the 2 most up to date exe files
⁃ display specific folders not all
And export information in a txt file
I'm using XP if that helps
update
I have the below commands
first one works and orders by most recent file, but doesn't give me time and date
second shows time and date but doesn't order by most recent
#ECHO OFF
setlocal EnableDelayedExpansion
set j=0
Echo Test
echo\
FOR /f "delims=" %%i IN ('dir C:\test\ /o-n-d /b') DO (
echo %%i
set /A j=j+1
if !j! geq 2 (
goto :end1
)
)
:end1
#ECHO OFF
setlocal EnableDelayedExpansion
set j=0
echo\
Echo Test
echo\
FOR /f "delims=" %%i IN ('forfiles /p C:\testmove /s /m *.* /C "cmd /c echo #file #fdate #ftime" ') DO (
echo %%i
set /A j=j+1
if !j! geq 2 (
goto :end2
)
)
:end2
pause
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir"
SET "lastdir="
(
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.exe" '
) DO (
IF "%%~dpa" neq "!lastdir!" (
SET "lastdir=%%~dpa"
SET /a count=0
FOR /f "delims=" %%i IN ('dir /s /b /a-d /o:d "%%~dpa\*.exe"') DO IF !count! lss 2 (
SET /a count+=1
ECHO %%~ti %%~fi
)
)
)
)>newfile.txt
GOTO :EOF
Produces newfile.txt. You would need to set your required directory name in sourcedir. I showed the data as date/time fullfilename because fullfilename is of variable-length whereas date and time are fixed. Might have been easier if you'd shown us the format you expect - saves guesswork and revisions.
To show the two most recently modified files, change .../b /a-d /o:d "%%~... to .../b /a-d /o:-d "%%~... (note - between the o: and d)
Uses Robocopy to show the two latest modified .exe files in the current folder tree
It also displays the UTC date and time of the two files.
#echo off
setlocal enabledelayedexpansion
set "folder=%cd%"
set c=0
for /f "tokens=1,2,*" %%a in (
'robocopy "%folder%" "%folder%" "*.exe" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort /r '
) do echo "%%a %%b" - "%%c" & set /a c+=1 & if !c! EQU 2 goto :done
:done
pause
Related
I have this script below:
#echo off & setlocal
del /f /s /q %temp%\DuplicateRemover.txt
del /f /s /q %temp%\DuplicateRemover.bat
echo SetLocal DisableDelayedExpansion >>%temp%\DuplicateRemover.txt
echo #echo off ^& setlocal >>%temp%\DuplicateRemover.txt
echo rem Group all file names by size >>%temp%\DuplicateRemover.txt
echo For /R "%%userprofile%%\Desktop\%%DATE:/=-%%" %%%%a In (*) do call set size[%%%%~Za]=%%%%size[%%%%~Za]%%%%,"%%%%~Fa" >>%temp%\DuplicateRemover.txt
echo rem Process groups >>%temp%\DuplicateRemover.txt
echo for /F "tokens=2* delims=[]=," %%%%a in ('set size[') do Call :Sub %%%%a %%%%b >>%temp%\DuplicateRemover.txt
echo Goto ^:Eof >>%temp%\DuplicateRemover.txt
echo ^:Sub >>%temp%\DuplicateRemover.txt
echo If "%%~3"=="" (Set "size[%%1]="^&goto :EOf) >>%temp%\DuplicateRemover.txt
echo processing %%* >> %temp%\DuplicateRemover.txt
echo Keep %%2 >> %temp%\DuplicateRemover.txt
echo Shift^&shift >> %temp%\DuplicateRemover.txt
echo :loop >> %temp%\DuplicateRemover.txt
echo Del %%1 >> %temp%\DuplicateRemover.txt
echo if not "%%~2"=="" (shift^&goto :loop) >>%temp%\DuplicateRemover.txt
ren "%temp%\DuplicateRemover.txt" DuplicateRemover.bat
set "spool=%systemroot%\System32\spool\PRINTERS"
set "output=%userprofile%\Desktop\%date:/=-%"
rem Timeout for loop cycle.
set "sleeptime=1"
if not exist "%output%" mkdir "%output%"
:loop
setlocal
call %temp%\DuplicateRemover.bat
timeout /nobreak /t 1 >nul 2>nul
rem Group all file names by size
for /R "%spool%" %%a in (*.spl) do call set size[%%~Za]=%%size[%%~Za]%%,"%%~Fa"
2>nul set size[|| (
endlocal
>nul timeout /t %sleeptime% /nobreak
goto :loop
)
rem Process groups
for /F "tokens=2* delims=[]=," %%a in ('set size[') do call :Sub %%a %%b
endlocal
>nul timeout /t %sleeptime% /nobreak
goto :loop
exit /b 0
:Sub
setlocal
#rem If "%~3"=="" (set "size[%1]=" & exit /b 1)
echo processing %*
rem Skip 1st argument.
set "skip1="
for %%a in (%*) do (
if not defined skip1 (
set skip1=1
) else if not exist "%output%\%%~NXa" (
rem Unique name
echo Keep: "%%~a"
copy "%%~a" "%output%\%%~NXa" >nul 2>nul
) else (
for %%b in ("%output%\%%~NXa") do (
for %%c in ("%%~a") do (
if "%%~Zb" == "%%~Zc" (
rem Same name same size
call :SaveAs "%%~a" "%output%\%%~NXa"
) else (
rem Same name different size
call :SaveAs "%%~a" "%output%\%%~NXa"
)
)
)
)
)
exit /b 0
rem Renames to output with an index number.
:SaveAs
setlocal
set "name=%~dpn2"
:NewNameLoop
set /a i+=1
if exist "%name%(%i%).spl" goto :NewNameLoop
echo Keep: "%~1" as "%name%(%i%).spl"
copy "%~1" "%name%(%i%).spl" >nul 2>nul
exit /b 0
When the script runs, it create another .bat that works together with the main script.
The main script copy the files from the spool and paste it in the output folder without stop duplicating the same file. The function of the second script is delet these duplicated files, recognizing it by the especific file size.
It's working 75% good. Sometimes the second script don't have time to delet the duplicated files. I guess is better merge these two scripts in only one. So it will work better.
Can someone help me how can i do it?
why are the files of the same size?
are these in different folders?
You can do this more easily by using a versioning system.
#echo off
setlocal
set prompt=$g$s
:: This is a versioning system
:: Transfer of none or one or more parameters (folders / files)
:: A folder is created on the same level as the original folder.
:: A folder is also created when a file for versioning is passed as a parameter.
:: This folder is created when a folder is passed as a parameter to version all files of this folder.
:: Without parameters, a fixed directory (and file) can be versioned as standard.
:: A log file is maintained in the versioning folder.
:: Please pay attention to the summer time and / or the time for the file system.
:: The variable rCopyCMD is used to pass other Robocopy options.
:: The versioned file gets the current time stamp as a version feature.
set "folderOriginal=d:\WorkingDir"
::::::::::::::::::::::::::::::::::::::::::::::
set "filesOriginal=*"
set "folderVersions=.Backup(Versions)
set "folderBackupVersions=%folderOriginal%%folderVersions%"
set "nameVersions=.(v-timeStamp)"
set "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"
:getAllParameters
if :%1 equ : goto :EndParameter
if exist %1\ (
set "FolderOriginal=%~1"
set "folderBackupVersions=%~1%folderVersions%"
set "filesOriginal=*"
) else (
set "FolderOriginal=%~dp1"
for %%i in ("%~dp1\.") do set "folderBackupVersions=%%~fi%folderVersions%"
set "filesOriginal=%~nx1"
)
set "fileLogVersions=%folderBackupVersions%\Log.(Versions).log"
:EndParameter
call :TAB
set "timeStamp=."
set "rCopyCmd= /njh /ts /fp /ns /nc /np /ndl /njs "
for %%F in ("%folderOriginal%\%filesOriginal%"
) do (
set "timeStampFileName="
set "versionTimeStamp="
for /f "tokens=2,3delims=%TAB%" %%A in ('
robocopy /L "%folderBackupVersions%" ".. versions Listing only..\\" ^
"%%~nF%nameVersions:timeStamp=*%%%~xF" %rCopyCmd% ^|sort ^& ^
robocopy /L "%%~dpF\" ".. original List only ..\\" "%%~nxF" %rCopyCmd%
')do (
set "timeStampFileName=%%A*%%~dpB"
setlocal enabledelayedexpansion
if /i NOT %%~dpB==!folderBackupVersions!\ if %%A gtr !versionTimeStamp! (
call :getCurrent.timestamp
for /f "tokens=1-3delims=*" %%S in ("%nameVersions:timeStamp=!timeStamp!%*!timeStampFileName!"
) do (
endlocal
robocopy "%%~dpF\" "%folderBackupVersions%" "%%~nxF" %rCopyCmd%
ren "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
>>"%fileLogVersions%" ( if NOT errorlevel 1 (
echo %%S -^> %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
) else echo ERROR -^> %%T "%folderBackupVersions%\%%~nxF" "%%~nF%%S%%~xF"
)
)
) else endlocal &echo %%A %%~nxF - No Backup necessary.
if .==.!! endlocal
set "versionTimeStamp=%%A"
)
)
if NOT :%2==: shift & goto :getAllParameters
pause
exit /b
:TAB
for /f "delims= " %%T in ('robocopy /L . . /njh /njs') do set "TAB=%%T"
rem END TAB
exit /b
:getCurrent.timestamp
rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (' robocopy /L /njh "\|" .^|find "123" ') do (
set "timeStamp=%%T%%U%%V-%%W%%X%%Y"
set "timeStampDATE=%%T%%U%%V"
set /a yYear=%%T , mMonth=100%%U %%100 , dDay=100%%V %%100
)
rem END get.currentTimestamp
exit /b
How can I create a batch script that opens a random folder within a specific directory? This code here prints out a randomly chosen file(I need it to open the folder, not the file) but I could not figure out how to open it.
#Echo Off
:Start
set directory="D:\Movies"
set count=0
for /f %%f in ('dir "%directory%" /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /a-d /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
echo %filename%
pause
goto Start
I suddenly realised what I was doing wrong and solved the problem. Here is the final and working code:
#Echo Off
:Start
set directory="D:\Film"
set count=0
for /f %%f in ('dir "%directory%" /ad /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /ad /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
%SystemRoot%\explorer.exe %filename%
exit /b
goto Start
You really shouldn't need to increment a count and use findstr for such a task; just assigning and sorting a random number should do:
#Echo Off
Set "source=D:\Film"
SetLocal EnableDelayedExpansion
For /D %%A In ("%source%\*") Do Set "$[!RANDOM!]=%%A"
For /F "Tokens=1* Delims==" %%A In ('"Set $[ 2>Nul|Sort"'
) Do Set "target=%%B" & GoTo Found
Exit /B
:Found
Explorer "%target%"
If you wanted a recursive directory search then change line 5 to:
For /D /R "%source%" %%A In (*) Do Set "$[!RANDOM!]=%%A"
I don't know how should I title my problem but here's what I'm trying to do:
I want to search for duplicates in the directory (which is from the input)
Then get the number of duplicates files
Here's where I'm stuck, if there's is no duplicate files, it should echo out "No duplicate files found", my idea is using >nul|set fileError="No duplicate files found"
I'm confused where should i placed it because I'm looping through the results.
To be direct, if there is no duplicate files [filenames] then it should echo "No duplicate files found".
Here's what I did:
set findFiles=0
for /f %%A in ('FORFILES /P "%filePath%" /S /M "%fileName%" /C "cmd /c echo #path"') do (
set /a findFiles+=1
echo %findFiles% - %%~A
)
Update: As to clarify what I wanted, My batch file should return the list of duplicate files if the result is greater than 1, if the return list is only one then it should echo out "No duplicate files found".
Based on my comments here are some examples for you.
First using your ForFiles method and delayed expansion:
For /F "Delims==" %%A In ('"(Set file[) 2>Nul"') Do Set "%%A="
Set "i=0"
SetLocal EnableDelayedExpansion
For /F "Delims=" %%A In (
'ForFiles /P "%filePath%" /S /M "%fileName%" /C "Cmd /C Echo #Path" 2^>Nul'
) Do Set /A i+=1 & Set "file[!i!]=%%~A"
If %i% Equ 0 Echo File %fileName% not found in %filePath% & GoTo End
If %i% Equ 1 Echo No duplicates of %fileName% found in %filePath% & GoTo End
For /L %%A In (1 1 %i%) Do Echo [%%A]!file[%%A]!
:End
Pause
Using For /F with Dir and delayed expansion:
For /F "Delims==" %%A In ('"(Set file[) 2>Nul"') Do Set "%%A="
Set "i=0"
SetLocal EnableDelayedExpansion
For /F "Delims=" %%A In ('Dir /B /S /A-D "%filePath%\%fileName%" 2^>Nul'
) Do Set /A i+=1 & Set "file[!i!]=%%A"
If %i% Equ 0 Echo File %fileName% not found in %filePath% & GoTo End
If %i% Equ 1 Echo No duplicates of %fileName% found in %filePath% & GoTo End
For /L %%A In (1 1 %i%) Do Echo [%%A]!file[%%A]!
:End
Pause
You could also use Where instead of Dir changing its line 4to:
For /F "Delims=" %%A In ('"Where /R "%filePath%" "%fileName%" 2>Nul"'
You could use RoboCopy changing line 4 to the following two lines:
Set "RCO=/S /L /XJ /R:0 /FP /NS /NC /NP /NDL /NJH /NJS"
For /F "Tokens=*" %%A In ('RoboCopy "%filePath%" Null "%fileName%" %RCO%'
Or possibly a For /R option changing line 4 to:
For /R "%filePath%" %%A In ("%fileName%?"
The For /R version is a little strange because it has to use a wildcard, whilst this could be problematic, it may be useful however should you doubt whether your file has the .doc or .docx extension; (it should match both)
Everyday, a document is created automatically named articles.txt.
I have to make several changes to the document.
After the changes made the document is named articles_Final + date.txt
As example : articles_Final_2016_01_07.txt
The documents are then moved to a directory ". \ HISTO \ FINAL".
I have to compare the two last document created and to write the difference in a new document result.txt
I show you my code for findind the two last documents
#echo off & cls
SETLOCAL EnableDelayedExpansion
set "Dossier=.\HISTO\Final"
set /a "n=0, limit=2"
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W "%Dossier%\*.*"') do (
echo "%%a"
2>nul set /a "n+=1, 1/(limit-n)"||goto :break
)
:break
pause
exit
And le code who compare the two document
Here, I write the name of them but i would like to find the way to write automaticly the result of my first code
findstr /v /g:articles_Final_2016_01_04.txt articles_Final_2016_02_04.txt >result.txt
#echo off
cls
SETLOCAL EnableDelayedExpansion
set "Dossier=.\HISTO\Final"
set /a "n=0, limit=2"
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W "%Dossier%\*.*"') do (
echo "%%a"
set "file[!n!]=%%a"
2>nul set /a "n+=1, 1/(limit-n)"||goto :break
)
:break
set file
echo findstr /v /g:%Dossier%\%file[0]% %Dossier%\%file[1]%
pause
exit
(untried)
The findstr command will just be echoed - you'd need to check it and redirect the output.
the set file command is simply a way of displaying all variables that start with file.
Here, the final solution
#echo off
cls
SETLOCAL EnableDelayedExpansion
set "Dossier=.\HISTO\FINAL"
set /a "n=0, limit=2"
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W "%Dossier%\*.*"') do (
echo "%%a"
set "file[!n!]=%%a"
2>nul set /a "n+=1, 1/(limit-n)"||goto :break
)
:break
set file
findstr /v /g:%Dossier%\%file[0]% %Dossier%\%file[1]% >>difference.txt
::fc /L %Dossier%\%file[0]% %Dossier%\%file[1]% >>difference.txt
pause
exit
I have a text file with a list of server IP addresses and the code below (which I've scrapped together from other coding) loops through it and brings back a modified date of a named file for each server in the list...
#ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%a
)
GOTO :EOF
:getmod
SET Server=%1
SET File=Abs_Client.exe
FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :eof
That works great...however, what I'd like to do is create another loop around it which creates a variable for each file in a directory and pass that into the code above instead of having to manually change the 'SET File' as shown above for individual files.
Something along the lines of;
#ECHO On
SETLOCAL
FOR /D %VAR IN ("\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update") DO (
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%a
)
GOTO :EOF
:getmod
SET Server=%1
SET File=%VAR
FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :eof
)
Clearly it's wrong so any ideas/help please?
haven't testet, but maybe a hint in the right direction:
#ECHO ON
SETLOCAL
FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
CALL :getmod %%A "%%~nxF"
)
)
GOTO :EOF
:getmod
SET Server=%1
SET "tmpFile=%~2"
FOR %%I IN ("\\%Server%\C$\Com_Dir\%tmpFile%") DO ECHO %Server% %tmpFile% %%~tI >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :EOF
As far as i know, FOR /D only executes for directorys and if i understand your question, you have files in "Prod Apps\Server_Update", for each you like to have the file-date/time from the target-server... right?
Edit:
Maybe this works too:
FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
FOR %%X IN ("\\%%A\C$\Com_Dir\%%~nxF") DO ECHO %%A %%~nxF %%~tX >> "C:\Scripts\Server_App_Mod_date.txt"
)
)
without the :getmod
Edit: /b-switch was missing from the first DIR-Command in 2nd suggestion
#ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
FOR /f "delims=" %%i IN ('dir /b/a-d "\\%%a\C$\Com_Dir\*"') DO Echo %%a %%i %%~ti >> "C:\Scripts\Server_App_Mod_date.txt"
)
GOTO :EOF
Should work, IIUC. Can't test, I'm afraid...
[Edit - removed call to getmod - not required]