Reputation: 921
How can I loop the following batch code on all files in a directory:
for /f "tokens=*" %%f in ('dir /b *.txt') do (
set OldFile=%cd%/%%f
set NewFile=%cd%/2%%f
echo. > "%NewFile%"
start far.vbs "%NewFile%" "%OldFile%"
)
where the far.vbs
is as follows:
Set OldFile = CreateObject("Scripting.FileSystemObject")
Set rdOldFile = OldFile.OpenTextFile(WScript.Arguments(1), 1)
oContent = rdOldFile.ReadAll
rdOldFile.Close
Set lOldFile = CreateObject("Scripting.FileSystemObject")
Set lrdOldFile = OldFile.OpenTextFile(WScript.Arguments(1), 1)
oLen = Len(lrdOldFile.ReadAll)
lrdOldFile.Close
oData = oContent
oData = Right(oContent, oLen-1)
oData = Left(oData, oLen-2)
Set NewFile = CreateObject("Scripting.FileSystemObject")
Set fData = NewFile.OpenTextFile(WScript.Arguments(0), 2)
fData.WriteLine (oData)
fData.Close
Currently no file is generating and the vbs code does not seem to execute. The directory should contain a text file with some generic string and the far.vbs
script with remove the first and last two characters.
The Purpose of this script is to delete the portions of unnecessary characters from multiple files in a folder.
Upvotes: 0
Views: 416
Reputation: 200293
Variable expansion in a for
loop doesn't work the way you expect. The entire loop is read as one statement, and all %var%
are expanded at that point. To make your code work you need to enable delayed expansion. You may also want to use cscript.exe
instead of start
.
setlocal EnableDelayedExpansion
for /f "tokens=*" %%f in ('dir /b *.txt') do (
set "OldFile=%cd%/%%f"
set "NewFile=%cd%/2%%f"
echo. > "!NewFile!"
cscript.exe //NoLogo far.vbs "!NewFile!" "!OldFile!"
)
Upvotes: 2