windows ".lnk" shortcuts and batches don't mix - batch-file

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

Related

using variables from VBScript in batch

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

how to loop through folders specified by text document on batch script

I have a list of folder paths that I want to loop through and get the files and creation date and then send a email notification that these files have been uploaded to the ftp. I have got everything working but I am having trouble looping through the folders to get the files. I think it loops through the text file but by the time i get to my second loop I think it is only looping through the last path that was in the text doc as the variable was getting overwritten. I tried enclosing the second for statement with parentheses for the first for statement but did not work. Here is my code:
scanFTPCLients.bat
#echo off
setlocal EnableDelayedExpansion
cls
#pushd %~dp0
set i=0
for /F "tokens=*" %%i in (Pathlist.txt) do (
set fp=%%i
set LIST=
for /r "%fp%" %%a in (*.*) do set i=i+1
set LIST=!LIST! ---%deptClient%--- %%~na ----UPLOAD TIME---- %%~ta
)
set LIST=%LIST:~1%
IF %i% NEQ 0 (wscript "%~dp0FTPFilesUploadedNotification.vbs")
popd
Pathlist.txt
\\vavm\CINICO\Incoming
\\vavm\CIS\Incoming
\\vavm\Forcht\Incoming
\\vavm\HPC\Incoming
\\vavm\K\Incoming
\\vavm\MWEmpCC\Incoming
\\vavm\National Labor Benefits\Incoming
\\vavm\PeriSons\Incoming
\\vavm\US\Incoming
\\vavm\K\Incoming
FTPFilesNotification.vbs
dim outputArray
dim inputText
dim message
inputText = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%LIST%")
outputArray = split(inputText, " ")
for each x in outputArray
message = message & x & vbCRLF
next
Set MyEmail=CreateObject("CDO.Message")
MyEmail.Subject="Clients Imported to System"
MyEmail.From="SYSTEMFUNCTION#mrsllc.org"
MyEmail.To="rickg#gmail.com"
MyEmail.TextBody= "The Following Clients have been imported to the system: " & vbCRLF & message
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="mail.org"
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="username"
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password"
MyEmail.Configuration.Fields.Update
MyEmail.Send
set MyEmail=nothing
For comparison, in PowerShell, it could be:
$emailSettings = #{
From = "you#example.org"
To = "you#example.org"
Subject = "Upload report"
SmtpServer = "yourmailserver"
}
$report = dir -Path #(gc pathlist.txt) | select FullName, CreationTime
Send-MailMessage #emailSettings -Body "$($report|ConvertTo-Html)" -BodyAsHtml
This shows how to loop through files in vbscript
'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
'Searchterm = Inputbox("Enter search term")
ProcessFolder DirName
Sub ProcessFolder(FolderPath)
On Error Resume Next
Set fldr = fso.GetFolder(FolderPath)
Set Fls = fldr.files
For Each thing in Fls
' Set contents = thing.OpenAsTextStream
' If err.number = 0 then
' If Instr(contents.readall, searchterm) > 1 then msgbox thing.path
' Else
' err.clear
' End If
msgbox Thing.Name & " " & Thing.DateLastModified
Next
Set fldrs = fldr.subfolders
For Each thing in fldrs
ProcessFolder thing.path
Next
End Sub
To send mail
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dc#gail.com"
emailObj.To = "dc#gail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
Set emailConfig = emailObj.Configuration
msgbox emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourUserName"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password1"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "Done"
To read a file line by line
On Error Resume Next
Set Fso = CreateObject("Scripting.FileSystemObject")
Set File = Fso.CreateTextFile("C:\myfile.txt", True)
If err.number <> 0 then
Wscript.Echo "Error: " & err.number & " " & err.description & " from " & err.source
err.clear
wscript.exit
End If
Do Until File.AtEndOfStream
Msgbox File.readline
Loop
Changed. Not sure what you want to do about sending lines (one at a time or send all when done). If one at a time, integrate into this. If all at once at end, then send output to a temp file and send it.
#echo off
pushd %~dp0
set /A Cnt=0
for /F "tokens=*" %%i in (Pathlist.txt) do (
echo i= %%i
for /f "usebackq tokens=*" %%a in (`Dir /s /b %%i\*.*`) do (
echo %%~na - %%~ta
set /A Cnt+=1
)
)
echo(Cnt=%Cnt%
pause
popd

Making Batch file code with VBScript to work with Unicode symbols

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

Garbage char returned by Shell.StdOut.ReadAll

Here is my code
Set objFSO = CreateObject("Scripting.FileSystemObject")
a = "#echo off && " & _
"pushd ""\\xxxxx.local\cfs\Development\Docs\Baseline"" &&" & _
"cls &&" & _
"dir /b /a-d &&" & _
"popd"
Set objShell = CreateObject("WScript.Shell").Exec("%comspec% /c " & a)
execStdOut = objShell.StdOut.ReadAll()
msgbox execStdOut
Basically I am trying to run Dir command on a network drive. It works fine, but the only issue is that the output contains a garbage character in the beginning. Not sure why?
Output
If I run the same steps in a batch file, the output is correct
The answer to your question is that cls is being captured by the StdOut stream and interpreted as an extended character. Get rid of it. You don't need it.
Just so I feel like I've done something, here's the script rewritten as a batch + JScript hybrid. Save it with a .bat extension if you want to mess with it.
#if (#CodeSection == #Batch) #then
#echo off
setlocal
if "%~1"=="shell" (
pushd "\\xxxxx.local\cfs\Development\Docs\Baseline"
dir /b /a-d
popd
goto :EOF
)
cscript /nologo /e:JScript "%~f0"
goto :EOF
#end // end batch / begin JScript
var oSH = WSH.CreateObject('Wscript.Shell'),
proc = oSH.Exec("cmd /c " + WSH.ScriptFullName + " shell");
oSH.popup(proc.StdOut.ReadAll());
As an added bonus, here's the same thing with a lovely "ding" noise added.
#if (#CodeSection == #Batch) #then
#echo off
setlocal
if "%~1"=="shell" (
pushd "\\xxxxx.local\cfs\Development\Docs\Baseline"
dir /b /a-d
popd
goto :EOF
)
cscript /nologo /e:JScript "%~f0"
goto :EOF
#end // end batch / begin JScript
var oSH = WSH.CreateObject('Wscript.Shell'),
noise = WSH.CreateObject('WMPlayer.OCX.7'),
proc = oSH.Exec("cmd /c " + WSH.ScriptFullName + " shell");
with (noise) {
URL = oSH.Environment('Process')('SYSTEMROOT') + '\\Media\\Windows Exclamation.wav';
Controls.play();
}
oSH.popup(proc.StdOut.ReadAll());
The ♀ character is ASCII control character Line Feed 0x0A, a residuum of CLI cls command. Just omit the "cls &&" & _ line in your code as follows:
a = "#echo off && " & _
"pushd ""\\xxxxx.local\cfs\Development\Docs\Baseline"" &&" & _
"dir /b /a-d &&" & _
"popd"

Sorting files by numerical order

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

Resources