szot Posted June 28, 2010 Posted June 28, 2010 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 offfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do md c:\usergiven\%%A%%B%%Cfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do move c:\usergiven\*.* c:\usergiven\%%A%%B%%Ccls exitWould 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
gunsmokingman Posted June 30, 2010 Posted June 30, 2010 You have a couple of simple optionsHere 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 LoopThe 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>
szot Posted June 30, 2010 Author Posted June 30, 2010 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 offfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do md USERINPUT\%%A%%B%%Cfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do move USERINPUT\*.* c:\usergiven\%%A%%B%%Ccls exitand save it as start.batAs 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 againDeana
szot Posted June 30, 2010 Author Posted June 30, 2010 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
Yzöwl Posted June 30, 2010 Posted June 30, 2010 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.
gunsmokingman Posted June 30, 2010 Posted June 30, 2010 If you where to use the hta, this is the value you would pass to the batch cmd.VarToPass.valueTo your batch cmdfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do md VarToPass.value\%%A%%B%%Cfor /F "tokens=1-4 delims=/- " %%A in ('date/T') do move VarToPass.value\*.* c:\usergiven\%%A%%B%%CTo 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 reasons1:\ Better error handling then cmd promt.2:\ Better looking GUI for the end user.3:\ Easier to work with stringsCould 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 OffTitle Get User Input:MainCLSMODE 62,9Color A9Echo.Echo Type in some stringSET /P UC=^>If /I '%UC%'=='' GOTO NoTextIf /I '%UC%'=='%UC%' GOTO Choice1:NoTextCLSColor CFECHO.ECHO This was not validping -n 3 127.0.0.1>nulGOTO Main:Choice1CLSEcho.Echo %UC%pause
Yzöwl Posted June 30, 2010 Posted June 30, 2010 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)).
CoffeeFiend Posted June 30, 2010 Posted June 30, 2010 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.
cluberti Posted June 30, 2010 Posted June 30, 2010 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 ).
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now