FOR command - skip first row in the file
Posted: 05 May 2010 10:28
Hi
I have a file temp.txt with a list of file names generated via a
dir /b /o:N file*.txt > temp.txt
command.
So temp.txt contains
file.txt
file1.txt
file2.txt
I now want to read the contents of temp.txt and skip the first row and delete the files whose filenames are denoted in the other 2 rows. To do this, I wrote a FOR command to read the file and process it.
set /a counter=0
FOR /f "tokens=* delims= " %%i in (temp.txt) do (
if %counter% EQU 1 (del %%i)
if %counter% EQU 0 (set /a counter+=1)
)
At the end of running the batch file, it is setting the counter to a value of 3 and not deleting any of the 3 files. It clearly proves that the "if" command is not working. Can you please tell me what is wrong with this or if there is a different way to do this.
Please note that the number of files in the directory is variable and I always want to delete all of them except the first one. Please advise.
I have a file temp.txt with a list of file names generated via a
dir /b /o:N file*.txt > temp.txt
command.
So temp.txt contains
file.txt
file1.txt
file2.txt
I now want to read the contents of temp.txt and skip the first row and delete the files whose filenames are denoted in the other 2 rows. To do this, I wrote a FOR command to read the file and process it.
set /a counter=0
FOR /f "tokens=* delims= " %%i in (temp.txt) do (
if %counter% EQU 1 (del %%i)
if %counter% EQU 0 (set /a counter+=1)
)
At the end of running the batch file, it is setting the counter to a value of 3 and not deleting any of the 3 files. It clearly proves that the "if" command is not working. Can you please tell me what is wrong with this or if there is a different way to do this.
Please note that the number of files in the directory is variable and I always want to delete all of them except the first one. Please advise.