Solved

How to nest commands in Batch FOR statement on multiple lines

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

22 Experts available now in Live!

Get 1:1 Help Now