Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Convert my VB into a Batch File

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
SES_Team
Asked:
SES_Team
  • 5
  • 4
  • 3
  • +2
1 Solution
 
TimCotteeCommented:
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
 
Arthur_WoodCommented:
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
 
SES_TeamAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sirbountyCommented:
I believe it's these lines...
objFSO.MoveFile(objFile, strCurrentFolder & newFile)
objFSO.MoveFile(objFile, strCurrentFolder & newFile)

remove the parentheses and it should work...
0
 
SES_TeamAuthor Commented:
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
 
sirbountyCommented:
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
 
SES_TeamAuthor Commented:
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
 
sirbountyCommented:
Try it by specifying objFile.Path instead of objFile

objFSO.MoveFile objFile.Path, strCurrentFolder & newFile
0
 
SteveGTRCommented:
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
 
SES_TeamAuthor Commented:
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
 
SES_TeamAuthor Commented:
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
 
sirbountyCommented:
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
 
SteveGTRCommented:
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
 
SteveGTRCommented:
Just love the grading system here... Makes perfect sense --- lol
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now