1.

Solve : for loop runs twice?

Answer»

I have an issue where a for loop from a previous post is running twice. I am trying to get the file properties from the current directory and all sub directories. The only issue is that I am getting double results in my out put file. The issue is some where in the below code.

Code: [Select]ECHO Working with %CD%^\%FILE%

set fname=%FILE%

dir %fname% > dir.txt

for /f "skip=4 tokens=5" %%v in (dir.txt) do (
SET PS=%%~dpnxv
SET FS=%%~zv
SET FMDT=%%~tv
GOTO NEXT
)

:NEXT
echo Path Specification: %PS%>>"%userprofile%\Desktop\FileProps.csv"
echo File Size: %FS%>>"%userprofile%\Desktop\FileProps.csv"
echo File Modified Date/Time: %FMDT%>>"%userprofile%\Desktop\FileProps.csv"
echo =================================================================>>"%userprofile%\Desktop\FileProps.csv"
echo =================================================================>>"%userprofile%\Desktop\FileProps.csv"

Pause

del dir.txt


The Fileprops.csv looks like the following:

Path Specification: C:\WINDOWS\system32\XPSViewer\fileProps.bat
File Size: 2026
File Modified Date/Time: 10/14/2009 08:10 AM
=================================================================
=================================================================
Path Specification: C:\WINDOWS\system32\XPSViewer\fileProps.bat
File Size: 2026
File Modified Date/Time: 10/14/2009 08:10 AM
=================================================================
=================================================================



Thanks,
WayneI notice you are using the append redirection symbol >> to write to the csv so each time you run the batch the csv will get bigger. Also take out the GOTO NEXT. You do not need that, nor the :next label.
Quote from: Salmon Trout on October 14, 2009, 10:21:10 AM

I notice you are using the append redirection symbol >> to write to the csv so each time you run the batch the csv will get bigger. Also take out the GOTO NEXT. You do not need that, nor the :next label.


the >> is so the file will be appended to. with out that the file would be recreated each time. That would be fine except this will be ran on all sub directories.

the goto was to try and force it to leave the loop without going through it twice. That is the issue I am trying to FIX now. The loop SEEMS to be picking on on the next LINE of the dir.txt. I have also tried to make it create the csv in the for loop and I still have the loop executing twice. Any ideas on how to fix it?

Thanks,
WayneIf there are 2 lines in the dir.txt then the loop will execute 2 TIMES. Investigate why the dir.txt has 2 lines. Also your "tokens=" block is redundant.
yes well is there a way to to only have it run on the 5th line and not the 6th and 7th? Code: [Select]
setlocal enabledelayedexpansion
set line=1
for /f "delims==" %%v in (dir.txt) do (
if !line! EQU 5 (
SET PS=%%~dpnxv
SET FS=%%~zv
SET FMDT=%%~tv
)
set /a line+=1
)



Discussion

No Comment Found