0

This is strange: The script works perfectly when run by double clicking [outputs the (split)arc files], when textFile is hard coded. When dragging and dropping the file onto the script, there are no errors but also no output files. The script definitely runs when using drag and drop, I added a simple message box to the line just before writeTo, to confirm that it does reach that point. No files output when dragging and dropping though, only works correctly when textFile is hard coded. Someone please help!

if WScript.Arguments.Count <> 0 then

    textFile = WScript.Arguments(0)

else

    textFile = "multi2.txt"

end if


saveTo = ""
writeTo = ""
strNewLine = "%_N_"
headingPattern = "(%_N_)"

dim fileFrom, regex, fileTo
Set fso = CreateObject("Scripting.FileSystemObject")

set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
set fileTo = nothing


with regex
    .Pattern = headingPattern
    .IgnoreCase = false
    .Global = true
end with

while fileFrom.AtEndOfStream <> true
    line = fileFrom.ReadLine
    set matches = regex.Execute(line)
    if matches.Count > 0 then

        strCheckForString = UCase("%")
        strNewLine = "%_N_"

        StrContents = Split(fso.OpenTextFile(textFile).ReadAll, vbNewLine)
        If (Left(UCase(LTrim(line)),Len(strCheckForString)) = strCheckForString) Then
            line = Right(line, len(line)-4)
            line1 = Left(line, len(line)-4)
            writeTo = saveTo & (line1 & ".arc")
            if not(fileTo is nothing) then fileTo.Close()
                set fileTo = fso.CreateTextFile(writeTo)
                fileTo.WriteLine(strNewLine & line)
        else
            fileTo.WriteLine(line)
        End If
    else
        fileTo.WriteLine(line)
    end if
wend

fileFrom.Close()

set fileFrom = nothing
set fso = nothing
set regex = nothing

The text file looks like this:

%_N_160_SP01_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah




%_N_160_SP02_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah




%_N_160_SP99_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
BertB
  • 108
  • 1
  • 9

1 Answers1

5

You are generating the output files, but not where you think. As you are not indicating where to generate the files, they are generated in the "current directory" for the started script process. And this "current directory" is not always the same:

  • If you double click over a .vbs file, the current directory for the operation is the folder in where the script is located.

  • If you drag and drop a file over a .vbs file, the current directory is the current active directory of the explorer instance that is handling the drop operation. Note that this directory is not what the explorer window shows, but the active directory of the explorer.exe process (usually %systemroot%\system32, but can change)

You have to determine where you want your files and explicitly indicate it on file creation.

So, if you need the files created in the .vbs folder use

writeTo = fso.BuildPath( _ 
    fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _ 
    , saveTo & (line1 & ".arc") _ 
)

If you need the files created in the same folder where the input file is, use

writeTo = fso.BuildPath( _ 
    fso.GetFile( textFile ).ParentFolder.Path _ 
    , saveTo & (line1 & ".arc") _ 
)

Or explicitly indicate where to create the files.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks! You were spot-on, adding the parent folder path was the way to go. – BertB Jul 08 '14 at 15:37
  • 1
    Maybe it depends on the Windows version, but I see the current working directory being `C:\Windows\System32` on Win8.1 x64 – Wolf Feb 04 '17 at 23:27
  • @Wolf, you are right. Tested now on Windows 10 and I also get the system directory. Answer changed. As it depends on system configuration, tomorrow I will re-test the behaviour in windows 7. Thank you – MC ND Feb 05 '17 at 08:48
  • Great you updated this, thanks in advance for the Win7 test :-) – Wolf Feb 05 '17 at 11:55
  • @Wolf, updated. It inherits the current active directory of the parent `explorer.exe` process – MC ND Feb 06 '17 at 12:56
  • Wow, impressive. How, for instance, could you change the active directory of the explorer process? Ah I see: http://stackoverflow.com/a/42068743/2932052 – Wolf Feb 06 '17 at 14:22