如何检测拖放的参数是否是文件夹?

时间:2015-12-26 19:22:07

标签: batch-file vbscript

我正在尝试制作一个批处理脚本,用于将 .m3u文件转换为.lst和.cfg

所以,我已经制作了一个vbscript来通过拖放处​​理单个文件,它工作正常。 现在我担心这批:如何检测拖放的参数是否是一个文件夹?

批处理文件:

@ECHO OFF
Title Conversion des fichiers de type .m3u en .lst et en .cfg by HACKOO
Mode con cols=70 lines=5
color 0A 
echo.
IF [%1] EQU [] Goto:Error
CD /D "%~1"
FOR %%f IN (*.m3u) DO (echo. & echo Conversion du fichier "%%f" ... & Cscript /NoLogo %~dp0Convert_m3u-lst-cfg.vbs "%%f" & Cls & echo.)
Start "" explorer "%~1"
Exit

:Error
Color 0c
echo.
ECHO    Il faut glisser et deposer un dossier sur ce programme batch 
Timeout /T 6 /NoBreak >nul
Exit

Vbscript:Convert_m3u-lst-cfg.vbs

Option Explicit
Const Titre = "Conversion des fichiers de type .m3u en .lst et en.cfg by © HACKOO"
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Const StartTagName = "<servername=>"
Const EndTagName = "</servername>"
Const StartserveurURL = "<serverurl=>"
Const EndserveurURL = "</serverurl>"
Const category = "<servercategory=>movies</servercategory>"
Dim sInfile,sOutfile,oFSO,oInfile,oOutfile,Texte,Folder,Ws,MyDirectory,FileM3UConverted
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Ws = CreateObject("Wscript.Shell")
MyDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
'****************************1ère étape : Formater le fichier.m3u déposé, càd enlever toutes les lignes vides***********************************
' Obtenir le nom du fichier d'entrée de ligne de commande , si le 2ème paramètre est entré
' utiliser le deuxième paramètre en tant que nouveau fichier de sortie, sinon réécriture du fichier d'entrée
If (WScript.Arguments.Count > 0) Then
    sInfile = WScript.Arguments(0)
        If LCase(oFSO.GetExtensionName(sInfile)) <> "m3u" Then 
            MsgBox "Faites glisser et déposer le fichier.m3u à convertir sur ce script",vbExclamation,Titre
            Wscript.Quit()
        End If  
Else
    MsgBox "Faites glisser et déposer le fichier.m3u à convertir sur ce script",vbExclamation,Titre
    WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
    sOutfile = WScript.Arguments(1)
Else
    sOutfile = sInfile
End If
' Lire le fichier en entré dans une variable puis fermetrure de ce dernier
Folder = GetFilenameWithoutExtension(sInfile) & "_Hackoo-Conversion"

If Not oFSO.FolderExists(Folder) Then
    oFSO.CreateFolder(Folder)
End If  
FileM3UConverted = Folder & "\" & GetName(Folder) &".m3u"
'wscript.echo sInfile
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
Set oOutfile = oFSO.OpenTextFile(FileM3UConverted, ForWriting, True) 

While Not oInfile.AtEndOfStream
    Texte = oInfile.ReadLine
    Texte = Trim(Texte)
    If ( Len(Texte) > 0 ) Then
        oOutfile.Writeline Texte
    End If
Wend
oOutfile.Close
Set oOutfile = Nothing
'*****************************2ème étape : Extraire les lignes depuis le fichier formaté .m3u vers un fichier.lst******************************
Dim strLine,Tab,strNewcontents,i,k,Count,Name,URL,Ligne,sOutfileCfg
sInfile = FileM3UConverted
sOutfile = GetFilenameWithoutExtension(sInfile) & ".lst" 
sOutfileCfg = GetFilenameWithoutExtension(sInfile) & ".cfg"
If oFSO.FileExists(sOutfile) Then
    oFSO.DeleteFile(sOutfile)
End If
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
oInfile.SkipLine()
Do Until oInfile.AtEndOfStream
    strLine = oInfile.Readline
    strLine = Trim(strLine)
    If Len(strLine) > 0 Then
        strNewcontents = Replace(strNewcontents,"#EXTINF:-1,","")
        strNewcontents = Replace(strNewcontents,"#EXTINF:0,","")
        strNewcontents = Replace(strNewcontents,"#EXTINF:0 ","")
        strNewcontents = Replace(strNewcontents,"#EXTINF:-1 ","")
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop
oInfile.Close
Count = 0
Tab = split(strNewcontents,vbcrlf)
For i = lbound(Tab) to ubound(Tab) Step 2
    If i Mod 2 = 0 then
        Count = Count + 1
        Name = Trim(Tab(i))
        k = i+1
        If k > UBound(tab) Then Exit For
        URL = Tab(k)
    End If  
    Ligne = StartTagName & Name & EndTagName & StartserveurURL & URL & EndserveurURL & category
    Call WriteLog(Ligne,sOutfile)
    Call WriteLog("I: " & URL & " " & Name,sOutfileCfg)
