Windows BAT : test if a specific file is empty - windows

I would like to check if a specific file is empty in a windows .bat file. Here is my non working script :
set dir="C:\test"
set file="%dir%\fff.txt"
cd %dir%
if %file%%~zi == 0 exit
ftp -s:"%dir%\ftp.action"
exit
Could you help me debug this please ?

Or try it with
#echo off
set "dir=C:\temp"
set "file=%dir%\a.txt"
call :CheckEmpty "%file%"
goto :eof
:CheckEmpty
if %~z1 == 0 exit
ftp -s:"%dir%\ftp.action"
goto :eof
The main difference is that I use a function call and use the %~z1, as the modifiers only works for paramters like %1, %2..%9 or for-loop parameters like %%a ...

batch solution using file compare:
type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty

Try this:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\boot.ini", ForReading)
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Related

Issue error checking a batch file run via VBScript

I am unsure why InstallResult always returns a 1 in my VBScript. I have put an echo in my batch file to confirm if I delete the source file before a copy it returns a 4 and that it returns nothing if it is successful. Any help would be appreciated. My files should do the following:
Copy a script from a network share to the local machine.
Run a batch file to install office (currently some test code for error checking). The batch file should run and post an error code on exit or a 0 if successful.
Go back to the VBScript to error check and run another cleanup VBScript.
Here is my code:
Run install bat (VBScript)
Dim objshell, InstallResult
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "\\altirisdata\AssetMgmt\Tools\WSM\DeleteOffice13Package.vbs", "C:\source\DeleteOffice13Package.vbs"
'DeleteMS2013FilePath = objShell.run ("c:\source\DeleteOffice13Package.vbs", 0, True)
WScript.Sleep 3000
Set objShell = WScript.CreateObject("WScript.Shell")
InstallResult = objShell.run ("cscript.exe C:\source\Microsoft_Office_2013_01\install.bat", 0, True)
WScript.Echo InstallResult
If InstallResult <> 0 Then WScript.Echo "Unable to install Microsoft Office 2013. Please manually check the install results"
If InstallResult = 0 Then
DeleteDelScript = objShell.Run("cscript.exe c:\source\DeleteOffice13Package.vbs", 0, True)
End If
If DeleteDelScript = 0 Then
FSO.DeleteFile("C:\source\DeleteOffice13Package.vbs")
End If
Set FSO = nothing
WScript.Quit
install.bat
#echo off
xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y
if %errorlevel% neq 0 (
exit /b %errorlevel%
)
exit
You get a return value of 1, because you're trying to run a batch script with a VBScript interpreter:
InstallResult = objShell.run ("cscript.exe C:\sourc...l.bat", 0, True)
Remove cscript.exe from the commandline, or replace it with %COMSPEC% /c:
InstallResult = objShell.run ("%COMSPEC% /c C:\sourc...l.bat", 0, True)
As a side note, you shouldn't need a condition in your batch script. Simply returning the errorlevel should suffice:
#echo off
xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y
exit /b %errorlevel%

Text encoding on WScript arguments

