Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need two codes working together

Status
Not open for further replies.

Lisa Johnson

Technical User
Feb 2, 2019
3
0
0
BR
Hi
I need two different codes working together

the first one is a VBS script:

Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("User.bat", False) ' False for ANSI, True for Unicode
Set shell = CreateObject( "WScript.Shell" )
username = shell.ExpandEnvironmentStrings( "%USERNAME%" )
file.WriteLine username

This will get the username and export it to a batch file, or a text file if the extension get changed to .txt

The second code is a batch file used to alter some attributes and permissions

setlocal enableextensions
cd /d "%~dp0"
@echo off
If EXIST "(The user name)" goto UNLOCK
if EXIST "User" goto LOCK
:UNLOCK
echo s| cacls "(The user name)" /G %USERNAME%:F
cls
attrib -H -S -R "(The user name)"
ren "(The user name)" "User"
goto END
:LOCK
ren "User" "(The user name)"
attrib +H +S +R "(The user name)"
echo s| cacls "(The user name)" /D %USERNAME%
goto END
:END
exit

I need that the VBS script executes this Batch file with the “Username” it got in the first place.
This will be part of a security useful application that I'm trying to put together.
All help will be very appreciated.
 
Hi Lisa,
If you get the USERNAME in your VBscript then you can pass it as command line argument to the batch script.
The batch script could be executed from Vbscript using the WScript.Shell command Run. Here is a little example:

I have this VBscript
user.vbs
Code:
wscript.echo "running VBscript: user.vbs"
set fso = CreateObject("Scripting.FileSystemObject")
set shell = CreateObject( "WScript.Shell" )
my_usr_name = shell.ExpandEnvironmentStrings( "%USERNAME%" )
' get environment variable
wscript.echo "username from system environment = '" & my_usr_name & "'"
' create batch command
my_cmd = "user.bat" & " " & my_usr_name
wscript.echo "now executong command : '" & my_cmd & "'"
' run the command
shell.run(my_cmd)
set shell = nothing
wscript.echo "end of VBScript: user.vbs"

and this batch file
user.bat
Code:
@echo off
echo * running batch script: user.bat
if  %1x==x  goto ARG_NOT_AVAIL
set usr=%1
echo   processing user '%usr%'
echo   ...
goto END

:ARG_NOT_AVAIL
echo Error: Username not passed as command line argument !

:END
echo * end of batch script: user.bat
pause

now when I open the command line window and run this command
Code:
cscript /NoLogo user.vbs

then VBscript runs producing the output
Code:
running VBscript: user.vbs
username from system environment = 'MIKROM'
now executong command : 'user.bat MIKROM'
end of VBScript: user.vbs

additionally other command line window opens where bat file runs
Code:
* running batch script: user.bat
  processing user 'MIKROM'
  ...
* end of batch script: user.bat
Press any key to continue . . .
 
Hi Mikrom
Thank you very much for your reply. I cannot give you many details about this application I'm trying to put together because in the end it will be a commercial software. A super Vault with a very strong encryption. This VBS file, together with the batch file will be a small part of the application. It is intended to be applied in a common folder that will work like if it was the main user folder. This will be used to hide an important part of the program. And that is the reason that it must have the username on it. Having said this and because the attributes and permissions that the batch file must apply to that common folder, the process must run in total silent mode. Now, and because the batch file must have a folder name to apply its function to that folder. It needs to get the name from the VBS file, all in the right places, so it can be executed by the VBS file properly.
 
>I need that the VBS script executes this Batch file with the “Username” it got in the first place

you could always rewrite the batch file as VBScript ...
 
VBS script executes this Batch file with the “Username” it got
IMO this is what I posted above: the VBscript user.vbs executes the batch file user.bat with username I got in VBscript from system environment. Then the batch file can do something with the username.
 
Hi Mikrom
Well, I couldn't figure it out. Is there any other language with which one could make an executable file to apply this required definitions in silent mode. I mean like an ON/OFF system. Please help me with this and I'll include your name in my program's credits ... Now for you to understand better what I need, just create a common folder in your desktop like “New Folder” etc. and then rename it with the name you want for example “user” and this special property, like this:
“User.{59031a47-3f72-44a7-89c5-5595fe6b30ee}” Unbelievable isn't it? After this, if you rename or modify one single letter after the (dot) it will become a common folder again, in addition to this I use the attrib command to make it a read only system folder and the cacls command to modify the ACL's in a way that if you apply it to a common folder you will lose all access to it, but in this case and because it will be a Windows special folder, you will be able to open it, but you won't be able to rename it nor delete it, copy it or anything else. Having performed this test you will understand what I really need to be done and that is a ON/OFF switch to this system no matter what language you will use to write it.
 
>Unbelievable isn't it?

Not to everyone! The reason this works is because of the way the shell (i.e your desktop) interprets certain GUIDS, some of which have a special meaning to it - the one you have selected happens to represent the User folder, but there are others, eg A8CDFF1C-4878-43be-B5FD-F8091C1C60D0 - Documents

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top