Solved

How to nest commands in Batch FOR statement on multiple lines

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 2000 Server to 2008 upgrade 8 519
Domain Controller all of a sudden stop replicating 3 455
no logon server available 8 158
OLD CPUs 12 95
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…
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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