I'm trying to generate mail configurations and personalized signatures through a batch file that reads a list of users, a template, and creates a personalized output. That's done and works:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS
GOTO begin
:writesignature
cscript //NoLogo replacetext.vbs "[NAME]" %1 signature.html stdout | cscript //NoLogo replacetext.vbs "[JOB]" %3 stdin stdout | cscript //NoLogo replacetext.vbs "[EMAIL]" %2 stdin signature-%4.html
GOTO :end
:begin
FOR /F "tokens=1,2,3,4 delims=;" %%A IN ('TYPE people.lst') DO CALL :writesignature "%%A" "%%B" "%%C" %%D
:end
To do the text replacing, I created replacetext.vbs, that allows me to replace a string for oter, and can be piped if stdin and stdout are indicated as the source and target files:
CONST ForReading = 1
CONST ForWritting = 2
CONST ForAppending = 8
CONST OpenAsASCII = false
CONST OpenAsUnicode = true
CONST OpenAsDefault = -2
Const OverwriteIfExist = true
Const FailIfExist = false
Const CreateIfNotExist = true
Const FailIfNotExist = false
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objFILEINPUT = Wscript.StdIn
SET objFILEOUTPUT = Wscript.StdOut
IF (Wscript.Arguments.Count < 2) OR (Wscript.Arguments.Count > 4) THEN
Wscript.Echo "Not enought arguments"
Wscript.Echo "replacetext ""<original>"" ""<replacement>"" "
Wscript.Quit(1 MOD 255)
END IF
IF Wscript.Arguments.Count > 2 THEN
IF Wscript.Arguments(2) = "stdin" THEN
' Wscript.Echo "Input: StdIn"
ELSE
' Wscript.Echo "Input: " + Wscript.Arguments(2)
SET objFILEINPUT = objFSO.OpenTextFile(Wscript.Arguments(2), ForReading, OpenAsASCII)
END IF
IF Wscript.Arguments.Count = 4 THEN
IF Wscript.Arguments(3) = "stdout" THEN
' Wscript.Echo "Output: StdOut"
ELSE
' Wscript.Echo "Output: " + Wscript.Arguments(3)
IF objFSO.FileExists(Wscript.Arguments(3)) THEN
SET objFILEOUTPUT = objFSO.OpenTextFile(Wscript.Arguments(3), ForWritting, CreateIfNotExist, OpenAsASCII)
ELSE
SET objFILEOUTPUT = objFSO.CreateTextFile(Wscript.Arguments(3), OverwriteIfExist, OpenAsASCII)
END IF
END IF
END IF
END IF
strText = objFILEINPUT.ReadAll()
strNewText = Replace(strText, Wscript.Arguments(0), Wscript.Arguments(1))
objFILEOUTPUT.Write(strNewText)
objFILEOUTPUT.Close
objFILEINPUT.Close
Wscript.Quit(0 MOD 255)
The problem is that when I put non-ASCII characters in ANSI/Windows-1250 in the people.lst (Comunicación), while it works and reads them in console, showing them (not converting them) as OEM characters (Comunicaci¾n) when I write the output files, somehow it does convert them transparently, so the output file in Windows shows Comunicaci¾n instead of Comunicación.
After much debugging, I've localized the problem in ONLY the arguments (no automatic conversion on the template file).
How can I disable said transparent conversion, or convert back the input from ANSI to OEM so the conversion works as intended?
The problem is that the cmd.exe works with different code page than cscript.exe/wscript.exe. I have similiar problem in Poland, where cmd.exe works with codepage 852 (I believe this is for compatibility with older MS-DOS programs) and wscript.exe works in Windows' native codepage 1250.
To solve the problem, put the following line on the beginning of the batch file:
mode con cp select=1250

bat read a file line by line

In my bat script, is it possible to access a txt file and read it line by line. The idea I'm having is to check if the line starts with an identifier word (in my case 1 or 2 stars * or **) but to do this I need to read the file line by line.
you can use vbscript
strToFind= WScript.Arguments(0)
strToFind = Replace(strToFind,"*","\*")
strFileName = WScript.Arguments(1)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFS.OpenTextFile(strFileName)
Set objRE = New RegExp
objRE.IgnoreCase = False
objRE.Pattern = "^"&strToFind&".*"
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set Matches = objRE.Execute(strLine)
'WScript.Echo Matches.Count
For Each Match in Matches ' Iterate Matches collection.
WScript.Echo Match.Value
Next
Loop
objFile.Close
Usage:
C:\test>cscript //nologo myscript.vbs "**" file
Here's what I found: http://www.computing.net/howtos/show/batch-file-tip-reading-writing-every-line-of-a-file/61.html
Hope that helps..
CODE:
#echo off
for /f "delims=] tokens=1*" %%a in ('find /v /n "" ^<%1') do (
echo.%%b
)

How to set environment variables in vbs that can be read in calling batch script