Next
Ws.Popup "La conversion du fichier  "& vbCrLf &  DblQuote(sInfile) & vbCrLf & " est terminé avec succès !","2",Titre,64
'MsgBox "La conversion du fichier " & vbCrLf &  DblQuote(sInfile) & vbCrLf & " est terminé avec succès !",vbInformation,Titre
'**********************************************************************************************
'Fonction pour écrire le résultat dans un fichier texte
Sub WriteLog(strText,LogFile)
    Dim fs,ts 
    Const ForAppending = 8
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
    ts.WriteLine strText
    ts.Close
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Function GetFilenameWithoutExtension(ByVal FileName)
  Dim Result, i
  Result = FileName
  i = InStrRev(FileName, ".")
  If ( i > 0 ) Then
    Result = Mid(FileName, 1, i - 1)
  End If
  GetFilenameWithoutExtension = Result
End Function
'**********************************************************************************************
Function GetName(Path)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetName = fso.GetBaseName(path)
End Function
'**********************************************************************************************

5 个答案:

答案 0 :(得分:3)

测试元素是文件夹,文件还是空白的方法。 改编自:How to test if a path is a file or directory in Windows batch file?

@echo off

set /p var=Name of the element to test :

call:test %var%
goto end

:test
if exist "%~1" (2>nul pushd "%~1" && (popd&set "type=FOLDER") || set "type=FILE" ) else set "type=UNKNKOWN"
echo "%~1" = %type%

:end

答案 1 :(得分:2)

您只能在文件夹上使用cd,这样就可以检查cd命令是否适用于参数。

批处理,看起来像是

set olddir=%CD%
2>nul cd %parametre%&&echo the parameter is not a folder||echo the parameter is a folder
REM Restore old dir after cd.
cd %olddir%

答案 2 :(得分:1)

除了@ TSnake41的方式之外,您还可以尝试这样的事情:

@echo off

::without pushd, fails on extensionless files
set "folder=false"
for /F "delims=" %%A in ("%~1") do if "%%~xA"=="" set "folder=true"
echo folder: %folder%

::with pushd
set "folder=true"
2>nul pushd "%~1"
if %errorlevel% neq 0 set "folder=false"
popd
echo folder: %folder%
pause

答案 3 :(得分:1)

您只需将\附加到项%~1并检查项目是否存在:

if exist "%~1\" (
    echo Item is a folder.
) else (
    echo Item is a file.
)

由于您正在谈论拖放,因此项目%~1始终存在,因为您无法拖动任何内容。但是如果附加了\,则检查将对文件失败但对文件夹失败。如果您拖动一个没有足够访问权限的文件夹,它也会失败。

请注意,始终列出%~1而不是%1,这会删除潜在的包围引号。因此,"%~1\"会明确地将该项目包含在一对引号中,并在其中包含\

答案 4 :(得分:1)

此处有几个答案使用existpushd / popd,但您也可以使用文件属性。我相信技术属性更强大。我根据How to test if a file is a directory in a batch script?中链接的答案编写了这个子程序。

::
:$is_dir ( ref_RETURN PATH )
:: RETURN == (BOOLEAN as undef/1) whether PATH is a directory
:: NOTE: URLref: https://stackoverflow.com/questions/138981/how-do-i-test-if-a-file-is-a-directory-in-a-batch-script/3728742#3728742
setlocal ENABLEEXTENSIONS
set "_RETval="
set "_RETvar=%~1"
if NOT EXIST "%~2" ( goto :_is_dir_RETURN )
set "attr=%~a2"
set "attr_bit=%attr:~0,1%"
if /i "%attr_bit%"=="d" (set "_RETval=1")
:_is_dir_RETURN
endlocal & set %_RETvar%^=%_RETval%
goto :EOF
::

这是一个测试批处理文件......

@echo off
if "%~1" == "" (echo usage: %~nx0 PATH & goto :EOF)
call :$is_dir is_a_dir "%~1"
set /p _OUTPUT="'%1' "<nul &:: aka, echo-noLF
if not exist "%~1" ( set /p _OUTPUT=does not exist;<nul )
if defined is_a_dir ( echo IS a directory ) else ( echo is NOT a directory)
goto :EOF

:: ** SUBs
goto :EOF
::
:$is_dir ( ref_RETURN PATH )
:: RETURN == (BOOLEAN as undef/1) whether PATH is a directory
:: NOTE: URLref: https://stackoverflow.com/questions/138981/how-do-i-test-if-a-file-is-a-directory-in-a-batch-script/3728742#3728742
setlocal ENABLEEXTENSIONS
set "_RETval="
set "_RETvar=%~1"
if NOT EXIST "%~2" ( goto :_is_dir_RETURN )
set "attr=%~a2"
set "attr_bit=%attr:~0,1%"
if /i "%attr_bit%"=="d" (set "_RETval=1")
:_is_dir_RETURN
endlocal & set %_RETvar%^=%_RETval%
goto :EOF
::