Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

Setting variables using the SET command in batch file

I have a problem with a DOS command.  I have acheived everything I want to with a series of DOS commands and all I want to do is place a string in the event log which I can de easily enough at a command prompt but it needs to be inserted within a batch file and it will not work
If I go to a command prompt with and type this
set VarName=D:    279 GB Total,  69.35 GB (24%) Free,  2% Fragmented (4% file fragmentation)

I will get this when I do an echo
echo %VarName%
D:    279 GB Total,  69.35 GB (24%) Free,  2% Fragmented (4% file fragmentation)

When I put these same lines in a batch file it treats the % within the string as a holder for part of the string and this happens

set VarName=D:    279 GB Total,  69.35 GB (24%) Free,  2% Fragmented (4% file fragmentation)
echo %VarName%
D:    279 GB Total,  69.35 GB (24 Fragmented (4 file fragmentation)

This is rather meaningless and I have spent all day getting to this point and I would like to resolve it as quickly as possible

0
jimcrint
Asked:
jimcrint
  • 2
1 Solution
 
SteveGTRCommented:
Use double percents:

set VarName=D:    279 GB Total,  69.35 GB (24%%) Free,  2%% Fragmented (4%% file fragmentation)
echo %VarName%
0
 
jimcrintAuthor Commented:
Sorry I missed the email about this.  What I am doing does not give me enouth control to do this.  I am not very good with scripting so I tend to do what i know

The problem is I want to put the results of a defrag scan into the event log (the monitoring software I have will then collect this information and report on it)

The log
Windows Disk Defragmenter
Copyright (c) 2001 Microsoft Corp. and Executive Software International, Inc.
Analysis Report                         
    37.26 GB Total,  18.87 GB (50%) Free,  3% Fragmented (6% file fragmentation)
Defragmentation Report                   
    37.26 GB Total,  18.87 GB (50%) Free,  2% Fragmented (5% file fragmentation)
Using a Command line script and EVENTCREATE I can only put in a single line so I have this complicated script that does this but I lose the bit between the % and I get this
    37.26 GB Total,  18.87 GB (50 Fragmented (5 file fragmentation)

This is my script if it makes sense
                     defrag %%A: -f > c:\defrag-%%A.log
      set /A Counter+=1
      findstr /e /n "fragmentation)" c:\defrag-%%A.log > "%Temp%\temp.log"
      findstr /b  "8:" "%Temp%\temp.log" > "%Temp%\temp1.log"
      copy "\\%SBSSERVER%\CTS$\init.txt"+"%Temp%\temp1.log" "%Temp%\VarSet.cmd"
      call "%Temp%\VarSet.cmd"
      Echo Number before fragmented is % free, number before file is % fragmentation
      Echo !VarName!
      EVENTCREATE /ID !counter! /T INFORMATION /SO Defrag /L APPLICATION /D "Drive %%A: !VarName!"
0
 
SteveGTRCommented:
I'm not sure how to comment on this other than what I said previously:

Echo Number before fragmented is %% free, number before file is %% fragmentation
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now