我不知道如何在没有扩展名的情况下显示记事本中选定的文件
这是第一步,如果你有想法
@echo off
set dirpath=%1
dir %dirpath% /b /o:gen > #Tree.txt
start notepad.exe #Tree.txt
感谢您的快速回复“这不是我/ Wasif Hasan”
实际上我选择我想要的文件,想将文件名保存在文本文件中,我使用发送到命令重定向到快捷方式 *.bat
我不知道如何选择文件并执行你的代码,它返回一个空白文件
为了更精确,我不想打开一个对话框并寻找不同的文件路径,我想在不同的文件夹中选择许多文件但不是全部,右键单击/发送到/“批处理程序”,它会在打开的文本文件中列出没有扩展名的文件
我已经适应了很好的解决方案,我首先需要一个程序来隐藏控制台:命令是'cmdow @ /HID'
将文件从 ritchielawrence-cmdow-1bbcd2b\bin\Release\cmdow.exe 复制到 C:\Windows\System32
https://github.com/ritchielawrence/cmdow/tarball/master
cmdow @ /HID
@echo off && SetLocal EnableDelayedExpansion & color 0a & mode 113,13
set "_file=C:\xxxmypathxxx\#Tree.txt" && if /i not exist "%APPDATA%\Microsoft\Windows\SendTo\%~nx0" (
copy "%~dpnx0" "%APPDATA%\Microsoft\Windows\SendTo\" ) else (
<nul cd.>"!_file!" && for %%# in (%*)do echo/%%~n#>>"!_file!")
type "!_file!" | clip & "%__APPDIR__%notepad.exe" "!_file!"
答案1
- 也许有些人喜欢这个?
@echo off && SetLocal EnableDelayedExpansion & color 0a & mode 113,13
set "_file=%~dp1!random!.txt" && if /i not exist "%APPDATA%\Microsoft\Windows\SendTo\%~nx0" (
copy "%~dpnx0" "%APPDATA%\Microsoft\Windows\SendTo\" ) else (
<nul cd.>"!_file!" && for %%# in (%*)do echo/%%~n#>>"!_file!")
type "!_file!" | clip & "%__APPDIR__%notepad.exe" "!_file!"
- 更新:
- 1)只需复制代码并保存,例如:list_name.cmd
- 2)单击 list_name.cmd 并选择您的文件
- 3)List.txt 将在 list_name.cmd 所在的同一文件夹中创建
- 4)完成此作业后,List.txt 将由 list_name.cmd 打开
观察:这是一个经过简单改编的代码/cmd,可以在混合 cmd/vbs/ps1 脚本中运行,并且,这也是来自另一个改编的脚本贡孔斯和他们资料在这里!
<!-- ::
@echo off && cls && mode con:cols=60lines=5 & title Select Files^!
<nul cd.>.\List.txt
for /f tokens^=* %%i in ('%__APPDIR__%CScript //NoLogo "%~f0?.wsf"
')do if not "%%~i" == "NO Files Selected" ( set "_n=%%~ni" && (
cmd /v /c echo/!_n!>>.\List.txt)) else echo/Canceled By User^!
if exist .\List.txt type .\List.txt |findstr . >nul && start "" /b .\List.txt
exit /b
# -->
<job><script language="vbscript">
Private Function SelectFiles(InitialDir, Filter)
Dim result : result = ""
With WScript.CreateObject("WScript.Shell").Exec( _
"powershell.exe -NonInteractive -NoProfile -NoLogo -Command ""& {" & _
"[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');" & _
"$objOFDialog = New-Object System.Windows.Forms.OpenFileDialog;" & _
"$objOFDialog.Filter = '" & Filter & "';" & _
"$objOFDialog.InitialDirectory = '" & InitialDir & "';" & _
"$objOFDialog.Multiselect = $True;" & _
"$objOFDialog.RestoreDirectory = $True;" & _
"$objOFDialog.ShowHelp = $True;" & _
"$objOFDialog.SupportMultiDottedExtensions = $True;" & _
"$objOFDialog.Title = 'SelectFiles (" & InitialDir & ")';" & _
"[void]$objOFDialog.ShowDialog();" & _
"$objOFDialog.FileNames -join '|'" & _
"}""")
.StdIn.Close ' Important if not the script hangs:
While .Status = 0
WScript.Sleep 100
Wend ' .Status = 0
if .ExitCode = 0 Then
While Not .stdOut.AtEndOfStream
result = result & .stdOut.ReadAll
Wend ' Not .stdOut.AtEndOfStream
' Convert the string to an array of file paths
SelectFiles = Split(Replace(result, vbCrLf, ""), "|")
Else
While Not .stdErr.AtEndOfStream
result = result & .stdErr.ReadAll
Wend ' Not .stdErr.AtEndOfStream
WScript.Echo result
SelectFiles = False
End If ' .ExitCode = 0
End With ' WScript.CreateObject("WScript.Shell").Exec(...)
End Function ' SelectFiles
Dim SelectedFiles
SelectedFiles = SelectFiles(".", "Text Files *.*")
If IsArray(SelectedFiles) Then
If UBound(SelectedFiles) >= 0 Then
WScript.Echo "Selected Files: " & Join(SelectedFiles, vbCrLf)
Else
WScript.Echo "NO Files Selected"
End If ' UBound(SelectedFiles) >= 0
WScript.Quit 0
Else
WScript.Quit 1
End If ' IsArray(SelectedFiles)
</script></job>
通过编辑第 5 行set "_n=%%~ni
,还可以得到:
set "_n=%%~xi" x = will save .ext only
set "_n=%%~ni" n = will save name only
set "_n=%%~pi" p = will save path only
set "_n=%%~di" d = will save drive only
set "_n=%%~fi" f = will save full path
set "_n=%%~dpnxi" dpnx = will save full path
type 'for /?' there is more optins in help command...
- 旧帖
假设您使用以下命令布局:
file.bat c:\some\path\for\argument
在您的代码中,%~1
==1st argument
您可以使用带有扩展变量内部的 for 循环来执行此操作
您不需要设置变量%dirpath%
用于%1
。
因为这样你可以直接使用:dir %1
, 喜欢:
- 仅对于没有扩展名的名称:
@echo off
>nul cd.>.\#Tree.txt && for /f ^tokens^=* %%i in ('
dir "%~1\*.*" /b /o:gen')do echo=%%~ni>>.\#Tree.txt
if exist .\#Tree.txt @start notepad.exe .\#Tree.txt
- 对于不带扩展名的完整路径名:
@echo off
>nul cd.>.\#Tree.txt && for /f ^tokens^=* %%i in ('
dir/b/o:gen "%~1\*.*"')do echo=%%~dpni>>.\#Tree.txt
if exist .\#Tree.txt @start notepad.exe .\#Tree.txt
答案2
您可以使用 forfiles:
- 仅限文件名
@echo off
if exist "%~1" forfiles /p "%~1" /m *.* /c "cmd /c echo @file" >#tree.txt
if exist ".\#tree.txt" notepad.exe .\#tree.txt
- 文件名 + 扩展名
@echo off
if exist "%~1" forfiles /p "%~1" /m *.* /c "cmd /c echo @file.@ext" >#tree.txt
if exist ".\#tree.txt" notepad.exe .\#tree.txt