My external hard drive was recently affected by the recycler.exe virus when I lent it to a friend. The virus affects only external drives. It changes the folders into shortcuts to those folders via an EXE file that it creates.
I am searching for a script to change all the shortcut targets to K:\{shortcut name}. However, I don't know scripting and in the last two days that I have tried to learn scripting, I am not sure which one I should use. VBScript seems the best option, but that's just my opinion.
Problem:
For example, earlier I had a folder called 'Anime'. Now I have a shortcut linking to that folder with the following target:
%windir%\system32\cmd.exe /c "start %cd%RECYCLER\894133bf.exe &&%windir%\explorer.exe %cd%Anime
The virus also creates a folder(and file) \RECYCLER\894133bf.exe and the shortcuts are linked through that EXE file (seen above).
I would like a batch file to convert the target path to:
K:\Anime
This way the shortcut can directly link to the file. The virus and folder were removed by Norton when I got my hard drive back. However, the shortcuts remain and they don't work unless I change the target path.
Since I have over 37 folders on my hard drive which have been converted to the shortcuts, I was thinking that maybe a script would be helpful doing the following:
Extracting the filename from the shortcut
Removing the .lnk from filename
Changing the target to K:\{shortcut name}
Go to next folder and loop until last folder
I came across a script to extract a filename here:
http://blogs.technet.com/b/heyscriptingguy/archive/2006/05/30/how-can-i-extract-just-the-file-name-from-the-full-path-to-the-file.aspx
However, I do not know how to put it in a loop to do it for each folder.
Any help will be much appreciated. Thank you very much.
objFolder.Name in the For Loop is the string for each Folder it's iterating through.
Option Explicit
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
strFolderToSearch = InputBox("Enter Path:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
strOutput = strOutput & objFolder.name
strOutput = strOutput & vbCrLf
MsgBox StrOutput
Next
Related
I am making a simple VBS application which has a bit where it creates a file in the startup directory. When I did it a while back, it worked, but I've since updated my code and need it to do other things too.
This is the code that creates the file:
Dim fso, openFile
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
It gives some sort of error saying it does not know where to go, I don't know if there's a problem with the file path I've supplied, because C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ is a folder, obviously. I've also just discovered that there is no startup\ folder in it, is that what's causing it?
If so, is there something I can specify that creates the directory if it does not exist?
I'm very new to VBS and I have a very limited knowledge of it, so a simple way to do this would be much appreciated.
Thanks! :)
Is the filepath correct? (can you find it mannualy?) If yes, then try to copy the path in a variable and call the variable with the path.
Dim fso, openFile
Dim filepath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile(filepath)
And also make sure, that the file don't exists before you start the programm.
Good Luck
Jonas
I have a question based on a previus question asked here.
I need to change registry key entries on multiple computers where REG GUIDs of said registry keys are different on every machine. I have found that I can pull the regkey into a .reg or .txt file, find the “Device_State” values, and replace them with what I want using a .vbs script similar to the one in the link I referenced above.
I cannot use any 3rd party solutions for this. It must be organic to Windows, by nature (Windows 7 and windows 10 is currently my env).
Problem
My Test.vbs script appears to work when I just have the .reg file in a directory, and run (double-click) the .vbs script (from same directory).
The script finds and replaces the strings I want and writes it all back to the .reg file (in this case, "replace.reg"). However, when I call it from my batch file, the .vbs script returns an error:
Line: 15 Char: 1 Error: Invalid procedure call or argument
Code: 800A0005 Source: Microsoft VBScrpt runtime error
line 15, 1 in my code is: objFile.Write strNewText.
When I check the .reg to see what has changed, the file is now blank. It seems like the script is reading the content of the file into the strText variable, finding my values, replacing values, then erroring out and not writing back to the .reg file.
Batch file
My batch file does the following:
creates a directory for backup and to work out of
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg
exports the registry key to a .reg file (and a separate .txt for reference) for actual manipulation of file content:
reg export “hkey_name” c:\windows\patches\replace.reg
reg export "hkey_name" c:\windows\patches\replace.txt
calls the .vbs script
#call cscript “%~dp0Test.vbs”
I have also tried
runas /user:localadmin /savecred "wscript.exe c:\windows\Patches\Test.vbs"
imports the new .reg into the registry (this is the plan; have not gotten this far yet).
reg import c:\windows\patches\replace.reg
VBScript
The Test.vbs script is as follows:
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpentTextFile(“C:\Windows\Patches\backup.reg”, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ”HKEY”, ”BLAH-BLAH”) '<—– this is just for testing function!
strNewText = Replace(strText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
objFile.Write strNewText
objFile.Close
MsgBox "Done"
So, again, to reiterate, the VBScript does exactly what I want when I run it independently. But when I call it from a batch file, it "wipes out" (or fails to write to) the file I need changed.
I am running this as an administrator of the local system, with full rights to the files in question.
I am running this from an elevated CMD prompt/window.
Question
Does anyone know why this would work independently of the batch file, yet not work when called by the batch file? I'm thinking it has to have something to do with permissions or authority to open/close the file for reading/writing while "inside" a batch routine. I just don't know enough to figure it out.
***************************** UPDATE ************************************
I have narrowed this down even further and here is what I'm finding:
-
1) I run my batch file that creates a backup of the registry keys I need to edit. The batch file creates a .reg file and a .txt file of the same registry keys (this is just for my own security to have both). I'm also granting full rights to everyone, users, administrators on this folder/files
2) I then run the find/replace text vbScript. It errors out on the line "objFile.Write strNewText". When it errors out it appears to wipe out the contents of the .reg file.
3) I found that if I take everything in the .txt file (same exact text) and paste into the .reg file (AFTER the vbscript errors out) and THEN run my script, it works.
I have tried manually deleting the .reg contents and replacing it with the .txt content and trying to run the script, and it errors out the same way and again appears to wipe out the contents of the .reg file. It's ONLY AFTER THE VBSCRIPT errors out on writing to the file, appears to wipe out the contents, that I can paste the contents of the .txt file into the .reg file and run the VBScript ... and it works. I'm so confused by this. I can't imagine what's going on. Please, I'm hoping someone else can help me. Thanks.
There is likely an error being thrown that you can't see. In your batch file, at the end of each line, add > output.txt, like so:
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg > outputExport1.txt
reg export "hkey_name" c:\windows\patches\replace.reg > outputExport2.txt
reg export "hkey_name" c:\windows\patches\replace.txt > outputExport3.txt
cscript "%~dp0Test.vbs" > outputVBS.txt
This will dump any error information you would have seen in the command prompt to the output file. Alternatively, you can manually run each line of your batch file in a command prompt and see what it says there.
Inside your script, you can add additional debugging outputs (and they will also either be dumped into your output file or display in the command prompt if you run it manually):
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
WScript.Echo "Reading the file"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpentTextFile("C:\Windows\Patches\backup.reg", ForReading)
strText = objFile.ReadAll
WScript.Echo "File Contents: " & strText
objFile.Close
strNewText = Replace(strText, "HKEY", "BLAH-BLAH") '<—– this is just for testing function!
WScript.Echo "Did first replace: " & strNewText
strNewText = Replace(strNewText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
WScript.Echo "Did second replace: " & strNewText
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
WScript.Echo "Opened the File for Writing"
objFile.Write strNewText
WScript.Echo "Wrote the File"
objFile.Close
WScript.Echo "Closed the File"
WScript.Echo "Done"
Note that in the code above I corrected a bug you have in the second Replace - in the original code you are reusing the strText variable, which means the changes done by the first Replace are overwritten.
First off I am not a coder at all so I am probably missing something very basic.
I am writing a script that will run via GPO when our users login to some of our terminal servers to check if a specific file exists in the users home folder, if it does it should exit, if not then it should copy an .ini file from another location. Here is the script I have that is failing.
On Error Resume Next
dim shell
set shell=createobject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\%USERPROFILE%\AppData\Roaming\Microsoft Dynamics SL\solomon.ini") Then
Wscript.Quit
Else
objFSO.CopyFile "\\Server\sharepath\file.ini" , "C:\%USERPROFILE%\AppData\Roaming\Microsoft Dynamics SL\"
Wscript.Echo "File Copied."
End If
It just displays the File Copied echo when I run the script, no file is copied if the file is not present, and if the file is there it still ticks past it and displays the file copied text.
Edit: Tried to clean up the way it is displayed here on the forum.
Try this:
On Error Resume Next
Dim strAppDataPath
strAppDataPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%appdata%")
With CreateObject("Scripting.FileSystemObject")
If .FileExists(strAppDataPath & "\Microsoft Dynamics SL\solomon.ini") Then
Wscript.Quit
Else
.CopyFile "\\Server\sharepath\file.ini", strAppDataPath & "\Microsoft Dynamics SL\"
Wscript.Echo "File Copied."
End If
End With
you cannot use system environment variables within the VB Strings.
expand them beforehand as specified by omegastripes and use concatenate operator to append them to your path.
I am trying to build a VBScript to automatically run some .exe files. The problem is that the script and the .exe files are on a flashdrive, so it needs to find the current drive letter by itself. I can do it on a batch file using %~d0, but I like some of the functions of VBScript better, especially the ability to send keystrokes. Anyways, I found a whole list of VBScript commands, but I am no expert and I need help with the syntax. So far I have it set to open the task manager and press some keys to have it select the "performance tab" of the task manager:
Dim Act :Set Act = CreateObject("Wscript.Shell")
Act.Run("taskmgr.exe")
Success = Act.AppActivate("taskmgr")
Wscript.Sleep 250
Act.SendKeys "{TAB 5}" :WScript.Sleep 500
Act.SendKeys "{RIGHT 3}" :WScript.Sleep 500
I'd like to know what command I need to use to tell the script to use the drive letter where the script was executed from (USB drive).
Use the .ScriptFullName property to get the full file spec of the running script and apply .GetParentFolderName for the folder's path or .GetDriveName for just the drive letter.
>> Set oFS = CreateObject("Scripting.FileSystemObject")
>> s = WScript.ScriptFullName
>> WScript.Echo oFS.GetParentFolderName(s), oFS.GetDriveName(s)
>>
M:\bin M:
cf. here
I want to open .mp3 files with mpg123.exe silently when a .mp3 file is double clicked from within Windows Explorer. For this I wrote a VBScript as bellow and changed the default program for playing .mp3 files by assigning my VBScript to it via Open with → Choose default program. My script is working well from within command line (cmd.exe) but when a .mp3 file is double clicked I get an error message that double clicked .mp3 file is not an executable file in Windows. Here is my VBScript, please let me know where I am doing wrong.
if Wscript.arguments.count = 0 then
WScript.quit
else
strSoundFile = WScript.Arguments.Item(0)
end if
Set objShell = CreateObject("Wscript.Shell")
strCommand = "mpg123.exe -q " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
Why don't you just associate mp3 files with mpg123.exe and set up the associated parameters (eg: -q "%1") instead?
Since I couldn't find a notable existing example, I've whipped up an example for you. (tested to work on Windows 7 with mpg123.exe). The response was too image heavy to post here. I hope it helps you.