I have a batch file that calls a vbscript file. I am trying to have the vbscript file change an environment variable that is later used in the batch file that calls the vbscript file.
Here are snippetes from the files.
Parent.bat
Set Value="Initial Value"
cscript Child.vbs
ECHO Value = %VALUE%
Child.vbs
Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "Process" )
wshSystemEnv("VALUE") = "New Value"
You can't. A process can pass environment variables to child processes, but not to its parent - and in this case the parent is cmd.exe, which is running your Parent.bat file.
There are of course other ways to communicate information back to the parent batch file - outputting to stdout or a file is an obvious way, e.g.
== Child.vbs ===
WScript.echo "New Value"
== Parent.cmd ===
for /f "tokens=*" %%i in ('cscript //nologo child.vbs') do set Value=%%i
echo %Value%
yes, you can.... however, you'll have to resetvars in your session. see the following link:
Is there a command to refresh environment variables from the command prompt in Windows?
'RESETVARS.vbs
Set oShell = WScript.CreateObject("WScript.Shell")
filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat")
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set oFile = objFileSystem.CreateTextFile(filename, TRUE)
set oEnv=oShell.Environment("System")
for each sitem in oEnv
oFile.WriteLine("SET " & sitem)
next
path = oEnv("PATH")
set oEnv=oShell.Environment("User")
for each sitem in oEnv
oFile.WriteLine("SET " & sitem)
next
path = path & ";" & oEnv("PATH")
oFile.WriteLine("SET PATH=" & path)
oFile.Close
This is how I did it:
SET oShell = CREATEOBJECT("Wscript.Shell")
dim varSet
SET varSet = NOTHING
SET varSet = oShell.Environment("SYSTEM")
varSet("WinVer") = "6.0.2008"
Then in a separate VB script (resetvars.vbs) I called from CMD script:
cscript //nologo \\%APPSERVER%\apps\IE9.0\restartvars.vbs
call %TEMP%\resetvars.bat
I don't think you can do this. At least, you would need to mess with the environment block in the calling process, and there's no guarantee that it will respect this...
Ho about this:
#echo off
set vbsFile=%temp%\createguid.vbs
call :CreateVbs
call :GetGuid NewGuid
echo.%NewGuid%
del %vbsFile%>nul
GOTO:EOF
:CreateVbs
echo.set obj = CreateObject("Scriptlet.TypeLib")>%vbsFile%
echo.WScript.StdOut.WriteLine obj.GUID>>%vbsFile%
GOTO:EOF
:GetGuid
for /f "tokens=*" %%i in ('cscript //nologo %vbsFile%') do set %1=%%i
GOTO:EOF
It is not pure batch script but works ok.
#echo off&color 4a&title %~n0&AT>NUL
IF %ERRORLEVEL% EQU 0 (
goto 2
) ELSE (
echo.
)
if not "%minimized%"=="" goto 1
set minimized=true & start /min cmd /C "%~dpnx0"&cls&exit
:1
wmic process where name="cmd.exe" CALL setpriority "realtime">nul&echo set shell=CreateObject("Shell.Application") > %~n0.vbs&echo shell.ShellExecute "%~dpnx0",,"%CD%", "runas", 1 >> %~n0.vbs&echo set shell=nothing >> %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit
:2
echo %~dpnx0 admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"&timeout 3 /NOBREAK>nul
:3
echo x=msgbox("end of line" ,48, "%~n0") > %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit

Read ONLY x lines from a txt file with a windows batch file

How can I read only X lines from a a.txt file?
The file contains all the names of a directory, I would like to read only x lines.
X can be a number that can varies from 1 to 99
You'll need to modify this based on your needs, but the script below will loop through the file 'directories.txt', and ECHO the contents of the line until you hit the maximum number of lines set in maxlines.
#ECHO OFF
setlocal enabledelayedexpansion
SET /A maxlines=1
SET /A linecount=0
FOR /F %%A IN (directories.txt) DO (
IF !linecount! GEQ %maxlines% GOTO ExitLoop
ECHO %%A
SET /A linecount+=1
)
:ExitLoop
PAUSE
you can use vbscript. Here's an example
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strNum = objArgs(0)
strFile=objArgs(1)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
If CInt(objFile.Line) > CInt(strNum) Then
Exit Do
End If
strLine=objFile.ReadLine
WScript.Echo strLine
Loop
save as myscript.vbs and
c:\test> cscript //nologo myscript.vbs 99 file
Or if have the luxury to install tools,
you can download sed or gawk for windows . Then on the command line
sed.exe "99q" file
gawk.exe "NR>2{exit}1" file

Resources