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

Author Topic: check to see if variable entered is .txt file  (Read 5763 times)

0 Members and 1 Guest are viewing this topic.

newuserlh

    Topic Starter


    Rookie

    check to see if variable entered is .txt file
    « on: November 27, 2009, 01:35:41 PM »
    Hi there,
    I'd like to check that when the user enters a variable.. that it is a .txt file..
    I've done something like this.. but no success right now..
    :FileBegin
    SET /p variable=[Enter the log file you need report(s) on] (e.g YYYYMMDD.txt) :
    cscript /nologo c:\DATES.vbs %variable%
    vb CODE is as follows:(Set objArgs = WScript.Arguments
    strDate = objArgs(0)
    s = Split(strDate,".")
    yr = Mid(s(0),1,3)
    mth = Mid(s(0),5,2)
    dy = Mid(s(0),7,2)

    If  IsDate(yr&"/"&mth&"/"&dy) <> 0 Then
       WScript.Echo "Valid date"
    Else
       WScript.Echo "Invalid date"
       

    End If)

    if %variable% NEQ %variable%.txt ECHO Enter a valid date with .txt extension 
    if %variable% NEQ %variable%.txt goto FileBegin

    I want to check if the variable entered has an extension of .txt. i'm sure this is quite simple but Im not sure how to do it! I want to check the date first.. to see if that's valid.. then check to see if it's a .txt file..
    I could always add it to the vbs code, what do you think?

    Ps your answers have been great so far, thanks alot :)

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: check to see if variable entered is .txt file
    « Reply #1 on: November 27, 2009, 01:38:53 PM »
    Here

    if not exist "%variable%.txt" echo Error! & goto FileBegin

    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    newuserlh

      Topic Starter


      Rookie

      Re: check to see if variable entered is .txt file
      « Reply #2 on: November 27, 2009, 01:40:23 PM »
      Oh god.. that was simple.. ;)
      Thanks

      newuserlh

        Topic Starter


        Rookie

        Re: check to see if variable entered is .txt file
        « Reply #3 on: November 27, 2009, 01:57:19 PM »
        Actually I have this already..
        Can I check if a variable has an extension of .txt?

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: check to see if variable entered is .txt file
        « Reply #4 on: November 27, 2009, 02:00:35 PM »
        Actually I have this already..
        Can I check if a variable has an extension of .txt?
        What do you mean?
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        newuserlh

          Topic Starter


          Rookie

          Re: check to see if variable entered is .txt file
          « Reply #5 on: November 27, 2009, 02:07:42 PM »
          I mean that I'd like to check if my date variable had a .txt extension.. not if the file does not exist.

          Because if it didn't, I'd like to return a message to user saying, please enter .txt extension.

          previously, we're simply just checking if the %variable%.txt exists, which is right too, but I want to add some validation for the user.

          I think I will probably use something similar to Mid.. and check the string to see if ".txt" is there in last four characters of variable string. I just need to think about it more maybe :)

          If you've any ideas, let me know :)

          BatchFileBasics



            Hopeful

            Thanked: 18
            Re: check to see if variable entered is .txt file
            « Reply #6 on: November 27, 2009, 02:16:26 PM »
            try this out, put this after your "set /p" line

            Code: [Select]
            set var=%variable:~-4%
            if %var%==.txt goto ok else goto no
            exit
            :ok
            code here if it works

            :no
            code here it doesn't work
            When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

            Salmon Trout

            • Guest
            Re: check to see if variable entered is .txt file
            « Reply #7 on: November 27, 2009, 04:30:14 PM »
            I mean that I'd like to check if my date variable had a .txt extension.. not if the file does not exist.

            You can check if a filename string has a .txt extension whether or not the file actually exists using the ~x variable modifier.

            Code: [Select]
            @echo off
            :loop
            set /p Uinput="Please input filename in the format YYYYMMDD.txt : "
            for /f %%A in ("%Uinput%") do set ext=%%~xA
            if /i "%ext%"==".txt" goto next
            echo Error - wrong format
            goto loop
            :next




            gh0std0g74



              Apprentice

              Thanked: 37
              Re: check to see if variable entered is .txt file
              « Reply #8 on: November 27, 2009, 04:31:11 PM »
              just do everything in vbscript,

              Code: [Select]

              Set objFS=CreateObject("Scripting.FileSystemObject")
              WScript.Echo "Enter log file date (YYYYMMDD.txt):"
              Do While Not WScript.StdIn.AtEndOfLine
                 strFile = strFile & WScript.StdIn.Read(1)
              Loop
              s = Split(strFile,".")
              yr = Mid(s(0),1,3)
              mth = Mid(s(0),5,2)
              dy = Mid(s(0),7,2)
              If  IsDate(yr&"/"&mth&"/"&dy) <> 0 Then
                 WScript.Echo "Valid date"
                 WScript.Echo "Now checking extension"
                 If s(UBound(s)) = "txt" Then
                  WScript.Echo "Valid text file"
                 End If
                 
              Else
                 WScript.Echo "Invalid date" 

              End If

              output
              Code: [Select]
              C:\test>cscript /nologo test.vbs 20090922.txt
              Enter log file date (YYYYMMDD.txt):
              20091203.txt
              Valid date
              Now checking extension
              txt
              Valid text file


              newuserlh

                Topic Starter


                Rookie

                Re: check to see if variable entered is .txt file
                « Reply #9 on: November 27, 2009, 06:19:49 PM »
                Hi there
                Thanks again.
                Is there any way we can Jump out of the vbs program and GOTO somewhere on my dos executable program??
                E.G
                Else
                   WScript.Echo "Invalid date" 
                   WScript.GOTO :filebegin

                 

                Joanlong



                  Rookie

                  Re: check to see if variable entered is .txt file
                  « Reply #10 on: November 27, 2009, 08:08:03 PM »
                  Text File



                  Code: [Select]
                  @echo  off
                  set var=%1

                  echo variable=%var%

                  set var=%var:~-4,4%

                  echo variable=%var%


                  if %var%==.txt (
                  goto ok
                  ) else (

                  goto no

                  )

                  :ok

                  echo ok: The file has %var% extension
                  goto eof

                  :no
                  echo no: The file has a %var% extension
                  :eof


                  Output:

                  C:\test>txtfile.bat  txtfile.txt

                  variable=txtfile.txt
                  variable=.txt
                  ok: The file has .txt extension

                  C:\test>txtfile.bat  txtfile.wpd
                  variable=txtfile.wpd
                  variable=.wpd
                  no: The file has a .wpd extension
                  C:\test>
                  « Last Edit: November 28, 2009, 08:33:28 AM by Joanlong »

                  BatchFileBasics



                    Hopeful

                    Thanked: 18
                    Re: check to see if variable entered is .txt file
                    « Reply #11 on: November 27, 2009, 09:18:14 PM »
                    Bill can you stick to one username?
                    When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                    gh0std0g74



                      Apprentice

                      Thanked: 37
                      Re: check to see if variable entered is .txt file
                      « Reply #12 on: November 27, 2009, 09:40:40 PM »
                      dos executable program??

                      what are you doing in cmd.exe(your so called "dos") that you think vbscript can't do?

                      Salmon Trout

                      • Guest
                      Re: check to see if variable entered is .txt file
                      « Reply #13 on: November 28, 2009, 01:21:59 AM »
                      Bill can you stick to one username?

                      I said it was him, but nobody listened.

                      newuserlh

                        Topic Starter


                        Rookie

                        Re: check to see if variable entered is .txt file
                        « Reply #14 on: November 28, 2009, 08:15:12 AM »
                        Hi thanks,
                        What I'd like to try and do is pass a variable from the .vbs program into the batch program.
                        Like when the date variable has been checked and is valid, I'd like to pass it back into the batch program.

                        Is this possible??