Read text content from docx file using batch script - batch-file

I am able to read text from txt files in Windows batch script. But I can't find a way to read the data from docx files. How can I do it?

Put in same folder where are the File.docx that you want get txt:
docx2txt.cmd file.docx
or use:
Drag_and_Drop
result in a notepad txt opened with txt in file same name from .docx but in .txt
docx2txt.cmd updated
#echo off & setlocal enabledelayedexpansion
((
echo/"%~1"| findstr /lic:["\.docx\""$] >nul) && (
for /f "tokens=* delims= " %%i in ('echo/%~1') do (
set "_Docx2txt=%%~dpni.txt" && >nul copy /y "!_Docx2txt:~0,-4!.docx" "%temp%\Docx.zip")
if /i not exist "%temp%\Docx.zip" set "_Msg_Err=%~1 not valid^!" && goto :_error_:
set "_Docx_zDir_=%temp%\Docx_Zip"
set "_Docx_uZip_=%temp%\Docx.zip"
set "_replace_00=openxmlformats"
set "_replace_01=urn:schemas-microsoft-com:vml"
set "_replace_02=urn:schemas-microsoft-com:office:word"
set "_replace_03=urn:schemas-microsoft-com:office:office"
set _Run_CScript="%Windir%\System32\CScript.exe" //nologo
set _Break_line=!_Run_CScript! "%temp%\Break_line.vbs"
set _Replc_Qute=!_Run_CScript! "%temp%\Replc_Qute.vbs"
set _Replc_Dots=!_Run_CScript! "%temp%\Replc_Dots.vbs"
set _Replc_Tag1=!_Run_CScript! "%temp%\Replc_Tag1.vbs"
set _Replc_Tag2=!_Run_CScript! "%temp%\Replc_Tag2.vbs"
set _Find_Replc=!_Run_CScript! "%temp%\Find_Rep.vbs"
set _UnZip_Docx=!_Run_CScript! "%temp%\UnZip.vbs"
set _Drop_Lines=^
<nul & rem .:| This blank line is needed to do this job! So, do not remove it! |:.
) 2>nul || (
:_error_:
cls & echo/ & color F4 & set "_Arg_PS=New-Object -ComObject Wscript.Shell" & echo/
set "_Arg_Err=Valid: Some_Document.Docx" & set "_Err=E R R O R ^! Argument missing: "
if not defined _Msg_Err set _Msg_Err="%~0" [+ !_Arg_Err!]& echo/ Well, something is really wrong^^!
echo/ & powershell ^(!_Arg_PS!^).Popup^("""!_Msg_Err!""",0,"""!_Err!""",0x10^) 2>nul >nul
echo/ Use: !_Msg_Err! & timeout /t -1 2>nul >nul & color 0A & goto :eof
)) 2>nul
call :_write_vbs_files_: & type nul >"!_Docx2txt!"
(rmdir /q /s "!_Docx_zDir_!" & ping 127.1 -n 1 >nul && mkdir "!_Docx_zDir_!" || mkdir "!_Docx_zDir_!") 2>nul >nul
!_UnZip_Docx! && type nul >"!_Docx_zDir_!\Docx_Text.txt" & type nul >"!_Docx_zDir_!\Docx.tmp"
copy /y "!_Docx_zDir_!\word\document.xml" "!_Docx_zDir_!\Docx.tmp" >nul
for %%r in ("/^>^</","^<w:t xml:space^=","/^>","^</a:^","w:rPr^>","^<w:r","^</w:t^>","xmlns:","^<w:instrText", "^</w:instrText"^
) do !_Find_Replc! "!_Docx_zDir_!\Docx.tmp" %%r "!_Drop_Lines!"
for /l %%l in (0 1 3) do !_Find_Replc! "!_Docx_zDir_!\Docx.tmp" "!_replace_0%%l!" "!_Drop_Lines!"
!_Replc_Tag1! "!_Docx_zDir_!\Docx.tmp"
!_Replc_Tag2! "!_Docx_zDir_!\Docx.tmp"
!_Replc_Qute! "!_Docx_zDir_!\Docx.tmp"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" chr^(47^)^&"schemas"^&chr^(46^)^&"microsoft"^&chr^(46^)^&"com"^&chr^(47^)^&^chr^(47^) "!_Drop_Lines!"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" chr^(47^)^&"schemas"^&chr^(46^)^&"openxmlformats"^&chr^(46^) "!_Drop_Lines!"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" chr^(46^)^&"org"^&^chr^(47^)^&^chr^(47^) "!_Drop_Lines!"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" "^<w:t^>" "1#2##3#4#"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" "^<a:t^>" "1#2##3#4#"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" chr^(32^)^&"1#2##3#4#" "1#2##3#4#"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" "1#2##3#4#"^&char^(32^) "1#2##3#4#"
!_Find_Replc! "!_Docx_zDir_!\Docx.tmp" "^>^<" "!_Drop_Lines!"
!_Replc_Dots! "!_Docx_zDir_!\Docx.tmp"
for %%r in (org/officeDocument,org/officeDocument/,org/,relationships,wordprocessingDrawing,http://schemas.,^
presentationml,spreadsheetDrawing,speadsheetDrawing,wordprocessingml,drawingml,wordprocessingShape^
) do !_Find_Replc! "!_Docx_zDir_!\Docx.tmp" %%r "!_Drop_Lines!"
for %%r in (chr^(60^)^&chr^(63^),chr^(63^)^&chr^(62^),chr^(9^)) do !_Find_Replc! "!_Docx_zDir_!\Docx.tmp" %%r "!_Drop_Lines!"
type "!_Docx_zDir_!\Docx.tmp"|findstr "1#2##3#4#">>"!_Docx_zDir_!\Docx_Text.txt"
!_Find_Replc! "!_Docx_zDir_!\Docx_Text.txt" "1#2##3#4#" "!_Drop_Lines!"
!_Break_line! "!_Docx_zDir_!\Docx_Text.txt"
!_Find_Replc! "!_Docx_zDir_!\Docx_Text.txt" chr^(32^)^&chr^(46^) "!_Drop_Lines!"
!_Find_Replc! "!_Docx_zDir_!\Docx_Text.txt" chr^(32^)^&chr^(32^)^&chr^(46^) "!_Drop_Lines!"
type "!_Docx_zDir_!\Docx_Text.txt"| more /e /s /p +3 | findstr /rc:"[\ ]" | findstr /vb "\ \." >>"!_Docx2txt!"
!_Find_Replc! "!_Docx_zDir_!\Docx_Text.txt" "xml"^&chr^(58^) "!_Droplines!
!_Find_Replc! "!_Docx_zDir_!\Docx_Text.txt" "space"^&chr^(61^) "!_Drop_Lines!"
start /b notepad.exe "!_Docx2txt!" && rmdir /q /s "!_Docx_zDir_!" 2>nul >nul
>nul (for %%D in (Find_Rep Replc_Tag1 Replc_Tag2 Replc_Qute Replc_Dots Break_Line UnZip) do del /q /f "%temp%\%%D.vbs"
del /q /f "%temp%\docx.zip") & goto :_end_of_file_:
:_write_vbs_files_:
>"%temp%\Find_Rep.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = WScript.Arguments^(0^)
echo/ strOldText = WScript.Arguments^(1^)
echo/ strNewText = WScript.Arguments^(2^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\Replc_Tag1.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = Wscript.Arguments^(0^)
echo/ strOldText = ^(chr^(60^)^&chr^(87^)^&chr^(58^)^&chr^(84^)^&chr^(62^)^)
echo/ strNewText = ^(vbCr^&vbLf^&chr^(34^)^&preserv^&chr^(34^)^&chr^(62^)^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ objFile.Close
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\Replc_Tag2.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = Wscript.Arguments^(0^)
echo/ strOldText = ^(chr^(34^)^&"preserve"^&chr^(34^)^&chr^(62^)^)
echo/ strNewText = ^("1"^&chr^(35^)^&"2"^&chr^(35^)^&chr^(64^)^&"3"^&chr^(35^)^&"4"^&chr^(35^)^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ objFile.Close
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\Replc_Qute.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = Wscript.Arguments^(0^)
echo/ strOldText = ^(chr^(34^)^&"preserve"^&chr^(34^)^&chr^(62^)^)
echo/ strNewText = ^("1"^&chr^(35^)^&"2"^&chr^(35^)^&chr^(64^)^&"3"^&chr^(35^)^&"4"^&chr^(35^)^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ objFile.Close
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\Replc_Dots.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = Wscript.Arguments^(0^)
echo/ strOldText = ^(chr^(46^)^&^chr^(0^)^)
echo/ strNewText = ^(chr^(46^)^&chr^(13^)^&"1"^&chr^(35^)^&"2"^&chr^(35^)^&chr^(64^)^&"3"^&chr^(35^)^&"4"^&chr^(35^)^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ objFile.Close
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\Break_Line.vbs"^
(
echo/ Const ForReading = 1
echo/ Const ForWriting = 2
echo/ strFileName = Wscript.Arguments^(0^)
echo/ strOldText = ^(chr^(46^)^)
echo/ strNewText = ^(chr^(46^)^&vbCr^&vbLf^)
echo/ Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForReading,ForReading^)
echo/ strText = objFile.ReadAll
echo/ objFile.Close
echo/ strNewText = Replace^(strText, strOldText, strNewText^)
echo/ Set objFile = objFSO.OpenTextFile^(strFileName, ForWriting^)
echo/ objFile.Write strNewText 'WriteLine adds extra CR/LF
echo/ objFile.Close
)
>"%temp%\UnZip.vbs"^
(
echo/ ZipFile="!_Docx_uZip_!"
echo/ ExtractTo="!_Docx_zDir_!\"
echo/ set objShell = CreateObject^("Shell.Application"^)
echo/ set FilesInZip=objShell.NameSpace^(ZipFile^).items
echo/ Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo/ objShell.NameSpace^(ExtractTo^).CopyHere^(FilesInZip^)
echo/ Set fso = Nothing
echo/ Set objShell = Nothing
)
exit /b
:_end_of_file_:

Related

Batch exe to shortcut

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 ----------------------------------------------------------------------------------------------------

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

Trying to create a shortcut in batch for server

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%

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

windows ".lnk" shortcuts and batches don't mix

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

Resources