Avatar of CalmSoul
CalmSoul
Flag for United States of America asked on

batch script help

Hello Experts:

I am looking for simple VB script to read file path from file1.txt and copy each of them (files) to different folder on the network

Does anybody has anything they can share?
Windows BatchScripting LanguagesVB Script

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

No nead for a VBS for this, a small BAT script can do the job.  Save as a BAT, adjust filenames near top, and give it a test.

@echo off
setlocal

set DestDir=H:\destdir
set FileList=file1.txt

for /f "tokens=* usebackq" %%A in ("%FileList%") do (
  copy "%%~A" "%DestDir%"
)

Open in new window

~bp
NVIT

If he really wants VBS, then...
' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1

sTgtPath="\\server\share\"
sSrcPath="c:\dir\file1.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (sSrcPath, ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
Loop

Open in new window

CalmSoul

ASKER
Will it be possible to have output logs what got copied?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bill Prew

This will add some logging to the file specified.

@echo off
setlocal

set DestDir=H:\destdir
set FileList=file1.txt
set LogFile=copylog.txt

echo Started: %DATE% %TIME%>>"%LogFile%"

for /f "tokens=* usebackq" %%A in ("%FileList%") do (
  echo Copying: "%%~A" to "%%~A">>"%LogFile%"
  copy "%%~A" "%%~A"
)

echo Ended: %DATE% %TIME%>>"%LogFile%"

Open in new window

~bp
NVIT

VBS. Added logging.

' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

sTgtPath="\\server\share\"

sSrcPath="c:\dir\file1.txt"
sLog="%temp%\copyfilesLog.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set obSrcFN = objFSO.OpenTextFile (sSrcPath, ForReading)
Set obLogFN = objFSO.OpenTextFile (sLog, ForAppending, True)

Do Until obSrcFN.AtEndOfStream
    strNextLine = obSrcFN.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo Now & " " & "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
    obLogFN.WriteLine Now & " " & strNextLine
Loop

Open in new window

Bill Prew

@NVIT,

I think you forgot the close of the log file, which is critical.

~bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NVIT

' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

sTgtPath="\\server\share\"

sSrcPath="c:\dir\file1.txt"
sLog="c:\dir\copyfilesLog.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set obSrcFN = objFSO.OpenTextFile (sSrcPath, ForReading)
Set obLogFN = objFSO.OpenTextFile (sLog, ForAppending, True)

Do Until obSrcFN.AtEndOfStream
    strNextLine = obSrcFN.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo Now & " " & "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
    obLogFN.WriteLine Now & " " & strNextLine
Loop
obSrcFN.Close
obLogFN.Close

Open in new window

CalmSoul

ASKER
@NewVillageIT (NVIT) - on your script I am getting error permission denied
C:\temp\readncopy.vbs(19, 5) Microsoft VBScript runtime error: Permission denied

Open in new window

CalmSoul

ASKER
@Bill Prew - on your batch file ...
nothing get copys.. and when I looked at the logs it was copying from target to target
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
NVIT

@CalmSoul

Do you have copy rights to sTgtPath? Can you confirm by creating a file manually there?
CalmSoul

ASKER
@NewVillageIT (NVIT) - yes, I can create file manually
CalmSoul

ASKER
I even added everyone with full control on that folder ...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NVIT

Can you change sTgtPath temporarily, to a local folder that also has write rights? Then test. e.g.
sTgtPath="c:\testfolder\"

Open in new window

CalmSoul

ASKER
the folder I am copying in is local folder to my machine
Bill Prew

@CalmSoul,

@Bill Prew - on your batch file ...
nothing get copys.. and when I looked at the logs it was copying from target to target
What do the lines in your text file look like, full path, or just a file name?

What folder did you run the batch script from?

Can you paste up a few lines from the output log?

~bp
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
NVIT

@CalmSoul,

I don't get an error here. Can you post a sample of your sSrcPath file?
CalmSoul

ASKER
Bill- I was able to fix your script and got it working now... Question will it be possible to capture instance file not exist in different log file..

I want to capture file path.. Thanks
Bill Prew

@CalmSoul,

I don't understand the question you are now asking, can you explain further please.

~bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CalmSoul

ASKER
@Bill: During the copy process, if source file doesn't exist .. I get output "file not found" or something.. Is it possible to track that output in separate log file ...
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.