Jump to content

GUI to build a batch file with a command using user input


Recommended Posts

Hello!

I need a gui to ask the user to give in a chosen path and with that path build a batch file containing this:

@echo off

for /F "tokens=1-4 delims=/- " %%A in ('date/T') do md c:\usergiven\%%A%%B%%C

for /F "tokens=1-4 delims=/- " %%A in ('date/T') do move c:\usergiven\*.* c:\usergiven\%%A%%B%%C

cls

exit

Would this be possible via vbs or anything very basic that does not require the user to install any suits or applications? Needs to be editable for future changes of parameters.

Many thanx for any help and pointers, am totally new to this.

Deana

Link to comment
Share on other sites


You have a couple of simple options

Here is a VBS script that uses a loop to get the user input.

Save As UserInputDemo.vbs


Dim A1 :
'-> Loop To Get The User Input
Do While A1 = ""
'-> Inputbox To Get User Input
A1 = InputBox( _
"Some Text Here With Some Details About What" & vbCrLf & _
"You Want The User To Do. Display An Example" & vbCrLf & _
"Some_Drive_Letter:\Path_Of_Folder\Folder" & vbCrLf & vbCrLf & _
"Some Instruction On How To Quit This." & vbCrLf & _
"Type Either Exit Or Quit To Do Nothing")
'-> Code To Exit The Script
If A1 = "quit" Or A1 = "exit" Then
WScript.Quit(0)
End If
'-> Check To Make Sure The Pathway Like This EG C:\Abc Would Be Correct
If InStr(A1, ":\") And Len(A1) > 5 Then
MsgBox "Code Here To Do What You Want."
WScript.Quit(1)
Else
'-> Check To Make Sure The Pathway Like This EG C:\Ab Would Fail
MsgBox "This Is Not A Valid Pathway"
A1 = ""
End If
Loop

The other way would be a HTA, this HTA I coded in the Brows For Folder Dialog as a way to get a pathway.

Save As DemoUserInput.hta


<TITLE> « Demo Get User Input »</TITLE>
<HTA:APPLICATION ID='DemoGetUserInput'
Scroll='No'
SCROLLFLAT ='No'
SingleInstance='Yes'
SysMenu='Yes'
MaximizeButton='No'
MinimizeButton='Yes'
Border='Thin'
BORDERSTYLE ='complex'
INNERBORDER ='Yes'
Caption='Yes'
WindowState='Normal'
APPLICATIONNAME='GetUserInputDemo'
Icon='%SystemRoot%\explorer.exe'>
<!-- For How The Window Is Display -->
<STYLE Type='text/css'>
Body
{
Font-Size:12.55pt;
Font-Weight:Bold;
Font-Family:Segoe Ui, Lucida Console, Arial, Tahoma, Comic Sans MS;
Color:Black;
BackGround-Color:Transparent;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#ece6e0',EndColorStr='#c0bab4');
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:4;
Margin-Right:4;
Padding-Top:1;
Padding-Bottom:1;
Padding-Left:4;
Padding-Right:4;
Text-Align:Center;
Vertical-Align:Top;
Border-Top:0px Transparent;
Border-Bottom:0px Transparent;
Border-Left:0px Transparent;
Border-Right:0px Transparent;
}
TD
{
Font-Size:8.55pt;
Font-Weight:Bold;
}
TD.Big
{
Font-Size:9.55pt;
Font-Weight:Bold;
}
.Txt
{
Font-Size:8.25pt;
Font-Weight:Bold;
}
BUTTON
{
Width:71pt;
Height:14pt;
Cursor:Hand;
Font-Size:8.25pt;
Font-Weight:Bold;
Color:#001137;
Text-Align:Center;
Vertical-Align:Middle;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='AliceBlue',endColorStr='LightSlateGray');
Border-Top:0px Transparent;
Border-Bottom:0px Transparent;
Border-Left:0px Transparent;
Border-Right:0px Transparent;
Padding-Top:0;
Padding-Bottom:2;
Padding-Left:0;
Padding-Right:0;
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:1;
Margin-Right:1;
BackGround-Color:Transparent;
}
</STYLE>
<!-- VBS Script Language -->
<script Language="VBScript">
Const MyComp = &H11&
'-> Resize And Move Window
Dim Wth :Wth = int(429)
Dim Hht :Hht = int(229)
window.ResizeTo Wth, Hht
MoveTo ((Screen.Width / 2) - (Wth / 2)),((Screen.Height / 2) - (Hht / 2))
'-> Vbs Object
Dim Shl :Set Shl = CreateObject("Shell.Application")
'-> Varible For Use In Script
Dim UserObject
'-> Open The Brow For Folder DiaLOG
Function BrowsForTheFolder()
Dim Fld :Set Fld = Shl.BrowseForFolder(0, "Select The User Folder",16,MyComp)
If Fld Is Nothing Then
alert "User Cancel Brow For Dialog"
Else
Set UserObject = Fld.Self
' alert UserObject.Path
VarToPass.value = UserObject.Path
End If
End Function
'-> Submit Button Function
Function SetUserChoice()
'-> Check To Make Sure The Pathway Like This EG C:\Abc Would Be Correct
If Not VarToPass.value = "" Then
If InStr(VarToPass.value, ":\") And Len(VarToPass.value) > 5 Then
alert "Code Here"
Else
'-> Check To Make Sure The Pathway Like This EG C:\Ab Would Fail
alert "This Is Not A Valid Pathway"
VarToPass.value = ""
End If
Else
alert "This Is No Pathway Listed" & Vbcrlf & _
"Please Type In The Pathway" & Vbcrlf & _
"Or Use The Brows Button To" & Vbcrlf & _
"Supply A Valid Pathway"
End If
End Function
</SCRIPT>
<BODY Scroll='No'>
<!-- Top Text Display -->
<TABLE>Title That You Want To Display</TABLE>
<!-- Main Text Display -->
<TABLE
Style='Width:99%;Height:32pt;Text-Align:Left;Margin-Top:3pt;Padding-Left:2pt;'><TD>
Some Text Here With Some Details About What
You Want The User To Do. Some More Detail To
Help The User. Or Press The Brows Button To
Open A Brows For Folder Dialog, To Select
The Target Folder.
</TD></TABLE>
<!-- Example Text Display -->
<TABLE Style='Width:99%;Text-Align:Left;'><TD Class='Big'>
<FONT Style='Color:DarkGreen;'>Example » </FONT>
Some_Drive_Letter:\Path_Of_Folder\Folder
</TD></TABLE>
<!-- Textbox Input Area -->
<TABLE Style='Width:99%;Text-Align:Center;'><TD>
<INPUT Type='Text' Name='VarToPass' Size='60' Class='Txt'>
</TD></TABLE>
<!-- Button Area -->
<TABLE Style='Width:99%;Text-Align:Center;'><TD>
<BUTTON ID='BrowForFolder' OnClick='BrowsForTheFolder()'>Brows</BUTTON>
<BUTTON ID='SubmitPathWay' OnClick='SetUserChoice()'>Submit</BUTTON>
<BUTTON ID='CloseTheWindow' OnClick='window.close()'>Close</BUTTON>
</TD></TABLE>
</BODY>

DemoGetUserInput.png

Link to comment
Share on other sites

Thank you gunsmokingman! The codes look fabulous and way better than what I was trying so far but my main problem is not to just get the input from the user, but that I need this input to be written into a batch file into the middle of a lot of text with special characters. With other words the vbs script would have to create the main .bat file that I use with the code I wrote, so literally call the batch file writing the lines but with the path that it got from the user. This would start a chain of batch-es running, that will do a lot of data manipulation but this initial one I need to run. I have to resort to this because we are talking about hundreds of batch-es already created.

So if the vbs gets the USERINPUT (I tried so far with strUserIn = InputBox, by far not as nice as yours, many thanx again) then it needs to write the text:

@echo off

for /F "tokens=1-4 delims=/- " %%A in ('date/T') do md USERINPUT\%%A%%B%%C

for /F "tokens=1-4 delims=/- " %%A in ('date/T') do move USERINPUT\*.* c:\usergiven\%%A%%B%%C

cls

exit

and save it as start.bat

As you can see the batch file holds a lot of special characters, so don't know how to parse it in there. I treid

strFileName = fs.BuildPath(Wscript.ScriptFullName & "\..", "start.bat")

however the USERINPUT\%%A%%B%%C part I don't know how to do, aka how do I write the userinput and after that the special characters %%A etc.

Many thanx again

Deana

Link to comment
Share on other sites

Think I solved it. I used your solution of inputbox because it looks much more elaborated than my tries and what I needed was:

Set fs = CreateObject("Scripting.FileSystemObject")

strFileName = fs.BuildPath(Wscript.ScriptFullName & "\..", "start.bat")

strFileName = fs.GetAbsolutePathName(strFileName)

Set ts = fs.OpenTextFile(strFileName, 2, True)

ts.WriteLine "@echo off"

ts.WriteLine ""

ts.WriteLine "for /F " & """tokens=1-4 delims=/- """ & " %%A in ('date/T') do md " & A1 & "\%%A%%B%%C"

ts.WriteLine "cls"

ts.WriteLine "exit"

ts.Close

So this part where I had to make it write the batch file and it had among others "-marks which I could not parse through. Thank you so much for your help again!! :)

Deana

Link to comment
Share on other sites

Why write a hta/vbs to take an input string to write a batch file!

Rewrite your batch file to take an input parameter, propagated from the input string and use a single scripting language for both tasks.

Link to comment
Share on other sites

If you where to use the hta, this is the value you would pass to the batch cmd.

VarToPass.value

To your batch cmd


for /F "tokens=1-4 delims=/- " %%A in ('date/T') do md VarToPass.value\%%A%%B%%C
for /F "tokens=1-4 delims=/- " %%A in ('date/T') do move VarToPass.value\*.* c:\usergiven\%%A%%B%%C

To code it in the hta


If InStr(VarToPass.value, ":\") And Len(VarToPass.value) > 5 Then
Dim Act :Set Act = CreateObject("Wscript.Shell")
Act.Run("Cmd.exe /C @Echo Off && for /F ""tokens=1-4 delims=/- "" %%A in ('date/T') do md " & VarToPass.value & "\%%A%%B%%C"),1,True
Act.Run("Cmd.exe /C @Echo Off && for /F ""tokens=1-4 delims=/- "" %%A in ('date/T') do move " & VarToPass.value & "\%%A%%B%%C"),1,True

Why write a hta/vbs to take an input string to write a batch file!

Here are some reasons

1:\ Better error handling then cmd promt.

2:\ Better looking GUI for the end user.

3:\ Easier to work with strings

Could you post the contents of your batch file, it might be easy to convert it to VBS.

If you want to get a string in cmd promt


@Echo Off
Title Get User Input

:Main
CLS
MODE 62,9
Color A9
Echo.
Echo Type in some string
SET /P UC=^>

If /I '%UC%'=='' GOTO NoText
If /I '%UC%'=='%UC%' GOTO Choice1

:NoText
CLS
Color CF
ECHO.
ECHO This was not valid
ping -n 3 127.0.0.1>nul
GOTO Main

:Choice1
CLS
Echo.
Echo %UC%
pause

Link to comment
Share on other sites

Gsm, my query was not to put down the use of hta/vbs it was that the batch file use appears to be the weak link.

The suggested batch files unreliable, (non-universal), method of creating the date for the folder name is my biggest worry. I'd suggest the use of the far more usable and superior js or vbs methods of creating the date part of the desired directory name. Then you can if necessary either output to another script, (batch or vbs/js), or run the commands directly, (either using cmd.exe as per your hta example above or better still directly utilising filesystemobject collection(s)).

Link to comment
Share on other sites

It's also a LOT easier to debug (you can step though code, set watches on variables, set breakpoints, etc), it's vastly more powerful, more robust, less cryptic in many ways, and it's also easy to have no GUI at all when desired. It's hardly perfect (the error handling still sucks quite badly IMO, not too keen on the VB syntax either -- jscript is a lot nicer/better than vbscript on both of these points) but it's still quite a step up from batch files.

And yes, as Yzöwl said date formats often change based on locales and settings. I personally stick to the ISO 8601 date format to prevent any issues.

Link to comment
Share on other sites

Another +1 for migrating off of .cmd scripts, and while using .js/.vbs is much more of a preferred method, powershell is where you want to do any sort of console scripting in today's day and age. For things you want GUI-based, HTAs with vbscript/jscript are still the easiest place to go (you could go .net + powershell, but at that point it's an app, not just an admin script ;)).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...