Solved

How to nest commands in Batch FOR statement on multiple lines

Posted on 2004-09-23
2
379 Views
Last Modified: 2011-08-18
Can anyone tell me what is wrong with this code?  Is it possible to nest and If withing a FOR?  The %1 var is a file with 2 string per line, a source and destination path.  I am trying to check if the destination path begins with a '\'.  The code works if I cram it on one line.  I was hoping to do it on multiple lines.  Any suggestions?

for /F "tokens=1,2 skip=1 delims= " %%A in (%1) do (
       set strB=%%B
      if %strB:~0,1% NEQ "\" (
            echo  ERROR: DESTINATION PATH IN MANIFEST FILE MUST BEGIN WITH  '\' >> %strLogName%
      @set Errcd=1
      )
)


0
Comment
Question by:rdclk23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Expert Comment

by:Longbow
ID: 12140619
You are usind batch files, not Pascal, C++ or a script.
It is like Basic and the numbers before each line.
You can't do it.
Batch needs to work with complete lines.
0
 
LVL 14

Accepted Solution

by:
dlwyatt82 earned 500 total points
ID: 12142668
You can do it, you just need to enable delayed variable expansion on the command line. Run cmd.exe with the "/V:ON" switch, or set the following registry value to 1:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor
"Delayed Expansion" = DWORD:00000001

Then modify your code as follows:

for /F "tokens=1,2 skip=1 delims= " %%A in (%1) do (
      set strB=%%B
     if !strB:~0,1! NEQ "\" (
          echo  ERROR: DESTINATION PATH IN MANIFEST FILE MUST BEGIN WITH  '\' >> %strLogName%
          @set Errcd=1
     )
)

*********************************************

Note the use of "!" characters instead of "%" when you want the variable to be evaluated after being reset inside a compound statement. Normally, the variable is expanded to whatever it contained before the for statement executed.

example:

set strTest=TESTING

for /F %%a in (textfile.txt) do (
  set strTest=%%a
  echo %strTest%
)

*************

would just echo "TESTING" once for each line in the file. If you enable delayed variable expansion and change the %s to !s, the code works as you intended.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Part Two of the two-part Q&A series with MalwareTech.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question