Results 1 to 4 of 4

Thread: DOS batch file

  1. #1
    Star Lounger
    Join Date
    Feb 2002
    Location
    Fort Collins, Colorado, USA
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm trying to write a batch file named FILEPROC.BAT that goes something like this:

    Echo You clicked on file %1
    Call DOSOMETHING %1
    Echo You are done
    Echo Thank you for using FILEPROC

    This batch file will be invoked by double-clicking on any file listed in Explorer. All it does is call another program, DOSOMETHING, with the name of the clicked file as a command-line parameter.

    The problem I have is that %1 holds the entire pathname of the clicked-on file. Because DOSOMETHING is an old DOS program, it only understands pathnames that confrom to old DOS conventions--i.e., no directories longer than 8 letters.

    I don't need to pass the entire path to DOSOMETHING. I'm content for the filespec to contain only the filename, without the path.

    So, is there any way to get %1 to contain only the filename, without the path?

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're using XP, you can still use the old DOS "command" (as opposed to "cmd") and obtain 8.3 file names, but parsing them out from the DIR output might be a bit of a pain. If you go the VBS script route, the solution is fairly easy. Here's an example that works for a file or folder dropped onto the VBS. You could easily link it to your double-click event, and run the DOSOMETHING program using the ShortPath parameter:

    Code:
    Set fso=CreateObject("Scripting.FileSystemObject")
    
    ' Is object a file or folder?
    If fso.FolderExists(WScript.Arguments(0)) Then
       'It's a folder
       Set objFolder = fso.GetFolder(WScript.Arguments(0))
       rtrn = InputBox("Here's your short path:", "SHORT PATH", objFolder.ShortPath)
    End If
    
    If fso.FileExists(WScript.Arguments(0)) Then
       'It's a file
       Set objFile = fso.GetFile(WScript.Arguments(0))
       rtrn = InputBox("Here's your short path:", "SHORT PATH", objFile.ShortPath)
    End If
    Alan

  3. #3
    Star Lounger
    Join Date
    Jul 2006
    Location
    Colorado, USA
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps using the FOR command would help. The /f flag (at least in XP) has the ability to handle filename only. Do for /? for help.

    Code:
    for /f %%i in ("%1") do echo You clicked on file %%~nxi
    call dosomething %1
    echo You are done
    echo Thank you for using FILEPROC
    You could do something similar with the call line if needed.

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,414
    Thanks
    33
    Thanked 195 Times in 175 Posts
    [quote name='DarylB' post='766495' date='20-Mar-2009 22:07'][/quote]
    This is a working version of what I called CALLER.BAT
    Code:
    @echo off
    echo What got passed as %%1 was  %1
    :: just the filename without the extension
    for /f %%a in ("%1") do set fn=%%~na
    echo filename is %fn%
    PAUSE
    :: filename dot extension
    for /f %%a in ("%1") do set fnx=%%~nxa
    echo filename.extension is %fnx%
    PAUSE
    :: take your pick from the above!  %fn% or %fnx%
    CALL DOSOMETHING %fn%
    The screen output from CALLER c:\windows\notepad.exe was
    Code:
    What got passed as %1 was  c:\windows\notepad.exe
    filename is notepad
    Press any key to continue . . . 
    filename.extension is notepad.exe
    Press any key to continue . . .
    I left out the error message about the call to DOSOMETHING...
    BATcher

    Time prevents everything happening all at once...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •