I have a requirement to create a batch file in windows 2003 server. We do have Cygwin installed so have the ability to run unix commands.
Every night, a process will download a timestamped file, FILE*%yyyy%%mm%%dd%.txt to c:\temp\download.
An FTP process will then FTP it to another location and move the file to c:\temp\download\archived.
What we are trying to accomplish is, prior to ftping, do a file compare against the new file in c:\temp\download against the last modified file in c:\temp\download\archived in order to only ftp lines that have changed since the last file was uploaded. The current days txt file should only contain lines that are different then the previous days.
I have ran the following script with success to sort and compare two files:
@echo on
set archive=C:\temp\download\archived
set download=C:\temp\download\
pushd "%archive%"
for /f "tokens=*" %%G in ('dir *.txt /b /a-d /od') do SET oldfile=%%G
pushd "%download%"
for /f "tokens=*" %%H in ('dir *.txt /b /a-d /od') do SET newfile=%%H
echo The last file processed was %oldfile%
echo The new file is %newfile%
comm -2 -3 <(sort %archive%\%oldfile%) < (sort %download%\%newfile%) > %download%\%newfile%
pushd "%download%"
popd
The script works without the sort but once that is added the script fails.
Running comm -2 -3 %archive%\%oldfile% %download%\%newfile% works but when adding the sort does not, with the error:
comm -2 -3 C:\temp\download\archived\CPSNS_20140527075503.txt) C:\temp\download\CPSNS_20140602075502.txt) 0<(sort 1>c:\temp\download\workpl ease.txt The system cannot find the file specified.
Any assistance would be greatly appreciated!
It looks like it's working now. Below is the script I used. Feel free to suggest any enhancements. It involves sorting both files then running COMM against them, and only outputs lines that have changed in the %newfile% vs the %oldfile%.
@echo on
set archive=C:\temp\download\archived
set download=C:\temp\download\
pushd "%archive%"
for /f "tokens=*" %%G in ('dir *.txt /b /a-d /od') do SET oldfile=%%G
pushd "%download%"
for /f "tokens=*" %%H in ('dir *.txt /b /a-d /od') do SET newfile=%%H
echo The last file processed was %oldfile%
echo The new file is %newfile%
sort %archive%\%oldfile% > tmp && mv tmp %download%\%oldfile%
sort %download%\%newfile% > tmp && mv tmp %download%\%newfile%
comm -2 -3 %download%\%newfile% %download%\%oldfile% > tmp && mv tmp %download%\%newfile%
pushd "%download%"
del %oldfile%
popd