Currently, I am attempting to create a shortcut for a program, I was able to do so shown in the code below.
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%HOMEDRIVE%%HOMEPATH%\Desktop\Unturned Dedicated Server\Unturned - Server.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs
The issue is I need the target path to be, "C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe" -batchmode -nographics +secureserver/ahhh
How would I go about doing so?
To do this as a batch-file, which creates a vbscript, runs it, then deletes it, I'd suggest that you do it like this:
#( Echo Set WshShell = WScript.CreateObject("WScript.Shell"^)
Echo strDesktop = WshShell.SpecialFolders("Desktop"^)
Echo str32bitPF = WshShell.ExpandEnvironmentStrings("%%ProgramFiles(x86)%%"^)
Echo Set oFSO = CreateObject("Scripting.FileSystemObject"^)
Echo If Not (oFSO.FolderExists(strDesktop + "\Unturned Dedicated Server"^)^) Then
Echo oFSO.CreateFolder(strDesktop + "\Unturned Dedicated Server"^)
Echo End If
Echo Set oShellLink = WshShell.CreateShortcut(strDesktop + "\Unturned Dedicated Server\Unturned - Server.lnk"^)
Echo oShellLink.Arguments = "-batchmode -nographics +secureserver/ahhh"
Echo oShellLink.TargetPath = str32bitPF + "\Steam\steamapps\common\Unturned\Unturned.exe"
Echo oShellLink.WindowStyle = 1
Echo oShellLink.Hotkey = "CTRL+SHIFT+U"
Echo oShellLink.Description = "Launch Unturned"
Echo oShellLink.WorkingDirectory = strDesktop + "\Unturned Dedicated Server"
Echo oShellLink.Save) > "CreateShortcut.vbs"
#%__AppDir__%cscript.exe /NoLogo "CreateShortcut.vbs"
#Del "CreateShortcut.vbs"
However, you can also do it directly from your batch-file without writing to a file too.
<!-- :
#%__AppDir__%cscript.exe /NoLogo "%~f0?.wsf"
#GoTo :EOF
-->
<Job><Script Language="VBScript">
Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
str32bitPF = WshShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not (oFSO.FolderExists(strDesktop + "\Unturned Dedicated Server")) Then
oFSO.CreateFolder(strDesktop + "\Unturned Dedicated Server")
End If
Set oShellLink = WshShell.CreateShortcut(strDesktop + "\Unturned Dedicated Server\Unturned - Server.lnk")
oShellLink.Arguments = "-batchmode -nographics +secureserver/ahhh"
oShellLink.TargetPath = str32bitPF + "\Steam\steamapps\common\Unturned\Unturned.exe"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+U"
oShellLink.Description = "Launch Unturned"
oShellLink.WorkingDirectory = strDesktop + "\Unturned Dedicated Server"
oShellLink.Save
</Script></Job>
If you wish to add your own batch-file code to this version it must be inserted just above the #GoTo :EOF line.
You should add another line where to put the arguments to be passed to your .exe target :
#echo off
Title Batch exe to shortcut
Set "VBS_Shortcut=%temp%\%~n0.vbs"
Set "ShortcutName=Unturned - Server"
Set "TargetPath=C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe"
Set "Arguments=-batchmode -nographics +secureserver/ahhh"
Call :Create_Shortcut "%ShortcutName%" "%TargetPath%" "%Arguments%"
Exit
REM ----------------------------------------------------------------------------------------------------
:Create_Shortcut
> "%VBS_Shortcut%" (
echo Call Create_Shortcut("%~1","%~2","%~3"^)
echo Sub Create_Shortcut(ShortcutName,TargetPath,Arguments^)
echo Dim objShell,DesktopPath,objShortCut
echo Set objShell = CreateObject("WScript.Shell"^)
echo DesktopPath = objShell.SpecialFolders("Desktop"^)
echo Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& ShortcutName ^& ".lnk"^)
echo objShortCut.TargetPath = chr(34^) ^& TargetPath ^& chr(34^)
echo objShortCut.Arguments = Arguments
echo objShortCut.Save
echo End Sub
)
cscript //nologo "%VBS_Shortcut%" "%~1" "%~2" "%~3"
If Exist "%VBS_Shortcut%" Del "%VBS_Shortcut%"
Exit /B
REM ----------------------------------------------------------------------------------------------------
Related
I want to pass variables from VBScript to batch but it wouldn't work.
My VBScript:
Dim shell
Set shell = CreateObject("WScript.Shell")
strnaam = InputBox ("naam")
and my batch:
#echo off
cls
echo %strnaam%
pause
I want the variable strnaam from my VBScript to my batch.
The most obvious way would be to run the vbscript as a For /F command and save its returned output as a variable:
#Echo Off
:NaamBox
Set "naam="
(Echo WScript.Echo InputBox("Naam:"^))>"%TEMP%\naam.vbs"
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%TEMP%\naam.vbs"')Do Set "naam=%%A"
If Not Defined naam GoTo NaamBox
Del "%TEMP%\naam.vbs"
Echo Uw naam is %naam%
Pause
If you don't like the idea of writing, running, then deleting the file, you could also embed your VBScript within the batch file:
<!-- :
#Echo Off
Echo Typ gelieve uw naam in de popup doos en OK te selecteren
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%~f0?.wsf"')Do Set "naam=%%A"
If Defined naam Echo Uw naam is %naam%&Pause
Exit /B
-->
<Job><Script Language="VBScript">
WScript.Echo InputBox("Naam:")
</Script></Job>
You can pass the variables only via environment variables:
The create_variable.vbs file:
Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
WshEnv("NAAM") = "This text will appear in batch"
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing
Then the batch file show_vbs_variable.bat (you have to open a new cmd.exe to have the new variable there! If you need more infor here is a topic on SO that covers it.:
#echo off
cls
echo %naam%
pause
vbs script for clearing up the variable clearing_variable.vbs:
Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
'Deleting the env variable
WshEnv.Remove("NAAM")
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing
So recently I asked for help with creating .srt subtitles. Here is the link.
I got the help and everything works fine until the videofile in the folder has unicode symbols in its name. If it does, then VBScript error appears. Question is how to make this code work correctly with Unicode symbols.
Here is the code:
#echo off&cls
::The Path of your Videos files
set "$VideoPath=C:\FolderwithVideos"
::If you want your Code in this BAT remove the REMs Below :
rem dir "%$VideoPath%" /O:S /b /-p /o:gn > "C:\result.txt"
rem call replacer.bat result.txt ".mp4" ""
setlocal enabledelayedexpansion
set /a $Count=1
set "$Timer=00:00:00"
(for /f "delims=" %%a in (result.txt) do (
call:getVideolength "%%a.mp4"
for /f "delims=" %%x in ('cscript //nologo getvideolength.vbs') do (
call:SumTime !$Timer! %%x
for /f "delims=" %%y in ('cscript //nologo SumTime.vbs') do set "$NewTimer=%%y"
echo !$Count!
echo !$Timer!,000 --^> !$NewTimer!,000
echo %%a
Set $Timer=!$NewTimer!
)
set /a $Count+=1
echo.
))>Output.srt
echo Done !!!
type Output.srt
pause
exit/b
:GetVideoLength
(echo dim objShell
echo dim objFolder
echo dim objFolderItem
echo set objShell = CreateObject("shell.application"^)
echo set objFolder = objShell.NameSpace("%$videoPath%"^)
echo set objFolderItem = objFolder.ParseName(%1^)
echo dim objInfo
echo objInfo = objFolder.GetDetailsOf(objFolderItem, 27^)
echo wscript.echo objinfo)>GetVideoLength.vbs
exit/b
:SumTime
echo wscript.echo FormatDateTime(CDate("%1"^) + CDate("%2"^),3^) >SumTime.vbs
exit/b
After reviewing the previous question I merged all the code and logic into one unicode safe VBScript file.
Option Explicit
Const adUnsignedBigInt = 21
Const adVarWChar = 202
Const adVarChar = 200
Dim VideoDir
VideoDir = "C:\FolderwithVideos"
'or from a script argument when called like : cscript script.vbs "C:\FolderwithVideos"
'VideoDir = WScript.Arguments(0)
Const adSaveCreateOverWrite = 2
Const adCRLF = -1
Const adWriteLine = 1
Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")
Dim Fso
Set Fso = CreateObject("Scripting.Filesystemobject")
Dim VideoExts
Set VideoExts = CreateObject("scripting.dictionary")
VideoExts.CompareMode = vbTextCompare
VideoExts.Add "mp4", Null
'add more extensions if you need
'VideoExts.Add "srt", Null
'VideoExts.Add "mkv", Null
Dim SrtStream
Set SrtStream = CreateObject("Adodb.Stream")
SrtStream.Charset = "utf-8"
SrtStream.Open
Dim Folder, IFolderItem, VideoCount, OldDuration, NewDuration, FileExtension, SrtPath, RsSorted
Set RsSorted = CreateObject("Adodb.Recordset")
RsSorted.Fields.Append "Name", adVarWChar, 255
RsSorted.Fields.Append "Size", adUnsignedBigInt
RsSorted.Fields.Append "Duration", adVarChar, 8
RsSorted.Open
NewDuration = TimeSerial(0,0,0)
Set Folder = ShellApp.NameSpace(VideoDir)
For Each IFolderItem In Folder.Items
FileExtension = Fso.GetExtensionName(IFolderItem.Name)
If VideoExts.Exists(FileExtension) Then
RsSorted.AddNew Array("Name", "Size", "Duration"), Array(IFolderItem.Name, IFolderItem.Size, Folder.GetDetailsOf(IFolderItem, 27))
End If
Next
RsSorted.UpdateBatch
RsSorted.Sort = "Size, Name"
If Not RsSorted.BOF Then RsSorted.MoveFirst
While Not RsSorted.EOF And Not RsSorted.BOF
FileExtension = Fso.GetExtensionName(RsSorted("Name").Value)
VideoCount = VideoCount + 1
OldDuration = NewDuration
NewDuration = OldDuration + CDate(RsSorted("Duration").Value)
SrtStream.WriteText VideoCount, adWriteLine
SrtStream.WriteText OldDuration & ",001 --> " & NewDuration & ",000", adWriteLine
SrtStream.WriteText Left(RsSorted("Name").Value, Len(RsSorted("Name").Value) - (Len(FileExtension) + 1)), adWriteLine
SrtStream.WriteText "", adWriteLine
RsSorted.MoveNext
Wend
SrtPath = Fso.BuildPath(VideoDir, "Output.srt")
SrtStream.SaveToFile SrtPath, adSaveCreateOverWrite
SrtStream.Close
WScript.Echo "Done!"
WScript.Echo SrtPath
Trying to create a shortcut in batch using a VBScript. I need to change the properties of the path to allow it to run in no-graphics mode.
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo Set shortcut = oWS.CreateShortcut("%USERPROFILE%\Desktop\unturned.exe -batchmode -nographics +secureserver/TheServer.lnk") >> %SCRIPT%
echo shortcut.TargetPath = "D:\SteamLibrary\SteamApps\common\Unturned\Unturned.exe" >> %SCRIPT%
echo shortcut.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
It can't save.
This should did the trick :
#echo off
Set vbsfile=%TEMP%\%RANDOM%.vbs
Set MyFile=D:\SteamLibrary\SteamApps\common\Unturned\Unturned.exe
Set ShorcutName=Unturned
(
echo Call Shortcut("%MyFile%","%ShorcutName%"^)
echo ^'**********************************************************************************************^)
echo Sub Shortcut(CheminApplication,Nom^)
echo Dim objShell,DesktopPath,objShortCut,MyTab
echo Set objShell = CreateObject("WScript.Shell"^)
echo MyTab = Split(CheminApplication,"\"^)
echo If Nom = "" Then
echo Nom = MyTab(UBound(MyTab^)^)
echo End if
echo DesktopPath = objShell.SpecialFolders("Desktop"^)
echo Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& Nom ^& ".lnk"^)
echo objShortCut.TargetPath = Dblquote(CheminApplication^)
echo ObjShortCut.IconLocation = "Winver.exe,0"
echo objShortCut.Save
echo End Sub
echo ^'**********************************************************************************************
echo ^'Fonction pour ajouter les doubles quotes dans une variable
echo Function DblQuote(Str^)
echo DblQuote = Chr(34^) ^& Str ^& Chr(34^)
echo End Function
echo ^'**********************************************************************************************
) > %vbsfile%
Start /Wait %vbsfile%
Del %vbsfile%
::****************************************************************************************************
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo Set shortcut = oWS.CreateShortcut("%USERPROFILE%\Desktop\unturned.exe.lnk") >> %SCRIPT%
echo shortcut.TargetPath = "D:\SteamLibrary\SteamApps\common\Unturned\Unturned.exe -batchmode -nographics +secureserver/TheServer" >> %SCRIPT%
echo shortcut.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
I have a batch that create shortcut based on the order of files, the problem is that when it comes to numbers he presents the following problem when passing the number 100.
01.mp4
02.mp4
03.rmvb
04.mp4
05.rmvb
06.rmvb
07.rmvb
08.rmvb
09.rmvb
10.rmvb
100.mp4
101.mp4
102.mp4
103.mp4
104.mp4
105.mp4
106.mp4
107.mp4
108.mp4
109.mp4
11.rmvb
I searched here and found various methods however the script I use works with folders and files that sometimes use accents, & and/or !
Example: C:\Séries & Movies\Remix!.mkv (Brazil and use E place of and).
I wonder if there is any way to check the content and organize it can be properly before you save it in .ini or after saving the same in .ini.
Observations:
The folder path is loaded the first time the Set command.
After entering the path he saved in an .ini file and always loaded.
The Script list only files within the directory does not list subfolders and files and folders within it.
The script needs other files to work the download link is below:
https://www.mediafire.com/?zcoybkfo8k4nm1t
My Full Code:
#Echo off
Title Create shortcuts in alphabetical order
mode con:lines=3 cols=25
Color 1f
CD /D "%~dp0"
If Exist "Files\command.ini" For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
If Exist "Files\Files.ini" Goto shortcuts
If Exist "Files\command.ini" Goto shortcuts
If Exist "Files\Config.ini" Goto shortcuts
for %%F in (""%1"") do Set "location-of-files=%%~F"
for %%F in ("%location-of-files%") do IF "%%~F" NEQ """" Set "location-of-files=%location-of-files:"=%" & Set Number=1 & Goto LocationofFiles2
:LocationofFiles
mode con:lines=18 cols=78
Set "location-of-files=r1u4unoiwqa6">nul 2>&1
cls
echo Location of Files
Set /p location-of-files="¯ Location of Files: "
Set "location-of-files=%location-of-files:"=%"
Set Number=1
IF "%location-of-files%"=="r1u4unoiwqa6" Goto LocationofFiles
:LocationofFiles2
mode con:lines=18 cols=78
Set "Menu=">nul 2>&1
cls
for %%F in ("%location-of-files%") do Echo %%~F
echo 1(Yes) 2(No)
Set/p Menu="¯ Menu: "
IF "%Menu%"=="1" Goto Iniciar
IF "%Menu%"=="2" Goto LocationofFiles
Goto LocationofFiles2
:Iniciar
if not exist "%location-of-files%" Cls & Start /Wait Files\Error.vbs & Goto LocationofFiles
:Name-AnimeSerie1
Set "Serie_Anime=">nul 2>&1
cls
echo Name Serie
Set /p Serie_Anime="¯ Name: "
IF "%Serie_Anime%"=="" Goto Name-AnimeSerie1
:Name-AnimeSerie2
Set "Menu=">nul 2>&1
cls
for %%F in ("%Serie_Anime%") do Echo %%~F
echo 1(Yes) 2(No)
Set/p Menu="¯ Menu: "
IF "%Menu%"=="1" Goto shortcuts
IF "%Menu%"=="2" Goto Name-AnimeSerie1
Goto Name-AnimeSerie2
:shortcuts
If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
If Not Exist "%location-of-files%" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1 & Goto end
Dir /a-d /b "%location-of-files%" >Files\Files.ini
Echo r1u4unoiwqa6.ending >>Files\Files.ini
Start "exclamation01" /Min /Wait "Files\exclamation01.vbs">nul 2>&1
Set location-of-files > Files\Config.ini
Set Serie_Anime >> Files\Config.ini
Set Number > Files\command.ini
If Exist "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1
setlocal EnableDelayedExpansion
For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
set "cmd=findstr /R /N "^^" Files\Files.ini | find /C ":""
for /f %%a in ('!cmd!') do set Numbers=%%a
set lines=%Number%
set Atual=1
for /f "delims=" %%a in ('type Files\Files.ini') do (
for %%b in (!lines!) do (
if !Atual!==%%b Set "Ep1=%%a"
)
set /a "Atual = Atual + 1"
)
Set "Ep2=%Ep1%"
set "find=*."
call set delete=%%Ep2:!find!=%%
call set Ep2=%%Ep2:!delete!=%%
Set Ep2=%Ep2:.=%
Set Ep1 > Files\command.ini
Set Ep2 >> Files\command.ini
Set lines >> Files\command.ini
Set Number >> Files\command.ini
endlocal
Start "exclamation02" /Min /Wait "Files\exclamation02.vbs">nul 2>&1
For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
IF "%Ep2%"=="r1u4unoiwqa6" Goto end
Start /Min /Wait Files\Shortcut.exe /F:"C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" /A:C /t:"%location-of-files%\%Ep1%" /D:"Episode %Serie_Anime%">nul 2>&1
If Not Exist "C:\Users\%username%\Desktop\[ shortcuts ].lnk" Echo %Serie_Anime%>Files\shortcut.ini & Start /Min /Wait Files\shortcut.vbs>nul 2>&1
Set /A Number = %lines% + 1
:::::::::::::::::::::::::::::::::::::::::::::
Set location-of-files > Files\Config.ini
Set Serie_Anime >> Files\Config.ini
:::::::::::::::::::::::::::::::::::::::::::::
Set Ep1 > Files\command.ini
Set Ep2 >> Files\command.ini
Set Number >> Files\command.ini
:::::::::::::::::::::::::::::::::::::::::::::
Exit
:end
If Not Exist "%location-of-files%" Start /Wait Files\PDoM.vbs>nul 2>&1
If Exist "%location-of-files%" Start /Wait Files\ending.vbs>nul 2>&1
If Exist "%location-of-files%" Start "Anime" "%location-of-files%">nul 2>&1
Del /q "Files\Files.ini">nul 2>&1
Del /q "Files\shortcut.ini">nul 2>&1
Del /q "Files\command.ini">nul 2>&1
Del /q "Files\Config.ini">nul 2>&1
Set "location-of-files=">nul 2>&1
Set "Serie_Anime=">nul 2>&1
Set "lines=">nul 2>&1
Set "Ep1=">nul 2>&1
Set "Ep2=">nul 2>&1
Goto LocationofFiles
Part where you need to use the DIR:
:shortcuts
If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
If Not Exist "%location-of-files%" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1 & Goto end
Dir /a-d /b "%location-of-files%" >Files\Files.ini
Echo r1u4unoiwqa6.ending >>Files\Files.ini
Start "exclamation01" /Min /Wait "Files\exclamation01.vbs">nul 2>&1
Set location-of-files > Files\Config.ini
Set Serie_Anime >> Files\Config.ini
Set Number > Files\command.ini
If Exist "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1
setlocal EnableDelayedExpansion
For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
set "cmd=findstr /R /N "^^" Files\Files.ini | find /C ":""
for /f %%a in ('!cmd!') do set Numbers=%%a
set lines=%Number%
set Atual=1
for /f "delims=" %%a in ('type Files\Files.ini') do (
for %%b in (!lines!) do (
if !Atual!==%%b Set "Ep1=%%a"
)
set /a "Atual = Atual + 1"
)
Set "Ep2=%Ep1%"
set "find=*."
call set delete=%%Ep2:!find!=%%
call set Ep2=%%Ep2:!delete!=%%
Set Ep2=%Ep2:.=%
Set Ep1 > Files\command.ini
Set Ep2 >> Files\command.ini
Set lines >> Files\command.ini
Set Number >> Files\command.ini
endlocal
Thanks in advance.
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set rs = CreateObject("ADODB.Recordset")
If LCase(Arg(1)) = "n" then
With rs
.Fields.Append "SortKey", 4
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
If IsNumeric(Sortkey) = False then
Set RE = new Regexp
re.Pattern = "[^0-9\.,]"
re.global = true
re.ignorecase = true
Sortkey = re.replace(Sortkey, "")
End If
If IsNumeric(Sortkey) = False then
Sortkey = 0
ElseIf Sortkey = "" then
Sortkey = 0
ElseIf IsNull(Sortkey) = true then
Sortkey = 0
End If
.AddNew
.Fields("SortKey").value = CSng(SortKey)
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "d" then
With rs
.Fields.Append "SortKey", 4
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
If IsDate(Sortkey) = False then
Set RE = new Regexp
re.Pattern = "[^0-9\\\-:]"
re.global = true
re.ignorecase = true
Sortkey = re.replace(Sortkey, "")
End If
If IsDate(Sortkey) = False then
Sortkey = 0
ElseIf Sortkey = "" then
Sortkey = 0
ElseIf IsNull(Sortkey) = true then
Sortkey = 0
End If
.AddNew
.Fields("SortKey").value = CDate(SortKey)
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "t" then
With rs
.Fields.Append "SortKey", 201, 260
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
.AddNew
.Fields("SortKey").value = SortKey
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
ElseIf LCase(Arg(1)) = "tt" then
With rs
.Fields.Append "SortKey", 201, 260
.Fields.Append "Txt", 201, 5000
.Open
Do Until Inp.AtEndOfStream
Lne = Inp.readline
SortKey = Trim(Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))))
.AddNew
.Fields("SortKey").value = SortKey
.Fields("Txt").value = Lne
.UpDate
Loop
If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
.Sort = SortColumn
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With
End If
To use
cscript //nologo script.vbs sort {n|d|t|tt} {a|d} startcolumn endcolumn < input.txt > output.txt
Options
n - extracts a number from the columns specified. Looks for the first number.
d - extracts a time or date from the columns specified. Looks for the first date.
t - extracts a text string including spaces from the columns specified.
tt - extracts a text string discarding leading and trailing spaces from the columns specified.
a - sorts acending
d - sorts decending
startcolumn - the starting column, the first character is column 1
endcolumn - the ending column
This is what command line synax means
The following table describes the notation used to indicate command-line syntax.
Notation Description
Text without brackets or braces
Items you must type as shown
<Text inside angle brackets>
Placeholder for which you must supply a value
[Text inside square brackets]
Optional items
{Text inside braces}
Set of required items; choose one
Vertical bar (|)
Separator for mutually exclusive items; choose one
Ellipsis (…)
Items that can be repeated
After much searching, I found the hybrid JScript/batch utility called REPL.BAT the dbenham and got what I wanted, based on my code the script is:
Del /q "Files\Files.ini">nul 2>&1
for /f "tokens=2 delims=:" %%F in (
'dir /b /a-d "%location-of-files%\*.*"^|Files\repl "^(\w+).*" "00000$1:$&" a^|Files\repl ".*(\d{5}:)" "$1"^|sort'
) do echo %%F >> Files\Files.ini
I have a batch file which is used by dragging a folder containing .mp3s into the batch.
#echo off
cd %~dp0
setlocal enabledelayedexpansion enableextensions
set FLDR="%1"
if not defined FLDR ( echo Drag a folder to the batch to play its contents.
pause
goto:EOF )
for %%x in (%FLDR%\*.mp3) do set "MP3=!MP3! "%%x""
mp3player %MP3%
pause
It works fine with actual folders, but when dragging shortcuts, the variable %FLDR% ends up as "c:\link location\folder.lnk" instead of the actual folder location.
I have no idea how to get around this.
Here is a way to get the target using a little hybrid VBS/Batch file function.
#echo off
setlocal
Call :GetTarget "%~1" tgt
echo %tgt%
pause
exit /b
:GetTarget
#echo off & setlocal
set gt=%temp%\_.vbs
echo set WshShell = WScript.CreateObject("WScript.Shell")>%gt%
echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>%gt%
echo wscript.Echo Lnk.TargetPath>>%gt%
set script=cscript //nologo %gt%
For /f "delims=" %%a in ( '%script% "%~1"' ) do set target=%%a
del %gt%
endlocal & set %~2=%target%
exit /b
HYBRID SCRIPT! No silly little temporary files.
::'<SUB>#echo off
::'<SUB>set shortcut=%~1
::'<SUB>if not defined shortcut goto 'usage
::'<SUB>if not %shortcut:~-4%==.lnk (if not %shortcut:~-4%==.url (set errorlevel=1
::'<SUB>goto 'usage ))
::'<SUB>if not exist %shortcut% (echo Error: Nonexistent shortcut
::'<SUB>set errorlevel=1
::'<SUB>goto:EOF )
::'<SUB>setlocal
::'<SUB>for /f "delims=" %%T in ('cscript //nologo //e:vbs %~nx0 "%shortcut%"') do set thing=%%T
::'<SUB>endlocal & set shortcut=%thing%
::'<SUB>goto:EOF
:'usage
::'<SUB>echo command-line shortcut redirect utility
::'<SUB>echo Usage: shortcut [file.lnk ^| file.url]
::'<SUB>echo The resulting link will be output to the %%shortcut%% variable.
::'<SUB>goto:EOF
set WshShell = WScript.CreateObject("WScript.Shell")
set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))
wscript.Echo Lnk.TargetPath
Where <SUB> indicates the substitute character.
UPDATE: I found a much fully vbscript solution over at Wayne's World of IT, and modified it slightly to suit my needs:
If WScript.Arguments.UnNamed.Count = 1 Then
strShortcut = WScript.Arguments.UnNamed(0)
Else
WScript.Echo "Please supply the name of an lnk file or directory to read, eg c:\test.lnk or c:\shortcuts"
WScript.Quit(1)
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strShortCut) Then
Set objFolder = objFSO.getFolder(strShortcut)
For Each objfile in objFolder.Files
If objfile.type = "Shortcut" Then
Call Readshortcut(objFile.Path, strProperties)
dtmCreationDate = objFile.DateCreated
WScript.Echo dtmCreationDate & "," & strProperties
End If
Next
ElseIf objFSO.FileExists(strShortCut) Then
Call Readshortcut(strShortcut, strProperties)
WScript.Echo strProperties
Else
WScript.Echo "Error: Could not read '" & strShortcut & "'"
WScript.Quit(2)
End If
Set objFSO = Nothing
Function Readshortcut(ByRef strShortcut, ByRef strProperties)
set objWshShell = WScript.CreateObject("WScript.Shell")
set objShellLink = objWshShell.CreateShortcut(strShortcut)
strProperties = objShellLink.TargetPath & " " & objShellLink.Arguments
Set objShellLink = Nothing
Set objWshshell = Nothing
End Function