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
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.
- Can anyone give me idea to solve this?
- Any suggestion for better writing code for my code?
Thank you in advance
After changing the base source location on line four, as needed, (and optionally un
Remarking yournew.txtoutput lines), does this untested version of your idea do what you intended?