Solved

How to nest commands in Batch FOR statement on multiple lines

Posted on 2004-09-23
2
372 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

785 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