ʕ≧ᴥ≦ʔ Rodrigo's blog

Batch - Tips & Tricks

1. Converting a Unix file to Windows file

TYPE unix_file.txt | FIND "" /V > windows_file.txt or FOR /f "eol= tokens=* delims=" %%i in (unix_file.txt) do echo %%i >>windows_file.txt

2. Comparing dates of two files

dir /b /OD file1.txt file2.txt | more +1

This only works if the files are in the same folder, and neither is hidden (limitation of the Dir command). It compares Last Modified Date, but it can easily compare Created Date or Last Accessed Date using /TC or /TA. To capture the filename in a batch file you can use a For loop:

For /F "Delims=" %%I In ('dir /b /OD file1.txt file2.txt ^| more +1') Do Set _Newer=%%I

You can use the /AH switch if both files are hidden, but it won't work if only one is hidden. This will also do the same:

For /F "Skip=1 Delims=" %%I In ('dir /b /OD file1.txt file2.txt') Do Set _Newer=%%I

You can use Xcopy if the files are in the same or different folders, hidden or not, but can only compare the Last Modified dates:

For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul

If File1 is newer, _Newer is set to 1. If same or older date, it's set to 0. If you need the file name instead of a 1/0 answer, takes one more line:

For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer==1 (Set _Newer=C:\Folder1\File1.txt) Else (Set _Newer=D:\Backup\File2.txt)

This will accept fully qualified file names on the command line and output the name of the newest file:

Set _File1=%1
Set _File2=%2
For /F "Delims=" %%I In ('xcopy /DHYL %_File1% %_File2% ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer%==1 (Set _Newer=%_File1%) Else (Set _Newer=%_File2%)
Echo The newest file is %_Newer%

PS: If you need to compare the Created Date or Last Accessed Date for files in different folders, you can move one of them, compare using the first option, then move the file back. Not a good option for larger files, or files on a flash drive. You'd have to parse the Date and Time Stamp from a Dir for each file, convert it to a number format that batch can handle, and then compare the timestamps. Doable, but much more involved using a batch file. The GNU utilities would be much easier if you need to compare Created/Accessed dates in different folders without moving either file. And it can do multiple files, not just two.

3. Reading a flat file

File sample:

TEST.TXT:
indiana, brazil, 49
john, africa, 50
nicole, argentina, 13

Reading the file:

FOR /f "delims=, tokens=1,2,3*" %%i in (C:\test.txt) DO call :read_file %%i %%j %%k
goto :END

:read_file
SET name=%1
SET country=%2
SET age=%3
ECHO %name%
ECHO %country%
ECHO %age%
goto :eof


:END
echo Execution Success!
exit 0

4. Creating a log file with date/time

FOR /F "tokens=2,3,4 delims=/ " %%I in ('DATE /T') DO SET LOGDATE=%%K%%I%%J
FOR /F "tokens=1,2 delims=: " %%I in ('TIME /T') DO SET TIMEVAR=%%I%%J

SET LOGFILE=D:\LOGS\logfile_%LOGDATE%_%TIMEVAR%.log

Got feedback? 😊😐☹️

#batch #cmd #scripting #windows