0

I have a bat file that calls a vbscript that converts the encoding of a file. while running the code by setting the parth of the file it works fin, but when I am trying to pass more than one file as parameter i am getting the following error: microsoft vbscript runtime error :invalid or unqualified reference

You can see my batch script below run.cmd :

@Echo off
SET CHEMINDAT = C:\Users\myuser\Desktop\TRY\DATA_TY\
SET CHEMINTMP = C:\Users\myuser\Desktop\TRY\TMP\

echo %DATE% %TIME% Debut de traitement.

dir %CHEMINDAT%* /a:-d/b > %CHEMINTMP%-liste.txt 2

FOR /F "eol=; tokens=1 delims=, " %%i in (%CHEMINTMP%-liste.txt) do (

echo %%i > %CHEMINTMP%Fichier_en_cours.txt
echo %DATE% %TIME% Traitement du fichier : %%i

cscript //NoLogo C:\Users\myuser\Desktop\TRY\\Scripts\unix2dos_7.vbs %%i

)
pause
exit /b 1

Below you will fins my VBScript 'unix2dos_7.vbs':

Const adTypeBinary          = 1
Const adTypeText            = 2
Const adSaveCreateNotExist  = 1
Const adSaveCreateOverWrite = 2
Const adWriteLine = 1
Const adReadLine = -2
Const adLF = 10
Const adCR = 13
Const adCRLF = -1
Set streamUTF8 = CreateObject("ADODB.Stream")
Set streamASCII = CreateObject("ADODB.Stream")

Dim args
set args = Wscript.arguments
strOutOne= args(0)

streamUTF8.Open
streamUTF8.Type = adTypeText
streamUTF8.LineSeparator = adCRLF
streamUTF8.Charset = "UTF-8"
streamUTF8.LoadFromFile strOutOne

streamASCII.Open
streamASCII.Type = adTypeText
streamASCII.LineSeparator = adCRLF
streamASCII.CharSet = "us-ascii"

Do Until streamUTF8.EOS
streamASCII.WriteText streamUTF8.ReadText(adReadLine), adWriteLine
Loop
streamASCII.SaveToFile "myfileConverted.txt", adSaveCreateOverWrite

I also want to replace the original file with the new one, instead of creating a new one 'myfileConverted.txt'. so that I don't have two times the same document

Can anyone pleas help?

Thanks in advance

  • The first question I'd ask is, why? I'd also suggest, that you take a look at this [previous question](https://stackoverflow.com/q/42774941), it's comments, and it's answer, _which leverages [tag:powershell], as opposed to [tag:vbscript]_. – Compo Feb 21 '20 at 12:43
  • 1
    If you are dealing with long filenames, then you probably need to double-quote (") the `%%i` in your .cmd file when calling the .vbs; otherwise, your .vbs is getting only the first part of the path. Also, get rid of your double-backslash (\\) after TRY. Optionally, you may need to call the 32-bit version of cscript.exe. Finally, be sure to `.Close` your ADODB.Stream objects in the .vbs script. `C:\Windows\SysWOW64\cscript.exe //NoLogo C:\Users\myuser\Desktop\TRY\Scripts\unix2dos_7.vbs "%%i"` – leeharvey1 Feb 21 '20 at 12:50

1 Answers1

0

I was able to do what i wanted below you would see the code: the batch code:

@Echo off
SET CHEMINDAT = C:\Desktop\try\script_try_2\data\
SET CHEMINTMP = C:\\Desktop\try\TMP\

echo %DATE% %TIME% Debut de traitement.
dir %CHEMINDAT%* /a:-d/b > %CHEMINTMP%-liste.txt 

FOR /F "eol=; tokens=1 delims=, " %%i in (%CHEMINTMP%-liste.txt) do (

echo %%i > %CHEMINTMP%Fichier_en_cours.txt
echo %DATE% %TIME% Traitement du fichier : %%i


cscript //NoLogo C:\Desktop\try\script_try_2\unix2dos_7_2.vbs "%%i"
)

pause
exit /b 1

My vbScript:

Const adTypeBinary          = 1
Const adTypeText            = 2
Const adSaveCreateNotExist  = 1
Const adSaveCreateOverWrite = 2
Const adWriteLine = 1
Const adReadLine = -2
Const adLF = 10
Const adCR = 13
Const adCRLF = -1
Set streamUTF8 = CreateObject("ADODB.Stream")
Set streamASCII = CreateObject("ADODB.Stream")

set args = Wscript.arguments
strOutOne= args(0)

streamUTF8.Open
streamUTF8.Type = adTypeText
streamUTF8.LineSeparator = adCRLF
streamUTF8.Charset = "UTF-8"
streamUTF8.LoadFromFile = strOutOne

streamASCII.Open
streamASCII.Type = adTypeText
streamASCII.LineSeparator = adCRLF
streamASCII.CharSet = "us-ascii"

Do Until streamUTF8.EOS
streamASCII.WriteText streamUTF8.ReadText(adReadLine), adWriteLine
Loop
streamASCII.SaveToFile(strOutOne), adSaveCreateOverWrite