Solved

Convert my VB into a Batch File

Posted on 2007-03-23
14
266 Views
Last Modified: 2010-04-16
I got a VBScript that i created in order to change/update filenames in a specified folder based on a couple of IF factors. I was hoping to convert it into a DOS-Batch file for ease of being able to run it on a clients machine which doesn't have Visual Studio, just set a specified path for them then all they would have to do is run the batch any time to update files in the folder.

My VB skills are minimal and Batch Program skills next to none (just a few basic cmds), so I was hoping someone could so kindly translate it into Batch Format, or even open for simple alternative suggestions.


Dim objFSO
        Dim strCurrentFolder
        Dim objFolder
        Dim newFile
        Dim objFile
        objFSO = CreateObject("Scripting.FileSystemObject")
        strCurrentFolder = "I:\Project_Data\"
        objFolder = objFSO.GetFolder(strCurrentFolder)
        For Each objFile In objFolder.Files
            If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "ABC") Or InStr(objFile.Name, "EFG") Then
                If Mid(objFile.Name, 14, 1) = "." Then
                    newFile = "2007-" & Left(objFile.Name, 8) & "LID" & Mid(objFile.Name, 8, 6) & ".pdf"
                    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
                End If
            End If
        Next
        For Each objFile In objFolder.Files
            If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "JKLM") Or InStr(objFile.Name, "EFGH") Then
                If Mid(objFile.Name, 15, 1) = "." Then
                    newFile = "2007-" & Left(objFile.Name, 9) & "LID" & Mid(objFile.Name, 9, 6) & ".pdf"
                    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
                End If
            End If
        Next



Thanks
Jay
0
Comment
Question by:SES_Team
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 18778720
You don't need to convert this to a batch file. VBScript does not require visual studio installed in order to run. It runs within the Scripting Runtime context and uses either CScript or WScript to run. These are installed usually with internet explorer and / or the operating system itself. If you have a .vbs script file then it can be run on any machine where this is not explicitly removed or disabled. Most machines will have the scripting runtime already installed.

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18778894
Just to complement what TimCottee has correctly stated, if your code is simply saves as a file with the extension .vbs, then you can simply Double-click on that file, and it will run, automatically.

In addition, if you have any version of Windows newer than Windows 98, there is no version of DOS whatsoever on your box.  That has been completely integrated with Windows.

AW
0
 

Author Comment

by:SES_Team
ID: 18779361
Excuse me for I mixed around my terminology. Your right about DOS, I was thinking Windows Batch (cmd prmpt) but said DOS-Batch. Also I said that I had VBScript when I actually have VB macro.

I appreciate the idea though, of course seems easier then coming up with a batch file. Can you please give me a little hint what I'm missing. I'm getting a compilation error - "Cannot use parentheses when calling a Sub".

What do I need to change?

Jay

0
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
LVL 67

Expert Comment

by:sirbounty
ID: 18779383
I believe it's these lines...
objFSO.MoveFile(objFile, strCurrentFolder & newFile)
objFSO.MoveFile(objFile, strCurrentFolder & newFile)

remove the parentheses and it should work...
0
 

Author Comment

by:SES_Team
ID: 18779532
I tried that after receiving the error msg and tracing down the line & char mentioned from the error. When I remove the parentheses from those lines I don't receive an error but then the MoveFile cmd doesn't work and nothing updates as it should.

It works when I run it in MS Visual Studio, just when trying to use it as vbs has some issue.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779563
Let's try this...missing a couple of 'set's above...

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
strCurrentFolder = "I:\Project_Data\"
Dim objFolder:objFolder = objFSO.GetFolder(strCurrentFolder)
Dim newFile
Dim objFile
For Each objFile In objFolder.Files
  If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "ABC") Or InStr(objFile.Name, "EFG") Then
    If Mid(objFile.Name, 14, 1) = "." Then
    newFile = "2007-" & Left(objFile.Name, 8) & "LID" & Mid(objFile.Name, 8, 6) & ".pdf"
    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
    End If
  End If
Next
For Each objFile In objFolder.Files
  If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "JKLM") Or InStr(objFile.Name, "EFGH") Then
    If Mid(objFile.Name, 15, 1) = "." Then
      newFile = "2007-" & Left(objFile.Name, 9) & "LID" & Mid(objFile.Name, 9, 6) & ".pdf"
      objFSO.MoveFile(objFile, strCurrentFolder & newFile)
    End If
  End If
Next
0
 

Author Comment

by:SES_Team
ID: 18779687
I don't see what is really different besides the format of moving things around. And I get that error again pertaining to;

objFSO.MoveFile(objFile, strCurrentFolder & newFile)

Could it be that I'm calling it as a Sub(), in vbs do I need to call it using Function or something other then Sub()?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779699
Try it by specifying objFile.Path instead of objFile

objFSO.MoveFile objFile.Path, strCurrentFolder & newFile
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18779767
Try this. It's in debug mode so it just echos out the command. To activate REM the set debug statement as annotated in the REM statement.

@echo off

setlocal

REM ** REM out the next statement to enable processing: REM set debug=echo
set debug=echo

set curFolder=I:\Project_Data
set fileMask=*.pdf

pushd "%curFolder%"

for /f "tokens=*" %%a in ('dir /b /a-d "%fileMask%" 2^>NUL ^| findstr /i "ABC EFG"') do call :PROCESS "%%a"

popd

goto :EOF

:PROCESS

set fileName=%~1

if not "%fileName:~14,1%"=="." goto :EOF

set fileName=2007-%fileName:~0,8%LID%fileName:~8,6%.pdf

%debug% ren "%~1" "%fileName%"

Good Luck,
Steve
0
 

Author Comment

by:SES_Team
ID: 18821971
Sirbounty,

Thanks for the help, but I receive the same error when trying to specify objFile.Path instead of objFile.

Line:    16
Char:   73
Error:  Cannot use parentheses when callin a s Sub
Code:  800A0414

0
 

Author Comment

by:SES_Team
ID: 18821983
Steve,

I have also tried your batch, while turning off echo it will run but doesn't seem to do anything. I don't recieve any msg, it runs then closes and nothing is update.

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18822595
Line 16 appears to be

 If Mid(objFile.Name, 15, 1) = "." Then

What are you trying to accomplish there?  I'd suspect it's processing a file that's not 15 characters long...

I think you're better off using
if right(objfile.name,4"="."
or if just getting the extension
if objfso.getextensionname(file.name)="pdf"
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 125 total points
ID: 18822772
Let me place some debugging information in and have you paste the output:

@echo off

setlocal

REM ** REM out the next statement to enable processing: REM set debug=echo
set debug=echo

set curFolder=I:\Project_Data
set fileMask=*.pdf

pushd "%curFolder%"

for /f "tokens=*" %%a in ('dir /b /a-d "%fileMask%" 2^>NUL ^| findstr /i "ABC EFG"') do call :PROCESS "%%a"

popd

goto :EOF

:PROCESS

echo File name: %~1

set fileName=%~1

if not "%fileName:~14,1%"=="." goto :EOF

set fileName=2007-%fileName:~0,8%LID%fileName:~8,6%.pdf

%debug% ren "%~1" "%fileName%"
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18842762
Just love the grading system here... Makes perfect sense --- lol
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question