Solved Win7 for /f loop crashes for no apparent reason.

January 8, 2016 at 07:48:43
Specs: Windows 7x64 Ultimate, i5-3570k 16g
I'm having problems trying to extract drive letters from an anonymous system. Actually, I'm having a problem trying to trim down the results.
This works well to collect the data:
wmic logicaldisk where drivetype=3 get deviceid >> %STORED%\DRIVES.TXT

:: IF NOT EXIST .\RESULTS MKDIR .\RESULTS
:: SET STORED=.\RESULTS

Typing the drives right before the for loop is correct, but one more step kills cmd.exe

When I try to use another for /f loop to skip the header, it crashes. The last thing that I tried was to add more variables to the final echo, but I'm completely in the dark as to why I did it, or even if it should be the case. Skip 2 is for the header and the Solid State C drive.

for /f "skip=2" %%x in (%STORED%\DRIVES.TXT) do echo %%g %%h %%i %%j %%k %%l %%m >> %STORED%\NEWDRIVES.TXT)

Does anybody with more experience see a blatant error on my part, and if you would be so kind, please rub my nose in it.

Finally, if I sometimes wish to skip 1, or skip 2, will the for /f loop accept the variable skip=%skip1%? I thought that might have been part of my problem, so I eliminated it, but it still crashes.

message edited by Longhand


See More: Win7 for /f loop crashes for no apparent reason.

Report •

#1
January 8, 2016 at 19:26:30
✔ Best Answer
::
::
::
::
::======  script starts here  ===============
:: 
:: longhand.bat  2016-01-09 10:21:01.95
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "skip=1 tokens=1 delims= " %%a in ('wmic logicaldisk where drivetype^=3 get deviceid') do (
echo.%%a
) >> NEWFILE
) >> OUTFILE
::======  script ends here  =================


message edited by Mechanix2Go


Report •

#2
January 8, 2016 at 21:41:36
::
::
::
:: ----------------- script starts here---------------------------

@echo off > enabledelayedexpansion
setlocal

echo.

REM set /p ssdok= Do you want to include the C drive today? ("y" or "n")

REM if /i %ssdok%== y (set skip= 1) else (set skip= 2)


ECHO INCLUDE THE SSD >> DRIVES.TXT

SET SKIP=1

for /f "skip=%skip% tokens=* delims= " %%a in ('wmic logicaldisk where drivetype^=3 get deviceid') do (
echo.%%a
) >> DRIVES.TXT

echo EXCLUDE THE SSD >> DRIVES.TXT

SET SKIP= 2

for /f "skip=%skip% tokens=* delims= " %%a in ('wmic logicaldisk where drivetype^=3 get deviceid') do (
echo.%%a
) >> DRIVES.TXT

notepad DRIVES.TXT
DEL /Q DRIVES.TXT
endlocal

::------------------------ script ends here----------------------------------

WORKS LIKE A CHAMP! THNX, MECHANIX!


Report •
Related Solutions


Ask Question