0

In DOS, I am trying to take the output from a command and assign it to a variable, then take the output and truncate the first two lines of the string. Here is a snippet of what I am trying to do:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=* delims= " %%a in ('net view') do (
set SERVER= %%a

set SERVER=%SERVER:~2
echo SERVER is SERVER
)

The output of the 'net view' command will list the servernames on my network. I want to remove the double blackslashes at the beginning of the string. I haven't had any luck using the truncation operation on the %%a variable. Is that possible, and if not how can I assign %%a to another variable to do the truncation on that instead? If there is another option to do this, I am also open to that as well.

fvrghl
  • 3,642
  • 5
  • 28
  • 36
M M
  • 21
  • 1
  • 3
  • Voting to close this question as a duplicate because it essentially boils down to this one: [Batch file variables initialized in a for loop](http://stackoverflow.com/questions/691047/batch-file-variables-initialized-in-a-for-loop). – Andriy M Jun 08 '13 at 09:43

1 Answers1

0

I don't have the time to formulate an example, but having had to recently address a similar issue, I believe the functions you are looking for can be found here: http://www.dostips.com/DtTipsStringOperations.php

I had to do a mass rename of files in a directory, but didn't want to rename .txt or .bat files - just all the others.

Key function was: set str=%str:~-4%

DOS script used:

dir /b > filelist.txt
FOR /F "TOKENS=1 DELIMS=," %%A IN (filelist.txt) DO CALL :PARSEIT %%A
del filelist.txt
EXIT
REM: END OF PROGRAM

REM: CALL FUNCTIONS BELOW
:PARSEIT %1
set str=%1
echo.%str%
set str=%str:~-4%
echo.%str%
IF %str%==.txt GOTO :EOF
IF %str%==.bat GOTO :EOF
set str=%1
RENAME %str% %str%.txt
:EOF

Hope this helps.