Solved

How to nest commands in Batch FOR statement on multiple lines

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now