Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Problem with writing batch file  (Read 4035 times)

0 Members and 1 Guest are viewing this topic.

abcmno

  • Guest
Problem with writing batch file
« on: October 19, 2006, 03:31:02 AM »
Hi all,
I am writing a batch file. The batch file is suppposed to find out the number of files in a particular folder. Is that possible using dos command as I want the value of the number of files to be stored in a variable.
Also, is it possible using dos command that if I execute any command in dos prompt then the resulting value of that command can I store in a variable.

GuruGary



    Adviser
    Re: Problem with writing batch file
    « Reply #1 on: October 19, 2006, 11:31:04 PM »
    Which version of DOS are you using, or what version of Windows is your command prompt running under?  If running under Windows NT / 2000 / XP / 2003, then what you want to do is pretty easy, and can be done several different ways.  To set the environment "numfiles" to contain the number of files in the current directory, you could run the following from within a batch file:
    Code: [Select]
    for /f %%a in ('dir /a^|find "File(s)"') do set numfiles=%%a

    abcmno

    • Guest
    Re: Problem with writing batch file
    « Reply #2 on: October 20, 2006, 12:31:42 AM »
    Thanks a lot for that reply.
    I have some more questions in my mind.

    Is it possible to set the resulting value of a command to a variable?

    If I have 2 values in 2 different variable, then can I concatenate them?

    I have some value in a variable and there is an extra space at the end of the value, some thing like
    "abcwdweydew ".I want to remove that extra space.Is it possible?


    GuruGary



      Adviser
      Re: Problem with writing batch file
      « Reply #3 on: October 20, 2006, 03:01:01 PM »
      Quote
      Thanks a lot for that reply.
      Is it possible to set the resulting value of a command to a variable?
      Yes
      Quote
      Thanks a lot for that reply.
      If I have 2 values in 2 different variable, then can I concatenate them?
      Yes
      Quote
      Thanks a lot for that reply.
      I have some value in a variable and there is an extra space at the end of the value, some thing like
      "abcwdweydew ".I want to remove that extra space.Is it possible?
      Yes

      The FOR /F loop will save resulting output to a variable as in my previous example.

      To concatenate 2 variables (let's say named numfiles1 and numfiles2) into a new variable (numfilesconcat) you can do:
      Code: [Select]
      set numfilesconcat=%numfiles1%%numfiles2%If you want to add them together instead you would do:
      Code: [Select]
      set /a numfilestotal=%numfiles1% + %numfiles2%
      To remove the last character off a variable named "string" you can do:
      Code: [Select]
      set new=%string:~,-1%

      abcmno

      • Guest
      Re: Problem with writing batch file
      « Reply #4 on: October 25, 2006, 06:00:16 AM »
      Thanks a lot. It worked.