Store path/filename uses space on user selection Batch File to create a shortcut

71 Views Asked by At

I'm writing a bat file to finds all .exe files in current my bat folder location, and lists them 1 going up and then user can select the file to create shortcut. The problem I'm having is when it echos each folder in the directory for a user to select it only shows the first part of the foldername before the space.

I have a directory with folder in it that have spaces in their name. Example: "Another Folder" shows as "Another" Here is my path: D:\Works\Script\Test\Program\Property Information System\Office\

Directory content:

Back Office.bat
Back Office.exe
Banquet.bat
Banquet.exe
Config.ini
New.txt
Shortcut.bat

Folder

My code works fine if there is no space in folder name or filename, but if there is a space, the result just show first word of the folder name or filename, in my case for path will shows D:\Works\Script\Test\Program\Property Or first word of filename. Here is my code:

@echo off
setlocal enabledelayedexpansion
set /A counter=0
set choice=
for /R %%i in (*.exe) do (
if not "%%~nxi" == "%~nx0" (
set /A counter+=1
echo List !counter!: %%~nxi
set thefile[!counter!]=%%i))
if %counter% EQU 1 (
echo !thefile! 
    set /A EXENUM=!choice!!counter!
    call %EXECUTABLE%
) else set /P EXENUM="Please choose : "
set EXECUTABLE=!thefile[%EXENUM%]!
call :expand %EXECUTABLE%
:expand
set filename=%~nx1
set file=%~n1
set gang=%~dp1
set pth=%~dp0
set SCRIPT="%pth%skrip-%RANDOM%.vbs"
rem echo This is pth = %pth% > "D:\Works\Script\Test\Backoffice\new.txt"
rem echo This is filename = %filename% >> "D:\Works\Script\Test\Backoffice\new.txt"
rem echo This is file = %file% >> "D:\Works\Script\Test\Backoffice\new.txt"
rem echo This is path = %gang% >> "D:\Works\Script\Test\Backoffice\new.txt"
rem echo This is pathexe = %gang%%file% >> "D:\Works\Script\Test\Backoffice\new.txt"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\%file%.lnk" >> %SCRIPT%
echo Set olink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo olink.TargetPath = "%gang%%file%.bat" >> %SCRIPT%
echo oLink.WorkingDirectory = "%gang%" >> %SCRIPT%
echo olink.IconLocation = "%EXECUTABLE%" >> %SCRIPT%
echo olink.Save >> %SCRIPT%
cscript //nologo %SCRIPT%
del %SCRIPT%
exit

I have tried using any arguments parameters like %~f1, %~dp1, etc, but the closest result is still shows only first word of folder name or filename.

  1. Can anyone give me idea to solve this?
  2. Any suggestion for better writing code for my code?

Thank you in advance

1

There are 1 best solutions below

4
Compo On

After changing the base source location on line four, as needed, (and optionally unRemarking your new.txt output lines), does this untested version of your idea do what you intended?

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "src=D:\Works\Script\Test"
PushD "%src%" 2>NUL || Exit /B

For /F "Delims==" %%G In ('"(Set #) 2>NUL"') Do Set "%%G="

Set "cnt=0"
For /F "Delims=" %%G In ('(Set PATHEXT^=^) ^& %SystemRoot%\System32\where.exe
 /R "%src%" "*.exe" 2^>NUL') Do If Exist "%%~dpnG.bat" (Set /A cnt += 1
    SetLocal EnableDelayedExpansion & For %%H In (!cnt!) Do (EndLocal
        Set "#%%H=%%G" & Echo( %%H. %%G))

If Not Defined #1 (Echo No file matches, press a key to exit.
    Pause 1>NUL & GoTo :EOF)

:Opt
Set "opt="
Echo(
Set /P "opt=Enter the number for your chosen file>"
Set "opt=%opt:"=%"
Set # | %SystemRoot%\System32\findstr.exe /BL "#%opt%=" 1>NUL || GoTo Opt

Set "rnd=%RANDOM%"
SetLocal EnableDelayedExpansion
For %%G In ("!#%opt%!") Do (EndLocal

    Rem (   Echo This is pth = %~dp0
    Rem     Echo This is filename = %%~nxG
    Rem     Echo This is file = %%~nG
    Rem     Echo This is path = %%~dpG
    Rem     Echo This is pathexe = %%~dpnG
    Rem ) 1>"%src%\new.txt"

    (   Echo Set oWS = WScript.CreateObject("WScript.Shell"^)
        Echo sLinkFile = "%UserProfile%\Desktop\%%~nG.lnk"
        Echo Set olink = oWS.CreateShortcut(sLinkFile^)
        Echo olink.TargetPath = "%%~dpnG.bat"
        Echo oLink.WorkingDirectory = "%%~dpG"
        Echo olink.IconLocation = "%%~G"
        Echo olink.Save
    ) 1>"%~dp0skrip-%rnd%.vbs"
)

If Exist "%~dp0skrip-%rnd%.vbs" (
    %SystemRoot%\System32\cscript.exe //NoLogo "%~dp0skrip-%rnd%.vbs"
    Del "%~dp0skrip-%rnd%.vbs")

Exit /B